构建部分(<build>
)包含一些插件及其配置信息,需要靠它们来执行Maven构建周期目标。对于大多数项目来说,这一部分通常都相当短,因为一般用默认插件的默认设置就够了。但对于java7developer项目而言,<build>
部分包含了几个覆盖了默认设置的插件。我们之所以这样做,是为了让java7developer项目可以:
- 构建Java 7代码;
- 构建Scala和Groovy代码;
- 运行Java、Scala和Groovy测试;
- 提供Checkstyle和FindBugs代码指标报告。
如果你的构建中还有更多需要配置的地方,可以在http://maven.apache.org/plugins/index.html找到完整的插件列表。
代码清单E-1是java7developer项目的构建配置。
代码清单E-1 POM:构建信息
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> //❶指明要用的插件
<configuration>
<source>1.7</source>
<target>1.7</target> //❷编译Java 7代码
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<fork>true</fork> //❸设置编译器选项
<executable>${jdk.javac.fullpath}</executable> //❹ 设置javac的路径
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.14.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal> //❺强制Scala编译
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.9.0</scalaVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<dependencies>
<dependency>
<groupId>org.codehaus.gmaven.runtime</groupId>
<artifactId>gmaven-runtime-1.7</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<configuration>
<providerSelection>1.7</providerSelection>
</configuration>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude> ﹃❻排除测试
com/java7developer/chapter11/listing_11_2 TicketRevenueTest.java</exclude>
<exclude>com/java7developer/chapter11/listing_11_7 TicketTest.java
</exclude> ﹄❻排除测试
...
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.6</version>
<configuration>
<includeTestSourceDirectory>
true
</includeTestSourceDirectory> //在测试上运行Checkstyle
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<findbugsXmlOutput>true</findbugsXmlOutput> //生成FindBugs报告
<findbugsXmlWithMessages>true</findbugsXmlWithMessages>
<xmlOutput>true</xmlOutput>
</configuration>
</plugin>
</build>
因为你要将编译Java 1.5代码的默认行为变为编译Java 1.7❷,所以需要指明正在使用(特定版本)的Compiler(编译器)插件❶。
因为已经打破了惯例,所以最好加上几个其他的编译器警告选项❸。还可以指明Java 7装在哪里❹。要想让Maven得到javac
的位置,只要将与操作系统对应的sample_build.properties另存为build.properties,并修改属性jdk.javac.fullpath
的值即可。
为了使用Scala插件,需要确保compile
和testCompile
目标运行时Scala插件能够执行❺1。用Surefire插件可以对测试进行配置。在这个项目的配置中,排除了几个故意失败的测试⑥(你会记起来自第11章的两个TDD测试)。
1 希望这个插件的后续版本能自动挂到这些目标上。
我们已经讨论过构建部分了,现在让我们转入POM中的另一个关键部分,依赖管理。