不同的maven编译器版本的测试和主 [英] Different maven compiler versions for test and main

查看:260
本文介绍了不同的maven编译器版本的测试和主的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何配置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>
< / goal>
< configuration>
< source> $ {testCompourceSource}< / source>
< target> $ {testCompourceSource}< / target>
< / configuration>
< / execution>
< / executions>
< / plugin>
...
< properties>
< compileSource> 1.4< / compileSource>
< testCompileSource> 1.5< / testCompSource>
< / 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>
< / goal>
< configuration>
< source> $ {testCompourceSource}< / source>
< target> $ {testCompourceSource}< / target>
< executable> $ {testCompileJdkPath} / bin / javac< / executable>
< compilerVersion> $ {testCompileSource}< / compilerVersion>
< / configuration>
< / execution>
< / executions>
< / plugin>
...
< properties>
< compileSource> 1.4< / compileSource>
< testCompileSource> 1.5< / testCompSource>
< compileJdkPath> path / to / jdk< / compileJdkPath>
< testCompileJdkPath> path / to / test / jdk< testCompileJdkPath>
< / properties>请注意,在配置文件中定义编译器配置可能有帮助,因为每个JDK都支持一个JDK,因此,



此外,在Maven 3.x中,您需要包含

 < plugin> 
< artifactId> maven-compiler-plugin< / artifactId>
< version> 3.1< / version>
< executions>
< execution>
< id> default-testCompile< / id>
< phase> test-compile< / phase>
< goals>
< goal> testCompile< / goal>
< / goal>
< configuration>
< fork> true< / fork>
< executable> $ {testCompileJdkPath} / bin / javac< / executable>
< source> 1.8< / source>
< target> 1.8< / target>
< / configuration>
< / execution>
< / executions>
< / plugin>


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

解决方案

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>

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>

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.

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>

这篇关于不同的maven编译器版本的测试和主的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆