質問

テストコードにjava 5を、メインコードにjava 1.4を使用するように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ごとに1つです。

また、 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テストソースをコンパイルする受け入れられた回答に運がありませんでした。コンパイルプラグインは、メインソースとテストソースの両方に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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top