15

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

2 Answers 2

6

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.

4

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.

1
  • Thank your for the detailed response. I totally agree with the fact that overriding JAVA HOME may not produce expected result and what you said makes sense. Let's see if we can have some more contributors so we will have a general consensus that it is not do-able. Your help is really appreciated!
    – Noman Amir
    Mar 11, 2013 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.