Skip to content

Commit

Permalink
Expose the 'java.import.gradle.java.home' preference
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed Jul 21, 2020
1 parent 2eb3c19 commit 9160c7e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ public class GradlePreferenceChangeListener implements IPreferencesChangeListene
public void preferencesChange(Preferences oldPreferences, Preferences newPreferences) {
ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
if (projectsManager != null) {
if (hasAllowedChecksumsChanged(oldPreferences, newPreferences)) {
boolean gradleJavaHomeChanged = !Objects.equals(oldPreferences.getGradleJavaHome(), newPreferences.getGradleJavaHome());
if (hasAllowedChecksumsChanged(oldPreferences, newPreferences) || gradleJavaHomeChanged) {
for (IProject project : ProjectUtils.getGradleProjects()) {
if (newPreferences.isGradleWrapperEnabled()) {
updateProject(projectsManager, project);
if (newPreferences.isGradleWrapperEnabled() || gradleJavaHomeChanged) {
updateProject(projectsManager, project, gradleJavaHomeChanged);
}
}
}
}
}

private void updateProject(ProjectsManager projectsManager, IProject project) {
private void updateProject(ProjectsManager projectsManager, IProject project, boolean gradleJavaHomeChanged) {
String projectDir = project.getLocation().toFile().getAbsolutePath();
Path projectPath = Paths.get(projectDir);
if (Files.exists(projectPath.resolve("gradlew"))) {
if (gradleJavaHomeChanged || Files.exists(projectPath.resolve("gradlew"))) {
ProjectConfiguration configuration = CorePlugin.configurationManager().loadProjectConfiguration(project);
GradleDistribution distribution = configuration.getBuildConfiguration().getGradleDistribution();
if (!(distribution instanceof WrapperGradleDistribution)) {
if (gradleJavaHomeChanged || !(distribution instanceof WrapperGradleDistribution)) {
projectsManager.updateProject(project, true);
} else {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ public static File getGradleUserHomeFile() {
return (gradleUserHome == null || gradleUserHome.isEmpty()) ? null : new File(gradleUserHome);
}

public static File getGradleJavaHomeFile() {
Preferences preferences = getPreferences();
if (StringUtils.isNotBlank(preferences.getGradleJavaHome())) {
File file = new File(preferences.getGradleJavaHome());
if (file.isDirectory()) {
return file;
}
}
return null;
}

protected void startSynchronization(Path projectFolder, IProgressMonitor monitor) {
File location = projectFolder.toFile();
boolean shouldSynchronize = shouldSynchronize(location);
Expand All @@ -233,14 +244,16 @@ protected void startSynchronization(Path projectFolder, IProgressMonitor monitor

public static BuildConfiguration getBuildConfiguration(Path rootFolder) {
GradleDistribution distribution = getGradleDistribution(rootFolder);
IVMInstall javaDefaultRuntime = JavaRuntime.getDefaultVMInstall();
File javaHome;
File javaHome = getGradleJavaHomeFile();
Preferences preferences = getPreferences();
if (javaDefaultRuntime != null && javaDefaultRuntime.getVMRunner(ILaunchManager.RUN_MODE) != null) {
javaHome = javaDefaultRuntime.getInstallLocation();
} else {
String javaHomeStr = preferences.getJavaHome();
javaHome = javaHomeStr == null ? null : new File(javaHomeStr);
if (javaHome == null) {
IVMInstall javaDefaultRuntime = JavaRuntime.getDefaultVMInstall();
if (javaDefaultRuntime != null && javaDefaultRuntime.getVMRunner(ILaunchManager.RUN_MODE) != null) {
javaHome = javaDefaultRuntime.getInstallLocation();
} else {
String javaHomeStr = preferences.getJavaHome();
javaHome = javaHomeStr == null ? null : new File(javaHomeStr);
}
}
File gradleUserHome = getGradleUserHomeFile();
List<String> gradleArguments = preferences.getGradleArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public class Preferences {
* Preference key for setting GRADLE_HOME.
*/
public static final String GRADLE_HOME = "java.import.gradle.home";
/**
* Preference key for the JVM used to run the Gradle daemon..
*/
public static final String GRADLE_JAVA_HOME = "java.import.gradle.java.home";
/**
* Preference key for setting GRADLE_USER_HOME.
*/
Expand Down Expand Up @@ -389,6 +393,7 @@ public class Preferences {
private List<String> gradleArguments;
private List<String> gradleJvmArguments;
private String gradleHome;
private String gradleJavaHome;
private String gradleUserHome;
private boolean importMavenEnabled;
private boolean mavenDownloadSources;
Expand Down Expand Up @@ -567,6 +572,7 @@ public Preferences() {
gradleArguments = new ArrayList<>();
gradleJvmArguments = new ArrayList<>();
gradleHome = null;
gradleJavaHome = null;
gradleUserHome = null;
importMavenEnabled = true;
mavenDownloadSources = false;
Expand Down Expand Up @@ -640,6 +646,8 @@ public static Preferences createFrom(Map<String, Object> configuration) {
prefs.setGradleJvmArguments(gradleJvmArguments);
String gradleHome = getString(configuration, GRADLE_HOME);
prefs.setGradleHome(gradleHome);
String gradleJavaHome = getString(configuration, GRADLE_JAVA_HOME);
prefs.setGradleJavaHome(gradleJavaHome);
String gradleUserHome = getString(configuration, GRADLE_USER_HOME);
prefs.setGradleUserHome(gradleUserHome);
boolean importMavenEnabled = getBoolean(configuration, IMPORT_MAVEN_ENABLED, true);
Expand Down Expand Up @@ -868,6 +876,11 @@ public Preferences setGradleHome(String gradleHome) {
return this;
}

public Preferences setGradleJavaHome(String gradleJavaHome) {
this.gradleJavaHome = gradleJavaHome;
return this;
}

public Preferences setGradleUserHome(String gradleUserHome) {
this.gradleUserHome = gradleUserHome;
return this;
Expand Down Expand Up @@ -1126,6 +1139,10 @@ public String getGradleHome() {
return gradleHome;
}

public String getGradleJavaHome() {
return gradleJavaHome;
}

public String getGradleUserHome() {
return gradleUserHome;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ public void testJavaHome() throws Exception {
}
}

@Test
public void testGradleJavaHome() throws Exception {
Preferences prefs = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
String gradleJavaHomePreference = prefs.getGradleJavaHome();
IVMInstall defaultVM = JavaRuntime.getDefaultVMInstall();
try {
IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
IVMInstall[] vms = installType.getVMInstalls();
IVMInstall vm = vms[0];
JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
String javaHome = new File(TestVMType.getFakeJDKsLocation(), "1.8").getAbsolutePath();
prefs.setGradleJavaHome(javaHome);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath rootFolder = root.getLocation().append("/projects/gradle/simple-gradle");
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootFolder.toFile().toPath());
assertEquals(javaHome, build.getJavaHome().get().getAbsolutePath());
} finally {
prefs.setJavaHome(gradleJavaHomePreference);
if (defaultVM != null) {
JavaRuntime.setDefaultVMInstall(defaultVM, new NullProgressMonitor());
}
}
}

@Test
public void testDisableImportGradle() throws Exception {
boolean enabled = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isImportGradleEnabled();
Expand Down

0 comments on commit 9160c7e

Please sign in to comment.