특정 Java 버전을 Maven으로 설정하는 방법
내 컴퓨터에는 두 개의 Java 버전이 설치되어 있습니다 (1.6 및 1.7은 수동으로 설치). 다른 프로젝트에는 둘 다 필요합니다. 그러나 Maven의 경우 1.7이 필요합니다. 이제 Maven은 1,6 Java 버전을 사용합니다. 어떻게 Maven을 1.7을 사용하도록 설정할 수 있습니까?
Maven은 JAVA_HOME
매개 변수를 사용하여 실행할 Java 버전을 찾습니다. 귀하의 의견으로는 구성에서 변경할 수 없다는 것을 알았습니다.
JAVA_HOME
maven을 시작하기 직전에 매개 변수 를 설정할 수 있습니다 (필요한 경우 나중에 다시 변경할 수 있음).- 당신은 또한 당신에 갈 수
mvn
/ (비 창)mvn.bat
/mvn.cmd
(창) 명시 적으로 거기 자바 버전을 설정합니다.
POM에서 컴파일러 속성을 설정할 수 있습니다 (예 : 1.8).
<project>
...
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
...
</project>
여러 Java 버전이 설치된 사용자를위한 솔루션 추가
우리는 큰 코드베이스를 가지고 있으며, 대부분은 Java로되어 있습니다. 내가 작업하는 대부분은 Java 1.7 또는 1.8로 작성되었습니다. JAVA_HOME
정적 이므로 .bashrc
다른 값으로 Maven을 실행하기 위해 별칭을 만들었습니다 .
alias mvn5="JAVA_HOME=/usr/local/java5 && mvn"
alias mvn6="JAVA_HOME=/usr/local/java6 && mvn"
alias mvn7="JAVA_HOME=/usr/local/java7 && mvn"
alias mvn8="JAVA_HOME=/usr/local/java8 && mvn"
이를 통해 프로젝트에 사용 된 JDK 버전에 관계없이 개발 시스템의 명령 줄에서 Maven을 실행할 수 있습니다.
내 두 센트를 추가하고 명시 적으로 솔루션을 제공합니다.
내 Windows 시스템에 설치 둘의 JDK가 - JDK 1.5
와 JDK 1.6
.
내 기본값 (및 Windows 시스템 환경 변수 JAVA_HOME
로 설정 ) 이로 설정되어 JDK 1.5
있습니다.
그러나을 사용하여 빌드 해야하는 maven 프로젝트가 있습니다 (예 : JBehave Tutorial 's Etsy.com ) JDK 1.6
.
이 시나리오의 솔루션 (작동했습니다!)은 @DanielBarbarian
에서 설정하도록 제안했습니다 mvn.bat
.
창의 배치 파일에 익숙하지 않은 사람들을 위해 기본적으로 set JAVA_HOME=<path_to_other_jdk>
뒤에 줄을 추가 @REM ==== START VALIDATION ====
했습니다 mvn.bat
(예 :) %MAVEN_HOME%\bin\mvn.bat
.
@REM ==== START VALIDATION ====
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45\jre
if not "%JAVA_HOME%" == "" goto OkJHome
창에서
JAVA_HOME
시스템 변수 내에서 변수 를 변경하지 않으려는 경우
mvn.bat
파일을 편집하고 다음 과 같은 줄을 추가하십시오
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45\jre
@REM ==== START VALIDATION ====
@Jonathan이 언급 한 것처럼 이 작업을 수행 할 수 있습니다
Mac에서 (& Linux?)
또는 JAVA_HOME
내부에서 변수 를 변경하지 않으려는 경우~/.bashrc
~/.bash_profile
java_home 도구를 사용하여 ~/.mavenrc
파일을 작성 하고 재정의 할 수 있습니다.JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_45`
위생 점검
다음 명령을 실행하여 모든 것이 제대로 작동하는지 확인할 수 있습니다. jdk 버전이 달라야합니다.
mvn -version
그때
java -version
최근에 Maven에서 7 년 동안 근무한 후 toolchains.xml에 대해 배웠습니다. Maven은 문서화되어 있으며 2.0.9부터 지원합니다- 툴체인 문서
따라서 toolchain.xml 파일을 ~ / .m2 / 폴더에 다음 내용으로 추가했습니다.
<toolchains xmlns="http://maven.apache.org/TOOLCHAINS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd">
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides>
<version>1.8</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java8</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>1.7</version>
<vendor>sun</vendor>
</provides>
<configuration>
<jdkHome>/opt/java7</jdkHome>
</configuration>
</toolchain>
</toolchains>
JDK Maven 실행에 관계없이 Maven이 프로젝트를 빌드하는 데 사용할 수있는 다른 JDK를 정의 할 수 있습니다. IDE의 프로젝트 레벨에서 JDK를 정의 할 때와 비슷합니다.
프로젝트 및 환경 변수에 대한 영향을 피하기 위해 소스 및 대상 Java 버전을 지정하여 프로젝트의 POM에 대해서만 Maven 컴파일러 플러그인을 구성 할 수 있습니다
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
...
</plugins>
문제에 대한 하나의 간단한 해결책-
JAVA_HOME = / usr / lib / jvm / java-6-sun / mvn 새로 설치
On Mac, it would look something like -
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/ mvn clean install
PS: One special case that i found is the above given command does not work on 'fish' shell. I also had bash shell available and it worked fine there. just use command 'bash' to switch to bash shell.
You can set Maven to use any java version following the instructions below.
Install jenv in your machine link
Check the available java versions installed in your machine by issuing the following command in command line.
jenv versions
You can specify global Java version using the following command.
jenv global oracle64-1.6.0.39
You can specify local Java version for any directory(Project) using the following command in the directory in command line.
jenv local oracle64-1.7.0.11
add the correct java version in your pom.xml
if you are running maven in command line install jenv maven plugin using below command
jenv enable-plugin maven
Now you can configure any java version in your machine to any project with out any trouble.
You could configure compiling sources using different JDK with maven-compiler-plugin
.
Just specify path to javac
in <executable>
tag. E.g for java11 it looks like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
<fork>true</fork>
<executable>C:\Program Files\Java\jdk-11.0.1\bin\javac</executable> <!--PATH TO JAVAC -->
</configuration>
</plugin>
On windows, I just add multiple batch files for different JDK versions to the Maven bin folder like this:
mvn11.cmd
@echo off
setlocal
set "JAVA_HOME=path\to\jdk11"
set "path=%JAVA_HOME%;%path%"
mvn %*
then you can use mvn11
to run Maven in the specified JDK.
On Linux/Unix, the JAVA_HOME within 'mvn' shell script is overridden by the settings in
$HOME/.mavenrc
please Where to add JAVA_HOME and MAVEN path variables in linux
Also you can have two versions of maven installed, and edit one of them, editing here:
mvn(non-windows)/mvn.bat/mvn.cmd(windows)
replacing your %java_home% appearances to your java desired path. Then just execute maven from that modified path
I did not have success on mac with just setting JAVA_HOME
in the console but I was successful with this approach
- Create .mavenrc file at your at your home directory (so the file will path will be
~/.mavenrc
- Into that file paste
export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
On Macs, (assuming you have the right version installed)
JAVA_HOME=`/usr/libexec/java_home -v 1.8` mvn clean install -DskipTests
참고URL : https://stackoverflow.com/questions/19654557/how-to-set-specific-java-version-to-maven
'Programing' 카테고리의 다른 글
ASP.NET :이 메서드는 응용 프로그램의 사전 시작 초기화 단계에서 호출 할 수 없습니다 (0) | 2020.06.26 |
---|---|
C ++ cout 16 진수 값? (0) | 2020.06.26 |
JavaScript에서 전역 변수를 선언하는 방법은 무엇입니까? (0) | 2020.06.26 |
Laravel에서 외부 API로 HTTP 요청 수행 (0) | 2020.06.26 |
ImportError : 이름이 notebook.notebookapp 인 모듈이 없습니다. (0) | 2020.06.26 |