Set JDK home (javac path) in user's gradle.properties
Asked Answered
M

3

6

I need to specify the path to javac in my gradle project. I can do this by adding the following to my build.gradle file:

options.forkOptions.executable = '/home/mj/lib/jdk1.7.0_80/bin/javac'

The problem is that this file is shared in our repository and I do not want the setting to get pushed there. I tried putting it in my local gradle.properties file, but it didn't work. Also setting gradle.java.home does not affect this. I guess it's because gradle uses gradle.java.home for java and not for javac, right? I also (hopelessly) tried setting a gradle.jdk.home which (not surprisingly) didn't work either!

Is there any place outside the project that is included in build.gradle?

Mariannemariano answered 6/9, 2015 at 9:6 Comment(0)
M
4

It's closing in on the end of 2021 and there's a much simpler approach now. In your project's root directory add the following to your gradle.properties file (add the file, too, if it doesn't exist):

org.gradle.java.home=path/to/jdk
Mauramauralia answered 15/12, 2021 at 17:46 Comment(2)
this may go to public when commiting to, e.g., Github. How to deal with sensitive path information?Habitat
@LucasSousa .gitignore, You don't check it into the repo.Mauramauralia
I
7

In your gradle.properties:

javacPath=/home/mj/lib/jdk1.7.0_80/bin/javac

In your build.gradle

options.forkOptions.executable = project.property('javacPath')
Illusion answered 6/9, 2015 at 9:43 Comment(2)
Thanks! That works. Although it would be much better not to have this configuration inside build.gradle.Mariannemariano
There is a way to put setting in a user's gradle.properties file .... like described here docs.gradle.org/current/userguide/…Venison
M
4

It's closing in on the end of 2021 and there's a much simpler approach now. In your project's root directory add the following to your gradle.properties file (add the file, too, if it doesn't exist):

org.gradle.java.home=path/to/jdk
Mauramauralia answered 15/12, 2021 at 17:46 Comment(2)
this may go to public when commiting to, e.g., Github. How to deal with sensitive path information?Habitat
@LucasSousa .gitignore, You don't check it into the repo.Mauramauralia
O
3

Using a path relative to "java.home" has worked for us. Not elegant, but this does not need another setting. The reason why we need this in the first place is that we build with a JDK that's part of the working copy, not pre-installed with the Jenkins slave. We have no javac/JDK on the slave, just a JRE to run the Jenkins slave. Our gradlew in the working copy points to the JDK next to it.

compileJava {
  options.fork = true
  options.forkOptions.executable = "${System.properties['java.home']}/../bin/javac"
}

(Note: At Java run time, java.home points to the $JAVA_HOME/jre folder within the JDK. That's one below where the JAVA_HOME environment variable points to.)

Ordeal answered 22/10, 2015 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.