emjay
emjay

Reputation: 440

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

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?

Upvotes: 6

Views: 15652

Answers (3)

PeterToTheThird
PeterToTheThird

Reputation: 397

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

Upvotes: 4

mgaert
mgaert

Reputation: 2398

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.)

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 692081

In your gradle.properties:

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

In your build.gradle

options.forkOptions.executable = project.property('javacPath')

Upvotes: 7

Related Questions