用于测试和main的不同maven编译器版本

问题描述 投票:0回答:3

如何配置 maven 编译器以使用 java 5 作为我的测试代码,使用 java 1.4 作为我的主代码?

java maven
3个回答
34
投票

如果要设置兼容相关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>

22
投票

我没有运气使用

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>

1
投票

我至少找到了适合我的情况的解决方案。

我正在编写一个需要与 Java 1.7 兼容的库,但依赖于 JDK 8 中的

nashorn
引擎来测试其一些功能,这是为
main
test 使用不同编译器/jdk 版本的完美原因
.

当谈论为主和测试使用不同的编译器/JRE版本时,说

A
的版本
main
B
的版本
test
A
严格小于
B
,我们真的想实现以下:

  1. 在开发过程中让 IDE(在我的例子中为
    Eclipse
    )使用 JDK 版本
    B
    ,以免引入编译错误(项目图标上的红叉)。
  2. 使 JUnit 测试可在 IDE 中运行。
  3. 使 JUnit 测试可通过发布所需的
    mvn clean test
    从命令行运行。
  4. 使用 JRE
    main
    A
    中编译源代码,这可以自动检测使用 JDK
    B
    中的新功能。
  5. 在测试用例代码中免费使用 JDK
    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
可用于在运行时覆盖参数设置。

© www.soinside.com 2019 - 2024. All rights reserved.