如何配置maven編譯器以使用java 5作爲我的測試代碼和java 1.4用於我的主代碼?針對測試和主要的不同maven編譯器版本
22
A
回答
24
如果您要設置符合相關Java版本,您可以爲每次執行配置編譯器插件。假設Maven至少使用JDK作爲您指定的最高版本的最新版本。通過使用屬性,如果需要,你可以覆蓋命令行或在孩子該配置:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compileSource}</source>
<target>${compileSource}</target>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>${testCompileSource}</source>
<target>${testCompileSource}</target>
</configuration>
</execution>
</executions>
</plugin>
...
<properties>
<compileSource>1.4</compileSource>
<testCompileSource>1.5</testCompileSource>
</properties>
如果你的意思是使用不同的編譯器,這是更復雜一點。因爲您需要指定JDK的路徑和您正在使用的編譯器版本。這些也可以在屬性中進行定義。雖然你可能想在你的settings.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${compileSource}</source>
<target>${compileSource}</target>
<executable>${compileJdkPath}/bin/javac</executable>
<compilerVersion>${compileSource}</compilerVersion>
</configuration>
<executions>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>${testCompileSource}</source>
<target>${testCompileSource}</target>
<executable>${testCompileJdkPath}/bin/javac</executable>
<compilerVersion>${testCompileSource}</compilerVersion>
</configuration>
</execution>
</executions>
</plugin>
...
<properties>
<compileSource>1.4</compileSource>
<testCompileSource>1.5</testCompileSource>
<compileJdkPath>path/to/jdk</compileJdkPath>
<testCompileJdkPath>path/to/test/jdk<testCompileJdkPath>
</properties>
注來定義他們也許更合理的配置文件來定義編譯器配置,一種是支持的每個JDK,讓你的普通版本不依賴特性被設置。
此外,在Maven的3.x中,你需要包括fork
參數中指定的可執行文件時,如:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<fork>true</fork>
<executable>${testCompileJdkPath}/bin/javac</executable>
<source>1.8</source>
<target>1.8</target>
</configuration>
</execution>
</executions>
</plugin>
9
我有接受的答案編譯Java源7和Java 8沒有運氣測試源使用maven-compiler-plugin
,版本3.5.1。因爲編譯插件對源和測試源都使用source/target參數。
但我發現,測試源和目標有單獨的配置參數。
所以對我來說這是工作
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
</plugins>
</build>
當我嘗試用源和目標版本`1.8`用於測試和`1.7`主代碼的第一個例子,編譯成功的時候`MVN compile`解決方案與`mvn test-compile`分開運行,但是在`mvn compile test-compile`或者更簡單的說,`mvn test-compile`(其中`compile`被拉入)主要配置中運行時爲'`似乎優先,並且失敗。 – 2015-09-14 18:47:27