用于测试和主要的不同 Maven 编译器版本

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

How can I configure the maven compiler to use java 5 for my test code and java 1.4 for my main code?

推荐答案

如果要设置符合相关Java版本,可以为每次执行配置编译器插件.假设 Maven 使用的 JDK 至少与您指定的最高版本一样最新.通过使用属性,您可以在命令行上覆盖该配置,如果需要,可以在子级中覆盖该配置:

If you want to set compliance to the relevant Java version, you can configure the compiler plugin for each execution. Assuming Maven is using a JDK at least as current as the highest version you specify. By using properties you can override that configuration on the commandline or in a child if needed:

<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 中定义它们

If you mean using different compilers, that's a bit more involved. as you need to specify the path to the JDK and what compiler version you're using. Again these can be defined in properties. Though you may want to define them in your 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 配置一个配置文件,这样您的正常构建就不会依赖于设置的属性.

Note it might make sense to define the compiler configurations in profiles, one for each JDK you support, so that your normal builds don't rely on properties being set.

另外,在 Maven 3.x 中,您需要在指定可执行文件时包含 fork 参数, 例如:

Also, in Maven 3.x, you need to include the fork parameter when specifying the executable, e.g.:

  <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>

相关文章