项目生命周期:process-resources -> compile -> process-classes -> process-test-resources ->
test-compile -> test -> prepare-package -> package
也可单独运行:
mvn resources:resources \
compiler:compile \
resources:testResources \
compiler:testCompile \
surefire:test \
jar:jar
Maven坐标:groupId:artifactId:packaging:version
groupId
团体,公司,小组,组织,项目,或者其它团体。团体标识的约定是,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。来自Sonatype的项目有一个以com.sonatype开头的groupId,而Apache Software的项目有以org.apache开头的groupId。
artifactId
在groupId下的表示一个单独项目的唯一标识符。
version
一个项目的特定版本。发布的项目有一个固定的版本标识来指向该项目的某一个特定的版本。而正在开发中的项目可以用一个特殊的标识,这种标识给版本加上一个“SNAPSHOT”的标记。
packaging
项目的类型,默认是jar,描述了项目打包后的输出。类型为jar的项目产生一个JAR文件,类型为war的项目产生一个web应用。
help插件:
$ mvn help:describe -Dplugin=help -Dfull
创建简单项目:
$ mvn archetype:create -DgroupId=org.clayz.simple -DartifactId=simple -DpackageName=org.clayz.simple
构建并打包:
$ mvn install
生成站点和报告:
$ mvn site
在不载入依赖情况下运行Java类:
$ mvn exec:java -Dexec.mainClass=org.clayz.simple.weather.Main
浏览项目依赖:
$ mvn dependency:resolve $ mvn dependency:tree
指定编译环境:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build>
忽略单元测试失败:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build>
跳过单元测试:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build>
构建打包好的命令行应用程序:
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build>
$ mvn assembly:assembly
创建一个web应用的pom:
<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.clay.web</groupId> <artifactId>clay-webapp</artifactId> <packaging>war</packaging> <version>1.0</version> <name>clay-webapp Maven Webapp</name> <url>http://clay.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_2.4_spec</artifactId> <version>1.1.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>clay-webapp</finalName> </build> </project>
为自定义JSP标签添加JSP 2.0规格说明:
<dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jsp_2.0_spec</artifactId> <version>1.1</version> <scope>provided</scope> </dependency>
多模块中父模块pom:
<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> <artifactId>simple-parent</artifactId> <packaging>pom</packaging> <name>Chapter 6 Simple Parent Project</name> <modules> <module>simple-weather</module> <module>simple-webapp</module> </modules> <parent> <artifactId>parent</artifactId> <groupId>org.sonatype.mavenbook.ch06</groupId> <version>1-SNAPSHOT</version> </parent> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </pluginManagement> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
其中simple-webapp子模块pom:
<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> <parent> <groupId>org.sonatype.mavenbook.ch06</groupId> <artifactId>simple-parent</artifactId> <version>1-SNAPSHOT</version> </parent> <artifactId>simple-webapp</artifactId> <packaging>war</packaging> <name>Chapter 6 Simple Web Application Project</name> <dependencies> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_2.4_spec</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.sonatype.mavenbook.ch06</groupId> <artifactId>simple-weather</artifactId> <version>${project.version}</version> </dependency> </dependencies> <build> <finalName>simple-webapp</finalName> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> </plugin> </plugins> </build> </project>
由Maven Reactor负责解析模块依赖以编译:
$mvn clean install
多模块的依赖优化:
上移共同的依赖至dependencyManagement
如果多于一个项目依赖于一个特定的依赖,你可以在dependencyManagement中列出这个依赖。父POM包含一个版本和一组排除配置,所有的子POM需要使用groupId和artifactId引用这个依赖。如果依赖已经在dependencyManagement中列出,子项目可以忽略版本和排除配置。
为兄弟项目使用内置的项目version和groupId
使用{project.version}和${project.groupId}来引用兄弟项目。兄弟项目基本上一直共享同样的groupId,也基本上一直共享同样的发布版本。使用${project.version}可以帮你避免前面提到的兄弟版本不一致问题。
如原始父pom:
<project> ... <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.0.7</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.5</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>3.3.0.ga</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>3.3.0.ga</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.5.ga</version> <exclusions> <exclusion> <groupId>javax.transaction</groupId> <artifactId>jta</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> ... </project>
原始子pom:
<project> ... <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> </dependency> </dependencies> ... </project>
可以看出父pom与子pom在hibernate-annotations和hibernate-commons-annotations上有重复,优化后的父pom如下:
<project> <properties> <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version> </properties> ... <dependencyManagement> ... <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>${hibernate.annotations.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>${hibernate.annotations.version}</version> </dependency> ... </dependencyManagement> ... </project>