一、build设置
主要用于编译设置,一般包含两种标签:resource和plugins,前者用于排除或包含某些资源文件,后者用于设置插件。
1、例如:在用junit4写测试程序,用到了@Test 等注解,但是由于maven的核心插件之一compiler插件默认只支持编译java1.3,因此需要配置该插件使其支持java6,需要修改pom文件如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
2、配置单元测试
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>com/taobao/media/**/*Test.java</include>
</includes>
<excludes>
<exclude>com/taobao/media/test/**/*Test.java</exclude>
</excludes>
<printSummary>true</printSummary>
</configuration>
</plugin>
includes 里面配置了哪些类是单元测试需要的类,excludes 里面配置了哪些类是非单元测试类。
3、覆盖率统计
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-clover2-plugin</artifactId>
<version>1.0.0.nl-SNAPSHOT</version>
<configuration>
<contextFilters>catch,static</contextFilters>
<excludes>
<exclude>com/taobao/media/web/**/*.java</exclude>
</excludes>
<includesTestSourceRoots>false</includesTestSourceRoots>
<generateHistorical>true</generateHistorical>
<encoding>GBK</encoding>
</configuration>
</plugin>
综上配置,当我们需要跑单元测试回归时,只需要:mvn -U clean test -Dmaven.test.failure.ignore=true 即可
其中-Dmaven.test.failure.ignore=true 是指当maven遇到一个测试失败,它默认的行为是停止当前构建,如果你希望继续构建项目,就需要加上这个参数。还可以在plugin的配置里添加这项:
<testFailureIgnore>true</testFailureIgnore>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>com/taobao/media/**/*Test.java</include>
</includes>
<excludes>
<exclude>com/taobao/media/test/**/*Test.java</exclude>
</excludes>
<printSummary>true</printSummary>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
二、profile
如果我即想跑单元测试(开发编写的在开发工程中),又想跑接口测试(由测试编写在测试工程中) 要怎么配置呢?
这时候呢就可以用到profile了。
profile能让你为一个特殊的环境自定义一个特殊的构建,profile使得不同环境构建的可移植性成为可能。
maven中的profile是一组可选的配置,可以用来设置或者覆盖配置默认值,有了profile,你就可以为不同的环境定制构建,profile可以在pom.xml中配置,并给定一个id,然后你就可以在运行maven的时候使用的命令行标记告诉maven运行特定profile中的目标。
例如:我在build中配置了单元测试,那么我在profile中再配置接口测试,那么我运行接口测试时,只需要在命令行里加 -P profileId 即可。
下面例子,我运行接口测试用例时:mvn -U test -P temp 即可。
<profiles>
<profile>
<id>tmp</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xms256m -Xmx1024m -XX:MaxPermSize=256m</argLine>
<includes>
<include>com/taobao/media/test/**/*Test.java</include>
</includes>
<excludes>
<exclude>com/taobao/media/test/task/*Test.java</exclude>
<exclude>com/taobao/media/manager/test/Test*.java</exclude><!-- Test* 是依赖环境的测试代码 -->
<exclude>com/taobao/media/test/dev/**/*Test.java</exclude>
</excludes>
<systemProperties>
<property>
<name>locations</name>
<value>${locations}</value>
</property>
</systemProperties>
<printSummary>true</printSummary>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
1、pom.xml中的profiles元素,它包括了一个或者多个profile元素,由于profile覆盖了pom.xml中的默认设置,profiles通常是pom.xm中的最后一个元素。
2、每个profile必须要有一个id元素,这个id元素包含的名字将在命令行调用profile时被用到,我们可以通过传给maven的一个-P<prifile_id>参数来调用profile。
3、一个profile元素可以包含很多其它元素,只要这些元素可以出现在pom.xml文档的project元素下面。