構建部分(<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插件可以對測試進行配置。在這個項目的配置中,排除了幾個故意失敗的測試6(你會記起來自第11章的兩個TDD測試)。
1 希望這個插件的後續版本能自動掛到這些目標上。
我們已經討論過構建部分了,現在讓我們轉入POM中的另一個關鍵部分,依賴管理。