简体   繁体   中英

How to hardcode java used by maven on windows

I know that maven uses JAVA_HOME environment variable to start itself. But I cannot change that variable due to lack of administration priviligies (security policy in company). I have Oracle JRE installed which is used by maven currently. I'm running on Windows 10.

I want to hardcode my maven to use specific JDK folder in order to run itself. The one solution I'm working on is to hardcode that in $MAVEN_HOME\\bin\\mvn file but I cannot to do so. As when I'm trying to add something like:

set JAVA_HOME="E:\ProgramFiles\java\jdk8"
export JAVA_HOME="E:\ProgramFiles\java\jdk8"
JAVA_HOME=`E:\ProgramFiles\java\jdk8`

in that file it still points to installed JRE as I can see from the output of mvn -version command.

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T22:39:06+03:00) Maven home: E:\\ProgramFiles\\apache-maven-3.5.0\\bin\\.. Java version: 1.8.0_131, vendor: Oracle Corporation Java home: C:\\Program Files\\Java\\jre1.8.0_131 Default locale: ru_RU, platform encoding: Cp1251 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Question is: how I need to change my mvn or mvn.cmd files from $MAVEN_HOME\\bin in order to use desired hardcoded JDK version? Or is there another solution to that (as I need to run mvn command from anywhere - and it need to use specific jdk), except changing environment variables?

In Linux you can do the same by editing/creating the mavenrc file.

vi ~/.mavenrc

with some input like:

export PATH
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home
export PATH=${PATH}:$JAVA_HOME/bin

I'm running on Windows 10

There can be a similar file(haven't tried that) created at %HOME%\\mavenrc.bat with the appropriate definition.

Edit : Thanks to @Robert for pointing it out, for windows its

%USERPROFILE%\mavenrc_pre.cmd

file that you need to modify/create.

You don't need to use export command in windows. What you could do is, edit mvn.cmd file and define your own MAVEN_JAVA_HOME at the beginning of the file.

set MAVEN_JAVA_HOME="E:\ProgramFiles\java\jdk8" 
echo %MAVEN_JAVA_HOME%

and replace all JAVA_HOME with MAVEN_JAVA_HOME in your mvn.cmd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM