How to make gradle use the correct JDK when compiling?

We use gradle to create our Java projects, some of them based on JDK7 and some based on JDK8. I know the property org.gradle.java.home, but it seems wrong to me.

If I set it to '~ / .gradle / gradle.properties', it will force me to use the same JDK for all my gradle projects.

If I configure it in '/ mygit-project / gradle.properties', it will force me to put the link to the local JDK installation in the general Git repository. The path to the JDK does not belong there.

What I basically would like to have is something like this in '~ / .gradle / gradle.properties':

systemProp.jdk8=/my/local/path/to/jdk8 systemProp.jdk7=/my/local/path/to/jdk7 

And under source control in '/ my-git-project / gradle.properties':

 org.gradle.java.home=$systemProp.jdk8 

What is the best solution / workaround for this?

+5
source share
2 answers

This is more a process question than a Gradle or Java question. Ultimately, you must get everyone to specify their various JAVA_HOME, without being burdensome. You have several options:

  • Command line: ./ gradlew -Dorg.gradle.java.home = / path_to_jdk_directory

But, of course, now everyone should type some disgusting garbage into their command line every time they start the assembly.

  1. gradle.properties and check the path. Then get everyone to use the same path.

Not everyone wants to use the same path, especially if you have Mac / Unix and PC users.

2b. Instead of using the identical path, everyone can change their local gradle.properties with their custom values ​​and then never log their changes.

The primary problem: someone is completely going to test their local values ​​and spoil the CI and everyone else.

  1. gradle.properties.template check-in, each creates its own gradle.properties; type gradle.properties in .gitignore

This may be your best bet. You have a template file that you register, but everyone should copy it to gradle.properties and fill in their specific values. You need to configure your CI to do something similar, or register something like gradle.ci.properties and use CI. But everyone has to do it once, and not once for assembly. Unfortunately, they will have to update their personal file every time the template changes (unless you have written code for this).

+1
source

We deal with this problem as follows:

 The one who starts the build is responsible for properly setting JAVA_HOME 

On machines for developers that can be fragile. But it works great if you create and deploy from a dedicated server.

0
source

Source: https://habr.com/ru/post/1241801/


All Articles