Skip to content

Commit

Permalink
Add additional Gradle preferences
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Gates <[email protected]>
  • Loading branch information
gatesn authored and fbricon committed Jul 8, 2019
1 parent 234ebc8 commit 6fa7d16
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
target/
bin/
**/lib/
maven-wrapper.jar
maven-wrapper.jar
*.iml
.idea
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.FilenameFilter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -131,6 +132,9 @@ public static File getGradleHomeFile() {
}

public static File getGradleHomeFile(Map<String, String> env, Properties sysprops) {
if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleHome() != null) {
return new File(JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleHome());
}
String gradleHome = env.get(GRADLE_HOME);
if (gradleHome == null || !new File(gradleHome).isDirectory()) {
gradleHome = sysprops.getProperty(GRADLE_HOME);
Expand Down Expand Up @@ -164,16 +168,20 @@ protected void startSynchronization(Path rootFolder, IProgressMonitor monitor) {
}
}

public BuildConfiguration getBuildConfiguration(Path rootFolder) {
public static BuildConfiguration getBuildConfiguration(Path rootFolder) {
GradleDistribution distribution = getGradleDistribution(rootFolder);
boolean overrideWorkspaceConfiguration = !(distribution instanceof WrapperGradleDistribution);
String javaHomeStr = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getJavaHome();
File javaHome = javaHomeStr == null ? null : new File(javaHomeStr);
List<String> gradleArguments = JavaLanguageServerPlugin.getPreferencesManager() != null ? JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleArguments() : new ArrayList<>();
List<String> gradleJvmArguments = JavaLanguageServerPlugin.getPreferencesManager() != null ? JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleJvmArguments() : new ArrayList<>();
// @formatter:off
BuildConfiguration build = BuildConfiguration.forRootProjectDirectory(rootFolder.toFile())
.overrideWorkspaceConfiguration(overrideWorkspaceConfiguration)
.gradleDistribution(distribution)
.javaHome(javaHome)
.arguments(gradleArguments)
.jvmArguments(gradleJvmArguments)
.build();
// @formatter:on
return build;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,18 @@ public class Preferences {
* Preference key for gradle version to use when the gradle wrapper is not used.
*/
public static final String GRADLE_VERSION = "java.import.gradle.version";

/**
* Preference key for arguments to pass to Gradle
*/
public static final String GRADLE_ARGUMENTS = "java.import.gradle.arguments";
/**
* Preference key for JVM arguments to pass to Gradle
*/
public static final String GRADLE_JVM_ARGUMENTS = "java.import.gradle.jvmArguments";
/**
* Preference key for setting GRADLE_HOME.
*/
public static final String GRADLE_HOME = "java.import.gradle.home";
/**
* Preference key to enable/disable maven importer.
*/
Expand Down Expand Up @@ -288,6 +299,9 @@ public class Preferences {
private boolean importGradleEnabled;
private boolean gradleWrapperEnabled;
private String gradleVersion;
private List<String> gradleArguments;
private List<String> gradleJvmArguments;
private String gradleHome;
private boolean importMavenEnabled;
private boolean mavenDownloadSources;
private boolean implementationsCodeLensEnabled;
Expand Down Expand Up @@ -397,6 +411,9 @@ public Preferences() {
importGradleEnabled = true;
gradleWrapperEnabled = true;
gradleVersion = null;
gradleArguments = new ArrayList<>();
gradleJvmArguments = new ArrayList<>();
gradleHome = null;
importMavenEnabled = true;
mavenDownloadSources = false;
referencesCodeLensEnabled = true;
Expand Down Expand Up @@ -453,6 +470,12 @@ public static Preferences createFrom(Map<String, Object> configuration) {
prefs.setGradleWrapperEnabled(gradleWrapperEnabled);
String gradleVersion = getString(configuration, GRADLE_VERSION);
prefs.setGradleVersion(gradleVersion);
List<String> gradleArguments = getList(configuration, GRADLE_ARGUMENTS);
prefs.setGradleArguments(gradleArguments);
List<String> gradleJvmArguments = getList(configuration, GRADLE_JVM_ARGUMENTS);
prefs.setGradleJvmArguments(gradleJvmArguments);
String gradleHome = getString(configuration, GRADLE_HOME);
prefs.setGradleHome(gradleHome);
boolean importMavenEnabled = getBoolean(configuration, IMPORT_MAVEN_ENABLED, true);
prefs.setImportMavenEnabled(importMavenEnabled);
boolean downloadSources = getBoolean(configuration, MAVEN_DOWNLOAD_SOURCES, false);
Expand Down Expand Up @@ -562,6 +585,21 @@ public Preferences setGradleVersion(String gradleVersion) {
return this;
}

public Preferences setGradleArguments(List<String> arguments) {
this.gradleArguments = arguments == null ? new ArrayList<>() : arguments;
return this;
}

public Preferences setGradleJvmArguments(List<String> jvmArguments) {
this.gradleJvmArguments = jvmArguments == null ? new ArrayList<>() : jvmArguments;
return this;
}

public Preferences setGradleHome(String gradleHome) {
this.gradleHome = gradleHome;
return this;
}

public Preferences setFormatterUrl(String formatterUrl) {
this.formatterUrl = formatterUrl;
return this;
Expand Down Expand Up @@ -775,6 +813,18 @@ public String getGradleVersion() {
return gradleVersion;
}

public List<String> getGradleArguments() {
return gradleArguments == null ? new ArrayList<>() : gradleArguments;
}

public List<String> getGradleJvmArguments() {
return gradleJvmArguments == null ? new ArrayList<>() : gradleJvmArguments;
}

public String getGradleHome() {
return gradleHome;
}

public String getFormatterUrl() {
return formatterUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,8 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import static org.eclipse.jdt.ls.core.internal.ProjectUtils.getJavaSourceLevel;
import static org.eclipse.jdt.ls.core.internal.WorkspaceHelper.getProject;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.eclipse.buildship.core.BuildConfiguration;
import org.eclipse.buildship.core.FixedVersionGradleDistribution;
import org.eclipse.buildship.core.GradleDistribution;
import org.eclipse.buildship.core.LocalGradleDistribution;
import org.eclipse.buildship.core.WrapperGradleDistribution;
import com.google.common.collect.ImmutableList;
import org.eclipse.buildship.core.*;
import org.eclipse.buildship.core.internal.CorePlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
Expand All @@ -47,6 +29,17 @@
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.eclipse.jdt.ls.core.internal.ProjectUtils.getJavaSourceLevel;
import static org.eclipse.jdt.ls.core.internal.WorkspaceHelper.getProject;
import static org.junit.Assert.*;

/**
* @author Fred Bricon
*
Expand Down Expand Up @@ -187,7 +180,7 @@ public void testWorkspaceSettings() throws Exception {
}

@Test
public void testGradleUserHome() throws Exception {
public void testGradleUserHome() {
Map<String, String> env = new HashMap<>();
Properties sysprops = new Properties();
File file = null;
Expand All @@ -206,7 +199,7 @@ public void testGradleUserHome() throws Exception {
projectFile = new File(rootFile, "fakeProject");
projectFile.mkdir();
projectFile.deleteOnExit();
BuildConfiguration build = new GradleProjectImporter().getBuildConfiguration(file.toPath());
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(file.toPath());
assertFalse(build.getGradleUserHome().isPresent());
} finally {
if (file != null) {
Expand Down Expand Up @@ -240,6 +233,59 @@ public void testGradlePropertiesFile() throws Exception {
assertTrue(projectsManager.isBuildFile(file));
}

@Test
public void testGradleHomePreference() {
String home = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleHome();
Map<String, String> env = new HashMap<>();
Properties sysprops = new Properties();
try {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleHome(null);
assertNull(GradleProjectImporter.getGradleHomeFile(env, sysprops));

JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleHome("/gradle/home");
assertEquals(new File("/gradle/home"), GradleProjectImporter.getGradleHomeFile(env, sysprops));
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleHome(home);
}
}

@Test
public void testGradleArguments() {
List<String> arguments = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleArguments();
try {
Path rootPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().toPath();
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootPath);
assertTrue(build.getArguments().isEmpty());

JavaLanguageServerPlugin.getPreferencesManager().getPreferences()
.setGradleArguments(ImmutableList.of("-Pproperty=value", "--stacktrace"));
build = GradleProjectImporter.getBuildConfiguration(rootPath);
assertEquals(2, build.getArguments().size());
assertTrue(build.getArguments().contains("-Pproperty=value"));
assertTrue(build.getArguments().contains("--stacktrace"));
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleArguments(arguments);
}
}

@Test
public void testGradleJvmArguments() {
List<String> jvmArguments = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleJvmArguments();
try {
Path rootPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().toPath();
BuildConfiguration build = GradleProjectImporter.getBuildConfiguration(rootPath);
assertTrue(build.getJvmArguments().isEmpty());

JavaLanguageServerPlugin.getPreferencesManager().getPreferences()
.setGradleJvmArguments(ImmutableList.of("-Djavax.net.ssl.trustStore=truststore.jks"));
build = GradleProjectImporter.getBuildConfiguration(rootPath);
assertEquals(1, build.getJvmArguments().size());
assertTrue(build.getJvmArguments().contains("-Djavax.net.ssl.trustStore=truststore.jks"));
} finally {
JavaLanguageServerPlugin.getPreferencesManager().getPreferences().setGradleJvmArguments(jvmArguments);
}
}

@Test
public void testJava11Project() throws Exception {
IProject project = importGradleProject("gradle-11");
Expand Down

0 comments on commit 6fa7d16

Please sign in to comment.