stevesaliman / gradle-properties-plugin

Gradle plugin to simplify loading project properties from external environment specific files
Apache License 2.0
192 stars 28 forks source link

Can't set `org.gradle.java.home` in local property #19

Closed igr closed 9 years ago

igr commented 9 years ago

I don't know what I am doing wrong, but I am not able to set custom org.gradle.java.home in gradle-local.properties. When I run gradle in debug mode, I see that 1 property is loaded from gradle-local.properties (the local jdk path), however, this value is simply ignored.

igr commented 9 years ago

I see that property is set:

project.properties.each {println "   $it"}

however, it is not applied to the project; I am not able to compile project with different JDK?

stevesaliman commented 9 years ago

Short answer: Gradle itself doesn't notice changes to the org.gradle.java.home property once set. As a wourkaround, you will need to either specify the JDK in one of Gradle's standard property files, or add -Dorg.gradle.java.home=/path/to/jdk to the command line when you are building in an environment where a different JDK is desired.

In order to fix this, you'd need to report a bug against Gradle itself to allow you to change the JDK used inside your build.gradle script itself.

If you are needing help falling asleep, here is a longer explanation :-)

When Gradle starts up, it reads the project's gradle.properties file, the user's gradle.properties file, system properties, and any properties set on the command line with -D arguments. It initializes project properties and does other internal things all prior to reading your build.gradle file or applying any plugins. When you apply the properties plugin, it re-reads these files, but adds some additional files to the mix (like gradle-local.properties), giving you greater control over how properties get set, but it can't do much about decisions that Gradle has already made up to that point, which appears to be what is causing this particular issue. It looks like Gradle decides what JDK to use before it starts processing your build.gradle file, and doesn't let you change it afterwords.

To prove this, I tried the following test:

  1. I added project.ext."org.gradle.java.home"="/opt/jdk1.7.0_09" to my build.gradle file. This sets the property completely outside of the properties plugin. You could set up a simple project that doesn't even use the properties plugin, and you'd get the same result.
  2. I ran gradle properties and saw that the org.gradle.java.home property was indeed set.
  3. I ran gradle compileJava and watched as my newly specified JDK was completely ignored.
igr commented 9 years ago

Thank you!