1. Maven2如何安装
下载软件包, 解压缩到目录xx,然后设置环境变量 M2_HOME 为xx,最后设置一下 PATH 路径为 $M2_HOME/bin , 即可
2. Maven2的配置文件
直接改 $M2_HOME/conf/settings.xml 或者改 $USER_HOME/.m2/settings.xml (推 荐)
3.Maven2的阶段
Maven Phases
Although hardly a comprehensive list, these are the most common default lifecycle phases executed.
validate : validate the project is correct and all necessary information is available
compile : compile the source code of the project
test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
package : take the compiled code and package it in its distributable format, such as a JAR.
integration-test : process and deploy the package if necessary into an environment where integration tests can be run
verify : run any checks to verify the package is valid and meets quality criteria
install : install the package into the local repository, for use as a dependency in other projects locally
deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
There are two other Maven lifecycles of note beyond the default list above. They are
clean : cleans up artifacts created by prior builds
site : generates site documentation for this project
4.将工程部署到远程仓库
先在 pom.xml 中配置仓库的URL地址
<distributionManagement>
<repository>
<id>mycompany-repository</id>
<name>MyCompany Repository</name>
<url>scp://repository.mycompany.com/repository/maven2</url>
</repository>
</distributionManagement>
然后再去 settings.xml 中配置验证信息
<settings 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/xsd/settings-1.0.0.xsd">
...
<servers>
<server>
<id>mycompany-repository</id>
<username>jvanzyl</username>
<!-- Default value is ~/.ssh/id_dsa -->
<privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)
<passphrase>my_key_passphrase</passphrase>
</server>
</servers>
...
</settings>
5.复合工程
父工程中设置如下
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>app</artifactId>
<packaging>pom </packaging>
<modules>
<module>my-app</module>
<module>my-webapp</module>
</modules>
</project>
子工程中设置如下
<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/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
...
6.LifeCycle,Phases和Goals的关系
内建有三个LifeCycle,分别是 default , clean 和 site
LifeCycle由Phases组成 , 如default生命周期由以下phases组成:
validate, compile, test, package, integration-test, verify, install, deploy
而, Phase则由Goals组成 , Goal绑定到零个或者多个Phase, 如果绑定了,则在此Phase执行的时候,自动执行,否则需要单独调用。
7.为工程设置生命周期
1 . 利用<packaging>标签设置基本的生命周期, 值可以是jar(默认), war, ear和pom.
如,jar的phases和goals对应如下
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | jar:jar |
install | install:install |
deploy | deploy:deploy |
2 .如何将goals添加到phases?
通过在pom.xml中的build标签下的plugins标签的子标签plugin中配置,进行绑定。
<plugin>
<groupId>com.mycompany.example</groupId>
<artifactId>maven-touch-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-test-resources</phase> //如果不指定,则进行默认的绑定
<goals>
<goal>timestamp</goal>
</goals>
</execution>
</executions>
</plugin>
1.什么是POM
POM,即Project Object Model, 通过pom.xml文件配置Maven2,然后Maven2根据此配置执行。
2.Super POM
Super POM是Maven2的默认pom.xml文件, 无需显示声明, 所有的pom.xml文件都默认继承此文件。
2.0.x的默认Super POM文件如下:
<project>
<modelVersion>4.0.0</modelVersion>
<name>Maven Default Project</name>
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.0</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.3</version>
</plugin>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.3.1</version>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-plugin-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<plugin>
<artifactId>maven-rar-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0-beta-7</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>2.0-beta-7</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.0.4</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-alpha-1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
<profiles>
<profile>
<id>release-profile</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<updateReleaseInfo>true</updateReleaseInfo>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
3.Minimal POM
最小的POM文件应该具有以下几个元素
4.Project Inheritance ( 注意,是继承而不是组合 )
POM中的以下元素会被合并:
* dependencies
* developers and contributors
* plugin lists (including reports)
* plugin executions with matching ids
* plugin configuration
* resources
示例:
设 pom.xml 文件为
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
1.标准目录结构下的继承
.
|-- my-module
| `-- pom.xml
`-- pom.xml
my-module下的pom.xml文件为
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId> //可以省略,省略则继承parent pom相同的groupId
<artifactId>my-module</artifactId>
<version>1</version>
</project>
2.非标准目录结构下的继承
.
|-- my-module
| `-- pom.xml
`-- parent
`-- pom.xml
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<relativePath>../parent/pom.xml </relativePath> //注意这里的区别
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-module</artifactId>
</project>
5. Project Aggresgation
改用组合,需要做以下工作
即,通过在parent pom文件中添加以下元素,即可
<modules>
<module>relativePath/To/Module</module> //如果是标准目录结构,则直接指定子模块文件夹名
...
</modules>
6.Aggregation和Inheritance的区别
当构建一个工程时,需要执行几个子工程,那么就可以使用aggregation
当需要抽取多个工程中的相同配置的话,那么可以使用inheritance
实际使用中,往往两者相结合使用,方法如下:
Profiles specified in the POM can modify the following POM elements:
mvn help:active-profiles
谢原作者:
http://hi.baidu.com/dvdface/blog/item/26c453fb787216106c22eb5d.html