Maven에서 Java 버전 지정-속성과 컴파일러 플러그인의 차이점
나는 maven에 익숙하지 않고 멀티 모듈 프로젝트를 실험하는 동안 부모 maven pom의 모든 자식 모듈에 대해 Java 버전을 어떻게 지정할 수 있는지 궁금해하기 시작했습니다. 오늘까지 나는 단지 사용했다 :
<properties>
<java.version>1.8</java.version>
</properties>
그러나 조사 할 때 다음과 같이 maven 컴파일러 플러그인에서 Java 버전을 지정할 수도 있음을 발견했습니다.
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
그런 다음 이것을 플러그인 관리 태그로 감싸서 하위 폼을 사용할 수있게합니다. 첫 번째 질문은 속성과 maven 컴파일러 플러그인에서 Java 버전을 설정하는 것의 차이점 은 무엇입니까?
나는 명확한 대답을 찾을 수 없었지만 연구 과정에서 다음과 같은 방법으로 Java 버전을 지정할 수 있음을 발견했습니다.
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
명시 적으로 선언하지 않아도 컴파일러 플러그인이 있음을 제안합니다. 로 mvn 패키지 출력 실행
maven-compiler-plugin:3.1:compile (default-compile) @ testproj ---
그리고 내가 선언하지 않은 다른 플러그인들. 그렇다면 플러그인이 기본이며 maven pom의 숨겨진 부분입니까? 속성과 maven 플러그인 구성 요소에서 소스 / 대상을 설정하는 데 차이점이 있습니까?
다른 질문은-어떤 방법을 사용해야합니까 (그리고 같지 않은 경우)? 다중 모듈 프로젝트에 가장 적합한 것은 무엇이고 pom에 지정된 Java 버전이 JAVA_HOME에 지정된 버전과 다른 경우 어떻게됩니까?
JDK 버전을 지정하는 방법은 무엇입니까?
1) <java.version>
Maven 문서에는 언급되어 있지 않습니다.
스프링 부트 특이성입니다.
소스와 대상 자바 버전을 동일한 버전으로 설정하여 둘 다 java 1.8을 지정할 수 있습니다.
<properties>
<java.version>1.8</java.version>
</properties>
Spring Boot를 사용하는 경우 자유롭게 사용하십시오.
2) maven-compiler-plugin
또는 maven.compiler.source
/ maven.compiler.target
속성을 사용하여 및를 지정 source
하는 target
것은 같습니다.
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
과
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
받는있어서 동등 컴파일러 플러그인 메이븐 문서 사람 <source>
및 <target>
속성 컴파일러 구성 요소를 사용 maven.compiler.source
하고 maven.compiler.target
그들이 정의되어 있으면.
-source
Java 컴파일러 의 인수
기본값은1.6
입니다.
사용자 속성은 다음과 같습니다maven.compiler.source
.
-target
Java 컴파일러 의 인수
기본값은1.6
입니다.
사용자 속성은 다음과 같습니다maven.compiler.target
.
About the default values for source
and target
, note that since the 3.8.0
of the maven compiler, the default values have changed from 1.5
to 1.6
.
3) The maven-compiler-plugin 3.6
and later versions provide a new way :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>9</release>
</configuration>
</plugin>
You could also declare just :
<properties>
<maven.compiler.release>9</maven.compiler.release>
</properties>
But at this time it will not work as the maven-compiler-plugin
default version you use doesn't rely on a recent enough version.
The Maven release
argument conveys release
: a new JVM standard option that we could pass from Java 9 :
Compiles against the public, supported and documented API for a specific VM version.
This way provides a standard way to specify the same version for the source
, the target
and the bootstrap
JVM options.
Note that specifying the bootstrap
is a good practice for cross compilations and it will not hurt if you don't make cross compilations either.
Which is the best way to specify the JDK version?
The first way (<java.version>
) is allowed only if you use Spring Boot.
For Java 8 and below :
About the two other ways : valuing the maven.compiler.source
/maven.compiler.target
properties or using the maven-compiler-plugin
, you can use one or the other. It changes nothing in the facts since finally the two solutions rely on the same properties and the same mechanism : the maven core compiler plugin.
Well, if you don't need to specify other properties or behavior than Java versions in the compiler plugin, using this way makes more sense as this is more concise:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
From Java 9 :
The release
argument (third point) is a way to strongly consider if you want to use the same version for the source and the target.
What happens if the version differs between the JDK in JAVA_HOME and which one specified in the pom.xml?
It is not a problem if the JDK referenced by the JAVA_HOME
is compatible with the version specified in the pom but to ensure a better cross-compilation compatibility think about adding the bootstrap
JVM option with as value the path of the rt.jar
of the target
version.
An important thing to consider is that the source
and the target
version in the Maven configuration should not be superior to the JDK version referenced by the JAVA_HOME
.
A older version of the JDK cannot compile with a more recent version since it doesn't know its specification.
To get information about the source, target and release supported versions according to the used JDK, please refer to java compilation : source, target and release supported versions.
How handle the case of JDK referenced by the JAVA_HOME is not compatible with the java target and/or source versions specified in the pom?
For example, if your JAVA_HOME
refers to a JDK 1.7 and you specify a JDK 1.8 as source and target in the compiler configuration of your pom.xml, it will be a problem because as explained, the JDK 1.7 doesn't know how to compile with.
From its point of view, it is an unknown JDK version since it was released after it.
In this case, you should configure the Maven compiler plugin to specify the JDK in this way :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerVersion>1.8</compilerVersion>
<fork>true</fork>
<executable>D:\jdk1.8\bin\javac</executable>
</configuration>
</plugin>
You could have more details in examples with maven compiler plugin.
It is not asked but cases where that may be more complicated is when you specify source but not target. It may use a different version in target according to the source version. Rules are particular : you can read about them in the Cross-Compilation Options part.
Why the compiler plugin is traced in the output at the execution of the Maven package
goal even if you don't specify it in the pom.xml?
To compile your code and more generally to perform all tasks required for a maven goal, Maven needs tools. So, it uses core Maven plugins (you recognize a core Maven plugin by its groupId
: org.apache.maven.plugins
) to do the required tasks : compiler plugin for compiling classes, test plugin for executing tests, and so for... So, even if you don't declare these plugins, they are bound to the execution of the Maven lifecycle.
At the root dir of your Maven project, you can run the command : mvn help:effective-pom
to get the final pom effectively used. You could see among other information, attached plugins by Maven (specified or not in your pom.xml), with the used version, their configuration and the executed goals for each phase of the lifecycle.
In the output of the mvn help:effective-pom
command, you could see the declaration of these core plugins in the <build><plugins>
element, for example :
...
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
...
You can have more information about it in the introduction of the Maven lifeycle in the Maven documentation.
Nevertheless, you can declare these plugins when you want to configure them with other values as default values (for example, you did it when you declared the maven-compiler plugin in your pom.xml to adjust the JDK version to use) or when you want to add some plugin executions not used by default in the Maven lifecycle.
None of the above solutions worked for me straight away. So I did the followings:-
Added
<properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties>
in pom.xml
Went to the
Project Properties > Java Build Path
, then removed the JRE System Library which was pointing toJRE1.5
.Force updated the project.
Consider the alternative:
<properties>
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
</properties>
It should be the same thing of maven.compiler.source/maven.compiler.target
but the above solution works for me, otherwise the second one gets the parent specification (I have a matrioska of .pom)
'development' 카테고리의 다른 글
FileInputStream을 사용할 때 이상적인 버퍼 크기를 어떻게 결정합니까? (0) | 2020.06.18 |
---|---|
프로그래밍 방식으로 아이폰의 MAC 주소를 얻는 방법 (0) | 2020.06.18 |
단위 테스트 프로젝트가 대상 응용 프로그램의 app.config 파일을로드 할 수 있습니까? (0) | 2020.06.18 |
JavaScript에서 UTC 타임 스탬프를 어떻게 얻습니까? (0) | 2020.06.18 |
비트 시프트는 엔디안에 의존합니까? (0) | 2020.06.18 |