개요

특정 프로젝트는 특정 버전의 JDK 로 빌드해야 하는 경우가 있다. JDK 는 이를 위해 -source와 -target 옵션으로 source 와 class 파일을 특정 JDK 의 버전에 맞출수 있다.

maven compiler plugin 을 사용하면 용도에 맞게 JDK 및 compile 옵션을 설정할 수 있다.

Goals

The Compiler Plugin has two goals. Both are already bound to their proper phases within the Maven Lifecycle and are therefore, automatically executed during their respective phases.

  • compiler:compile is bound to the compile phase and is used to compile the main source files.
  • compiler:testCompile is bound to the test-compile phase and is used to compile the test source files.


사용

특정 JDK 사용해서 compile

Setting the -source and -target of the Java Compiler

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <verbose>true</verbose>
          <fork>true</fork>
          <executable><!-- path-to-javac --></executable>
          <compilerVersion>1.7</compilerVersion>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
XML

하드코딩을 막기 위해 절대 경로 대신 다음과 같이 환경 변수를 사용할 수도 있다.

 <executable>${JAVA_1_7_HOME}/bin/javac</executable>
XML

또는 개발자마다 settings.xml 에 설정할 수 있다.

프로젝트에 사용하는 JDK 를 개발자마다 동일하게 할 경우는 Maven Enforcer Plugin 을 사용하여 requireJavaVersion을 설정하면 된다.


-source 와 -target 옵션 사용

Setting the -source and -target of the Java Compiler

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
XML

compiler arguments 전달

compilerArgument 프로퍼티를 사용하면 컴파일러에 아규먼트를 전달할 수 있다.

Setting the -source and -target of the Java Compiler

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>
XML

다음은 -DcompilerArgument=-Xlint:deprecation 를 

<compilerArgument>-DcompilerArgument=-Xlint:deprecation</compilerArgument>
CODE
-Xlint:derepecation 옵션은 명령행에서 실행해도 된다.
mvn clean compile  -Dmaven.compiler.showDeprecation=true
CODE

profile 과 연계하여 profile 별 JDK 설정


Ref