문제

테스트 코드에는 Java 5를 사용하고 기본 코드의 경우 Java 1.4를 사용하도록 Maven 컴파일러를 구성하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

관련 Java 버전을 준수하려면 각 실행에 대한 컴파일러 플러그인을 구성 할 수 있습니다. Maven이 지정한 가장 높은 버전으로 JDK를 최소한으로 사용하고 있다고 가정합니다. 속성을 사용하면 CommandLine 또는 필요한 경우 자식의 구성을 재정의 할 수 있습니다.

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

다른 팁

허용 된 답변을 사용하여 Java 7 소스와 Java 8 테스트 소스를 사용하여 운이 없었습니다. maven-compiler-plugin, 버전 3.5.1. 컴파일 플러그인은 메인 및 테스트 소스 모두에 소스 / 대상 매개 변수를 사용했기 때문입니다.

그러나 테스트 소스 및 대상에 대한 별도의 구성 매개 변수가 있음을 알았습니다.

그래서 저에게는 효과가있는 솔루션이었습니다

 <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