针对测试和主要的不同maven编译器版本
答
如果您要设置符合相关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>
答
我有接受的答案编译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`被拉入)主要配置中运行时为' 1.7 `似乎优先,并且失败。 –
2015-09-14 18:47:27