Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass JAVA HOME as parameter to mvn

I would like to know if it is possible to pass JAVA_HOME as parameter to mvn command line. I have searched almost everywhere, but couldn't find an answer to that.

I know we can set JAVA_HOME using export, but I need to pass it as parameter if that is possible. Something like:

 mvn install -DJava_Home=/usr/java/jdk-1.7.0
like image 772
Noman Amir Avatar asked Mar 11 '13 17:03

Noman Amir


People also ask

Does Maven use JAVA_HOME?

How does Maven verify the JAVA_HOME path? Before running any goals, Maven checks for the existence of the java command in the path specified by JAVA_HOME or by asking the OS for a default JDK installation. If the executable is not found, Maven terminates with the error.

How do I set Java version to Maven?

You can set the JAVA_HOME parameter just before you start maven (and change it back afterwards if need be). You could also go into your mvn (non-windows)/ mvn. bat / mvn. cmd (windows) and set your java version explicitly there.

Which JDK is used by Maven?

The Maven tool uses JDK version 11.0. 10. The default JDK is set to 13.0.


2 Answers

Another hacky way I did this: ( I have most of my projects on Java 7, but a handful on java 8 )

1) Add a new env variable JAVA8_HOME to your .zshrc ( or similar )

2) copy the 'mvn' executable and call it 'mvn8'

3) Replace 'JAVA_HOME' with 'JAVA8_HOME' in 'mvn8'

Now, mvn8 clean install should just work.

like image 152
jackrabb1t Avatar answered Sep 30 '22 21:09

jackrabb1t


no, not directly, but looking at mvn.bat on my machine i see this promising snippet:

@REM Execute a user defined script before this one
if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"

so you could override any variable you like in a mavenrc_pre script file, although i realize this wont let you override java home from the command line. worst case, mvn is a simple script file and you could add the option to it. also note that simply overriding JAVA_HOME may not always produce the expected results as on many systems JAVA_HOME\bin is on the path. this means that even if you override it the previous jvm will still be on the path, which might lead to unexpected results.

like image 40
radai Avatar answered Sep 30 '22 21:09

radai