0

Is it possible to set a property in a POM file with reference to $JAVA_HOME?

<executable><$JAVA_HOME>/bin/javac</executable>

The reason I am doing this is the only way I can get maven to use the correct compiler version is by setting the <executable>. But I don't want to hard-code the JAVA_HOME location in the pom file.

Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
Richard
  • 8,193
  • 28
  • 107
  • 228
  • 1
    Can we have more context on why you want to do this? In general maven should already know about and respect `JAVA_HOME`? – PiRocks Jul 23 '20 at 07:28
  • You can refer this link https://stackoverflow.com/questions/2503658/specify-jdk-for-maven-to-use – Dhruv Jul 23 '20 at 07:29
  • Sounds like you want to use a different jdk than the one that maven uses. – Thorbjørn Ravn Andersen Jul 23 '20 at 07:29
  • My question stems from this question where I cannot get maven to use the version of java I want. https://stackoverflow.com/questions/63048137/setting-java-version-for-maven-build/63048917?noredirect=1#comment111493403_63048917 – Richard Jul 23 '20 at 07:37
  • 1
    @Richard in that question you seem to have completely other problem. I think either your "mvn" script or settings.xml is somehow broken, as it should use JAVA_HOME by default, but doesn't seem to. – eis Jul 23 '20 at 07:44
  • @eis yes, there is some very strange behaviour on the server. I am still trying to get to the bottom of it. – Richard Jul 23 '20 at 07:46
  • 2
    If you really need to use a different JDK for running Maven and the JDK you would like to build with (compile/test) you should take a look at https://maven.apache.org/guides/mini/guide-using-toolchains.html – khmarbaise Jul 23 '20 at 09:03

1 Answers1

5

You can use ${env.ANY_ENV_VARIABLE} to get env variables in pom.xml. So just use ${env.JAVA_HOME} in your case

Planck Constant
  • 1,406
  • 1
  • 17
  • 19
  • 2
    ...but note that maven already should use JAVA_HOME, so it is expected that this will change nothing - unless something else has been manually changed in the maven settings or script – eis Jul 23 '20 at 07:45