Maven2

首先当然去Apache网站下载Maven2了。下载地址:http://maven.apache.org/download.html 。我下载的版本是maven-2.0.4-bin.zip

然后我把maven-2.0.4-bin.zip解压到D:\maven-2.0.4
下面就是配置环境变量了,这和1.0的版本是一样的。(1)MAVEN_HOME:D:\maven-2.0.4 (2)在Path 中加入%MAVEN_HOME%\bin

这样配置就OK了。然后,我建立了如下目录:E:\app\mavenApp,当然这些可以随便建立。

生命周期的引入
在Maven2中有了明确的生命周期概念,而且都提供与之对应的命令,使得项目构建更加清晰明了。主要的生命周期阶段:

validate,验证工程是否正确,所有需要的资源是否可用。
compile,编译项目的源代码。
test-compile,编译项目测试代码。
test,使用已编译的测试代码,测试已编译的源代码。
package,已发布的格式,如jar,将已编译的源代码打包。
integration-test,在集成测试可以运行的环境中处理和发布包。
verify,运行任何检查,验证包是否有效且达到质量标准。
install,把包安装在本地的repository中,可以被其他工程作为依赖来使用
deploy,在整合或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。
generate-sources,产生应用需要的任何额外的源代码,如xdoclet。

新增Dependency Scope
在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前<scope>可以使用5个值:

compile,缺省值,适用于所有阶段,会随着项目一起发布。
provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
------------------------------------
需要在pom.xml文件中增加servlet容器的插件(嵌入式):
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.6</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

部署到tomcat:
tomcat配置有管理权限的用户:conf\tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager"/>
<user username="marshal" password="password" roles="manager"/>
</tomcat-users>
在pom文件的tomcat插件中添加:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<url>http://localhost:8080/manager</url>
<server>myserver</server>
<path>/mycontext</path>
</configuration>
</plugin>

在.m2/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>myserver</id>
<username>marshal</username>
<password>password</password>
</server>
</servers>
</settings>
运行打包部署,在maven项目目录下:
mvn tomcat:deploy
然后访问:http://localhost:8080/mycontext/ 即可。
撤销部署:
mvn tomcat:undeploy
启动web应用:
mvn tomcat:start
停止web应用:
mvn tomcat:stop
重新部署:
mvn tomcat:redeploy
部署展开的war文件:
mvn war:exploded tomcat:exploded
下面说些基本设置,打开D:\maven-2.0.4\conf\settings.xml
1. 改变<localRepository>E:/app/m2/repository</localRepository> 目的:改变本地的数据仓库目录(存放从远程下载的jar包),如果不改变也可以,如不改变,缺省目录为:C:\Documents and Settings\主机名\.m2\repository
2. 配置代理,主要目的是为了能从远程下载jar包,根据实际的网路情况进行配置吧。因为我现在单位是有代理服务器的,如果不配置,就不能从远程下载jar包了。我的配置如下(注意:如果你的网络没有代理服务器,就不用配置这个步骤)
      <proxy> 
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<host>192.168.17.100</host>
<port>8889</port>
<nonProxyHosts>192.168.*.*</nonProxyHosts>
</proxy>


下面就是一些goal命令了,这里和Maven1有不少不同,常用的命令有:
创建Maven的普通java项目:
mvn archetype:create -DgroupId=com.codeline.commons -DartifactId=pjoName     

创建Maven的Web项目:
mvn archetype:create -DgroupId=com.mycompany.app 
-DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp


普通java项目:
mvn archetype:generate
在交互界面中:
Choose a number: 回车即可,也就是选择15
Define value for groupId: 输入组织id,比如easymorse.com
Define value for artifactId:输入项目名称,比如helloworld
Define value for version: 输入版本号,可以直接回车,默认是1.0-SNAPSHOT
Define value for package: java的包名,比如com.easymorse
然后回车
web项目:
Choose a number: 回车即可,也就是选择18,这里和java普通项目不一样
Define value for groupId: 输入组织id,比如easymorse.com
Define value for artifactId:输入项目名称,比如helloworld
Define value for version: 输入版本号,可以直接回车,默认是1.0-SNAPSHOT
Define value for package: java的包名,比如com.easymorse
然后回车

编译源代码:
mvn compile (或者:mvn compiler:compile)

编译测试代码:
mvn test-compile

运行测试:
mvn test

产生site:
mvn site 

打包:
mvn package;mvn package -Dmaven.test.skip=true 

在本地Repository中安装
mvn install 

发布到本地私服
mvn deploy

清除产生的项目:
mvn clean  

生成eclipse项目:
mvn eclipse:eclipse

生成idea项目:
mvn idea:idea 

组合使用goal命令,如只打包不测试:
mvn -Dtest package

编译测试的内容:
mvn test-compile

只打jar包:
mvn jar:jar

只测试而不编译,也不测试编译:
mvn test -skipping compile -skipping test-compile

忽略失败的测试:
mvn test -Dmaven.failure.ignore=true
(这里要特别注意 -skipping 的灵活运用,当然也可以用于其他组合命令)
清除eclipse的一些系统设置:
mvn eclipse:clean

指定jetty端口:
mvn -Djetty.port=8087 jetty:run 

=========================================================
配置存储库
要求项目的每个开发者必须在conf目录中配置存储库是不方便的,所以Maven可以同时查看多个存储库并且将它们全部配置在pom.xml文件中。让我们看看一个例子,它展示了如何在应用程序用使用多个存储库。在以下从pom.xml文件摘录的片断中,我们设置了两个存储库来让Maven寻找依赖项。Ibiblio一直是默认的存储库,我们又添加了Planet Mirror作为后援存储库。我们也可以让团队使用的本地web服务器作为第二个存储库。
<repositories> 
<repository>
<id>Ibiblio</id>
<name>Ibiblio</name>
<url>http://www.ibiblio.org/maven/</url>
</repository>
<repository>
<id>PlanetMirror</id>
<name>Planet Mirror</name>
<url>http://public.planetmirror.com/pub/maven/</url>
</repository>
</repositories>

其他配置:
maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。
有以下分类:
env.X:操作系统环境变量,比如${env.PATH}
project.x:pom文件中的属性,比如:<project><version>1.0</version></project>,引用方式:${project.version}

settings.x:settings.xml文件中的属性,比如:<settings><offline>false</offline></settings>,引用方式:${settings.offline}

Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}
自定义:在pom文件中可以:<properties><installDir>c:/apps/cargo-installs</installDir></properties>,引用方式:${installDir}

==================================================
遇到的小问题解决。
* 当建立一个工程后,执行 mvn eclipse:eclipse ,建立一个maven的工程,用eclipse打开后,发现报如下错误:Unbound classpath variable: 'M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar' in project my-webapp ,这是因为在Eclipse 没有配置 M2_REPO 变量,配置步骤:window >>
preferences >> Java >> Build Path >> Classpath Variables 
新建一个 M2_REPO 的变量,变量值指向你系统的Maven2的数据仓库位置

------------------------------------------
* POM.XML 继承的写法
<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.apache.hivemind</groupId>
<artifactId>hivemind-examples</artifactId>
<packaging>jar</packaging>
<version>2.0-alpha-1</version>
<parent>
<groupId>org.apache.hivemind</groupId>
<artifactId>hivemind-project</artifactId>
<version>2.0-alpha-1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>HiveMind Examples</name>
<inceptionYear>2003</inceptionYear>
<dependencies>
<dependency>
<groupId>org.apache.hivemind</groupId>
<artifactId>hivemind-framework</artifactId>
<version>2.0-alpha-1</version>
</dependency>

* 如何安装Jar(或war)依赖到本地的数据仓库中?
下面具一个如何把dwr.jar包上传到本地数据仓库的例子,一看例子,大家就明了了。
- 下载dwr.jar包到本地,我把它放到了 C:/ (C盘根目录),

- 进入“命令行提示符”,进入到C:/,然后执行
mvn install:install-file -Dfile=dwr.jar -DgroupId=dwr.mocha -DartifactId=dwrDart -Dversion=1.0 -Dpackaging=jar 
这样就把dwr传到了本地仓库中(我的本地仓库地址为:E:\app\m2\repository\),上传到仓库中的jar包名为dwrDart-1.0.jar,目录结构为dwr/mocha/dwrDart/1.0/dwrDart-1.0.jar
[code="java"]mvn package -Dmaven.test.skip=true
打包命令:web根目录. jar -cvf 包名.war *
java -jar *.jar[/code]
[color=olive]补充:settings.xml的配置
settings.xml对于maven来说相当于全局性的配置,用于所有的项目。在maven2中存在两个 settings.xml,一个位于maven2的安装目录conf下面,作为全局性配置。对于团队设置,保持一致的定义是关键,所以 maven2/conf下面的settings.xml就作为团队共同的配置文件。保证所有的团队成员都拥有相同的配置。当然对于每个成员,都需要特殊的 自定义设置,如用户信息,所以另外一个settings.xml就作为本地配置。默认的位置为:${user.dir} /.m2/settings.xml目录中(${user.dir} 指windows 中的用户目录)。
settings的结构
<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">
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
localRepository:表示本地库的保存位置,也就是maven2主要的jar保存位置,默认在${user.dir}/.m2/repository,如果需要另外设置,就换成其他的路径。
offline:如果不想每次编译,都去查找远程中心库,那就设置为true。当然前提是你已经下载了必须的依赖包。
Servers:在POM中的 distributionManagement元素定义了开发库。然而,特定的username和pwd不能使用于pom.xml,所以通过此配置来保存server信息
id:server 的id,用于匹配distributionManagement库id,比较重要。
username, password:用于登陆此服务器的用户名和密码
privateKey, passphrase:设置private key,以及passphrase
filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775
Mirrors:表示镜像库,指定库的镜像,用于增加其他库
id,name:唯一的标志,用于区别镜像
url:镜像的url
mirrorOf:此镜像指向的服务id
Proxies:此设置,主要用于无法直接访问中心的库用户配置。
id:代理的标志
active:是否激活代理
protocol, host, port:protocol://host:port 代理
username, password:用户名和密码
nonProxyHosts: 不需要代理的host
Profiles:类似于pom.xml中的profile元素,主要包括activation,repositories,pluginRepositories 和properties元素刚开始接触的时候,可能会比较迷惑,其实这是maven2中比较强大的功能。从字面上来说,就是个性配置。
单独定义profile后,并不会生效,需要通过满足条件来激活。
repositories 和pluginRepositories定义其他开发库和插件开发库。对于团队来说,肯定有自己的开发库。可以通过此配置来定义。
如下的配置,定义了本地开发库,用于release 发布。
releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)
properties:maven 的properties作为placeholder值,如ant的properties。
包括以下的5种类型值:
1. env.X,返回当前的环境变量
2. project.x:返回pom中定义的元素值,如project.version
3. settings.x:返回settings.xml中定义的元素
4. java 系统属性:所有经过java.lang.System.getProperties()返回的值
5. x:用户自己设定的值
Activation:用于激活此profile
jdk:如果匹配指定的jdk版本,将会激活
os:操作系统
property:如果maven能检测到相应的属性
file: 用于判断文件是否存在或者不存在
除了使用activation来激活profile,同样可以通过activeProfiles来激活
Active Profiles:
表示激活的profile,通过profile id来指定。

常用的 maven 仓库
http://repo2.maven.org/maven2
http://download.java.net/maven/2/


一些常用的命令补充
[http://macrochen.iteye.com/blog/531437]
mvn help:effective-pom
用来查看当前工程的完整的pom文件, 比如从父类pom以及默认pom继承的内容
mvn install
将当前的maven构建(项目打包后的文件)安装到本地仓库
mvn install -Dmaven.test.skip=true
跳过测试(同时会跳过test compile)
mvn deploy
将当前的maven构建(项目打包后的文件)安装到远程仓库
mvn archetype:create
这里的archetype是插件, create是目标(goal)
profile命令:
mvn install -DskipTests=true -Penv_test
生命周期
resource->compile->process-classes->process-test-resources->test-compile->test->prepare-package->package
resources:resources 绑定在resource处理阶段, 用来将src/main/resources下或者任何指定其他目录下的文件copy到输出目录中
resources:testResources 将test下的resources目录或者任何指定其他目录copy到test输出目录下
compiler:testCompile 将测试类编译(包括copy资源文件)
surefire:test 运行测试用例
jar:jar 打jar包
mvn help:describe -Dplugin=exec -Dfull
查看插件的doc文档
查看依赖
mvn dependency:resolve
查看项目依赖情况
mvn dependency:tree
打印出项目的整个依赖树
mvn dependency:analyze
帮助你分析依赖关系, 用来取出无用, 重复依赖的好帮手
mvn install -X
追踪依赖的完整轨迹

测试相关
默认情况下, 遇到测试失败, surefire:test会中断测试, 如果想忽略测试失败, 继续运行后面的测试, 需要这么配置:
Xml代码
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>


从doc文档中看到:Expression: ${maven.test.failure.ignore}
因此也可以从命令行中通过传递参数来实现同样的功能
mvn test -Dmaven.test.failure.ignore=true

mvn install -Dmaven.test.skip=true
跳过单元测试

要跳过测试, 在pom文件也可以这样配置:
Xml代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>


打包输出
Assembly是一个关于打包输出的插件, 比如这样配置:
Xml代码
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
运行命令
mvn assembly:assembly
生成xxxx-1.0-jar-with-dependencies.jar的打包文件, 里面将包含所有的依赖文件

生成项目
mvn archetype:create
用来生成项目, artifactId和groupId用来指定目标, archetypeArtifactId通过制定类型, 比如 maven-archetype-webapp用该创建一个web项目

在maven中使用jetty容器, 我们需要这样配置一个插件:
Xml代码
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>

mvn jetty:run
用来运行jetty服务器

build配置和dependencies都会被所有的子模块继承

依赖管理
为了去掉依赖重复, 在多模块pom中将加入所有重复的依赖:
Xml代码
<dependencyManagement>
<dependencies>

为了去掉版本号这样的重复, 可以这样写:
Xml代码
<properties>
<hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
</properties>
这样用:
Xml代码
<version>${hibernate.annotations.version}</version>


属性引用
对project属性的引用, 比如这样写:
org.sonatype.mavenbook-${project.artifactId}

直接定义属性:
比如这个属性${foo}=bar, 定义如下:
Xml代码
<properties>
<foo>bar</foo>
</properties>


可选依赖的配置
Xml代码
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>1.4.1</version>
<optional>true</optional>
</dependency>

这里举的是关于两种缓存方案的依赖配置, 在编译的时候需要, 但是运行的时候可能不需要.


依赖范围
compile, test是常用的
需要接的是运行时依赖, 表示在编译时不需要, 在运行时需要, 例子是driver编译的时候是一个api接口依赖, 运行时就需要一个具体的driver class实例了.

感觉只要理解compile和test依赖范围即可, a对b是test依赖, b对c是compile依赖, 那么a对c是test依赖, compile对所有直接依赖下的compile范围都是compile传递依赖, 很绕:(

一个对传递性依赖的排除
Xml代码
<dependency>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>project-a</artifactId>
<version>1.0</version>
<exclusions>
<exclusion>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>project-b</artifactId>
</exclusion>
</exclusions>
</dependency>
表示虽然依赖project-a, 但是不想依赖a所依赖的b
这里是排除一个依赖, 添加另外一个依赖
Xml代码
<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>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jta_1.1_spec</artifactId>
<version>1.1</version>
</dependency>

使用排除依赖, 一般是一个传递依赖在编译的时候需要, 但是在实际运行环境不需要的时候

dependencyManagement用来在父pom文件中定义公共的依赖, 以及版本号, 是一个集中管理依赖版本的地方

项目关系
groupId用.分隔, artifactId用-分隔

在继承模块中, maven约定父pom在本地仓库, 或者在当前项目的父目录../pom.xml中可用.可以通过relativePath指定具体的父pom.xml的位置, 一般在多模块中与子模块同级存放父pom.xml, 这时就需要通过relativePath来指定pom.xml了.

从父模块继承要加这样的标签:
Xml代码
<parent>
<groupId>com.training.killerapp</groupId>
<artifactId>a-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>


将共同的依赖使用一个pom项目统一组织起来, 并让其他的模块添加对该模块的依赖是一个不错的复用机制.
此时使用的标记是dependencies而不是dependencyManagment了.比如这样写:
Xml代码
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>persistence-deps</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
</dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlVersion}</version>
</dependency>
</dependencies>
<properties>
<mysqlVersion>(5.1,)</mysqlVersion>
<properties>
对该pom的依赖需要这样写:
Xml代码
<dependency>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>persistence-deps</artifactId>
<version>1.0</version>
<type>pom</type>
</dependency>

多了一个type标记

一个简单的多模块+继承应用
server-side包含web-apps和server-lib两个子模块, 并且是二者的父模块, web-apps依赖server-lib模块(里面定义了所有的web-app所要依赖的jar包), web-apps 是个多模块, web-client, web-admin是他的两个子模块, 同时具有父子关系. 这样设计的结果将导致web-client和web-admin的定义将非常小巧, 简洁
POM继承十分有用,但它可能被滥用。当然在需要共享依赖和配置的时候,父子关联需要被使用。但当两个项目截然不同的时候使用父子关联是不明智的。

生命周期
在执行clean之前的pre-clean中指定需要执行的插件目标, 这个配置没看懂:(

由于生命周期阶段会绑定对应的插件, 因此可以对对应的插件进行定制, 达到定制声明周期的目的
比如对clean插件的定制:
Xml代码
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>target-other</directory>
<includes>
<include>*.class</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>


资源处理周期
resources:resources会将src/main/resources下的文件内容复制到target/classes中. 同时还可以在copy之前做一些过滤替换工作.
关于过滤替换, 如果有这样一个文件:
Xml代码
<service>
<!-- This URL was set by project version 0.6-SNAPSHOT -->
<url>${jdbc.url}</url>
<user>${jdbc.username}</user>
<password>${jdbc.password}</password>
</service>
属性配置文件如下:
jdbc.url=jdbc:hsqldb:mem:mydb
jdbc.username=sa
jdbc.password=
过滤默认是不开启的, 需要配置一下:
Xml代码
<build>
<filters>
<filter>src/main/filters/default.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

src/main/resources是默认的资源文件目录, 但是也可以指定其他目录:
Xml代码
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/xml</directory>
</resource>
<resource>
<directory>src/main/images</directory>
</resource>
</resources>

对不同的资源进行分门别类的处理, 这样方便我们对不同的资源采用不同的过滤处理, 比如这样写:
Xml代码
<resource>
<filtering>true</filtering>
<directory>/src/main/command</directory>
<includes>
<include>run.bat</include>
<include>run.sh</include>
</includes>
<targetPath>/</targetPath>

会将指定目录下的run.bat, sun.sh文件复制到指定的目录(targetPath)中

编译周期
maven默认的编译(compile:compile)版本是1.3, 运行平台版本是1.1, 现在一般需要配置成1.5以上版本
Xml代码
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

上面是指定插件的版本, 如果要制定目标的版本, 需要将configuration元素放到execution元素下.
如果你想要存储项目的源码至src/java而非src/main/java,让构建输出至classes而非target/classes,你可以覆盖定义在超级POM中的sourceDirectory的默认值(不推荐这样做, 我们始终应该遵循"约定优于配置"的原则)。
Xml代码
<build>
...
<sourceDirectory>src/java</sourceDirectory>
<outputDirectory>classes</outputDirectory>
...
lt;/build>


Test周期
绑定的surefire:test, 默认情况下, 如果测试失败, 停止构建, 如果希望继续构建, 需要这样设置:
Xml代码
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>


profile
通过profile可以针对特定的环境来定制不同的artifact, 比如这样写来覆盖compile插件的默认配置:
Xml代码
<profiles>#
<profile>
<id>production</id>#
<build>#
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>false</debug>#
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

使用profile的命令行用法:
mvn clean install -Pproduction -X
可以根据某种条件激活对应 profile, 比如根据jdk版本包含指定的模块:
Xml代码
<profile>
<id>jdk16</id>
<activation>
<jdk>1.6</jdk>
</activation>
<modules>
<module>simple-script</module>
</modules>
</profile>

activation元素列出了所有激活profile需要的条件, 比如下面的配置:
Xml代码
<activation>
<activeByDefault>false</activeByDefault>#
<jdk>1.5</jdk>#
<os>
<name>Windows XP</name>#
<family>Windows</family>
<arch>x86</arch>
<version>5.1.2600</version>
</os>
<property>
<name>mavenVersion</name>#
<value>2.0.5</value>
</property>
<file>
<exists>file2.properties</exists>#
<missing>file1.properties</missing>
</file>
</activation>

通过属性来激活:
Xml代码
<activation>
<property>
<name>!environment.type</name>
</property>
</activation>

可以将profile从pom.xml中拆分出来, 单独的放在profiles.xml文件中
除了可以在pom, pom外部设置profile外, 还可以在setting中设置profile, 可以将一些私密的信息设置在自己的settging.xml中
通过profile指定属性, 比如有一个envClassifier属性:
Xml代码
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classifier>${envClassifier}</classifier>
</configuration>
</plugin>
</plugins>
</build>

那么可以有这样的profile来指定一个envClassifier:
Xml代码
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<envClassifier>win</envClassifier>
</properties>
</profile>


Assembly
简单的说就是打包方式, 比如jar, ear, war都是不同的Assembly, 比如希望打包的文件中带有源码, api文档等都需要定制Assembly.
一些Assembly描述符(指定打包包含内容)
bin:最小打包文件
jar-with-dependencies:包含所有依赖
project:包含所有的项目信息, 可以直接构建maven项目, 因此里面会包含pom.xml文件, 如果是个eclipse工程, 还会包含eclipse工程配置文件
src:包含源码
将项目打包一份儿, 发给其他人的命令:
mvn -DdescriptorId=project assembly:single
配置一个将一个工程打包成一个可运行的jar文件(跟package阶段绑定):
Xml代码
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<executions>
<execution>
<id>create-executable-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.sonatype.mavenbook.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>


写在顶层pom中pluginManagement(dependencyManagement)标签下的配置只是定义, 并不会被使用.子模块中需要显式的激活, 比如这样写:
Xml代码
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-2</version>
<executions>
<execution>
<id>create-project-bundle</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>project</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>

子模块的激活配置:
Xml代码
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>


maven插件
一个插件包含一个描述符和多个mojo, 可以将mojo理解为目标, 比如compile:compile对应的是compile插件的CompileMojo类
一个描述符用来告诉maven, 一个插件的各种配置他在jar文件中的META-INFO/maven/plugin.xml, 当maven加载一个插件的时候, 它首选读取该描述文件, 然后去找Mojo以及资源等信息.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值