Study of Maven
Table of Contents
1 Start Maven Example
Maven projects are defined with an XML file named pom.xml. This file gives the project's name, version, and dependencies that it has on external libraries.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>gs-maven</artifactId> <packaging>jar</packaging> <version>0.1.0</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>hello.HelloWorld</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
<modelVerion>
POM model version<groupId>
Domain name of the group or organization.<artifactId>
Name to the project's library artifact (name of JAR file)<version>
Version of the project<packaging>
How it should be packaged (in JAR or WAR file)
mvn compile mvn package mvn install
The first command compiles the project, and the .classfiles shold be
in the target/classes directory.
The second command compile the code, run tests and finish by
packaging the code up in a JAR file in the target directory.
Tht third command does the same thing as the second command, then
copy the JAR file into the local dependency repository, under the
directories with name of groupId, artifactId and version. So on my
machine, its location is
~/.m2/repository/org/springframework/gs-maven/0.1.0/gs-maven-0.1.0.jar
.
Add dependencies of project into the pom.xml file, in the
<project>
element.
<dependencies> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version> </dependency> </dependencies>
This description will tell the maven to get the joda-time package
as external library. You can specify a <scope>
element to specify
if the dependencies are required for compiling the code but will be
provided during runtime by provided
; or decalre the dependencies
are only necessary for compiling and running tests.
When I compile the command, maven downloads the joda-time pakcage from https://repo.maven.apache.org/maven2/joda-time/. Does the maven use its domain when the group id does not contain the domain by default?
You can also create a project from scratch
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
1.1 Directory layout
project src main java [scala] resources environment.properties environment.test.properties environment.prod.properties config application.yml application-dev.yml application-[profile].yml [other] test java [scala] target classes the classes with the same structure as src/main the directories in src/main/resources test-classes the classes with the same structure as src/mainproject xxx.jar target
2 POM Configuration
Fully qualified name of project is "<groupId>:<artifactId>:<version>". If groupId is a domain, it will be transformed into directory path while downloading. Download something with groupId=org.X.A from url.com will download things from url.com/org/X/A in the end.
2.1 Inheritance
When we have two projects, A and B, A is supposed to be parent
project of B. You have to specify the pom.xml of project B to
indicate that the parent of B is A with the groupId,
artifactId and version of A. You can remove the groupId and
version of B to make A and B have the same /groupId and
version. One can also indicate the <relativePath>
for parent
project to put the A and B in the same directory level.
B inherite the dependencies from A.所以parent用来继承POM,dependency 用来解决代码依赖. parent的项目在代码上没有关系.
The packages downloaded by maven are put under HOME.m2/repository/.
所有 POM.xml 里面的元素都可以用 ${project.A.B.C}
的方式进行引用.比
方说如果在 XML 根路径下有这样的结构
<A><B>lalala<C>blabla<C/></B></A>
, ${project.A.B.C}
就等于
blabla
.