如何配置 maven 编译器以使用 java 5 作为我的测试代码,使用 java 1.4 作为我的主代码?
如果要设置兼容相关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>
我没有运气使用
maven-compiler-plugin
版本 3.5.1 编译 java 7 源代码和 java 8 测试源代码。因为编译插件对主源和测试源都使用了源/目标参数。
但是我发现,测试源和目标有单独的配置参数。
所以对我来说有效的解决方案是
<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>
我至少找到了适合我的情况的解决方案。
我正在编写一个需要与 Java 1.7 兼容的库,但依赖于 JDK 8 中的
nashorn
引擎来测试其一些功能,这是为 main
和 test
使用不同编译器/jdk 版本的完美原因
.
当谈论为主和测试使用不同的编译器/JRE版本时,说
A
的版本main
和B
的版本test
和A
严格小于B
,我们真的想实现以下:
Eclipse
)使用 JDK 版本 B
,以免引入编译错误(项目图标上的红叉)。mvn clean test
从命令行运行。 main
在 A
中编译源代码,这可以自动检测使用 JDK B
中的新功能。B
的新功能/新 API。以下 pom 声明有效(例如 let
A = 1.7
和 B = 1.8
):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<fork>true</fork>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArguments>
<source>1.7</source>
<target>1.7</target>
</compilerArguments>
</configuration>
</execution>
<execution>
<id>default-testCompile</id>
<configuration>
<compilerArguments>
<source>1.8</source>
<target>1.8</target>
</compilerArguments>
</configuration>
</execution>
</executions>
</plugin>
首先,
<source>1.8</source> <target>1.8</target>
是让 Eclipse 满意并在项目本身中使用 1.8 的必要条件,正如其他人指出的那样,Eclipse 有一个尚未解决的 bug,并且本身不支持单个项目中的不同 JDK 版本。
其次,在源编译和测试源编译期间使用
<execution>
覆盖 source
和 target
参数。 default-compile
和 default-testCompile
是编译器插件的特殊名称。
第三,一定要使用
compilerArguments
,而不是compilerArgs
。据我所知,只有 compilerArguments
可用于在运行时覆盖参数设置。