From 12e31994b37b3d69109d80f4cd296eec0355ac95 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Fri, 5 Jul 2019 11:08:41 +0100 Subject: [PATCH] Add additional Gradle preferences Signed-off-by: Nicholas Gates --- .gitignore | 4 +- .../managers/GradleProjectImporter.java | 10 ++- .../internal/preferences/Preferences.java | 52 ++++++++++- .../managers/GradleProjectImporterTest.java | 90 ++++++++++++++----- 4 files changed, 131 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index a5ff17a010..968a524709 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ target/ bin/ **/lib/ -maven-wrapper.jar \ No newline at end of file +maven-wrapper.jar +*.iml +.idea diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java index 124668ca56..f81299d5bb 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java @@ -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; @@ -131,6 +132,9 @@ public static File getGradleHomeFile() { } public static File getGradleHomeFile(Map 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); @@ -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 gradleArguments = JavaLanguageServerPlugin.getPreferencesManager() != null ? JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleArguments() : new ArrayList<>(); + List 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; diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java index 7f6a8022e9..455337a49a 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java @@ -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. */ @@ -288,6 +299,9 @@ public class Preferences { private boolean importGradleEnabled; private boolean gradleWrapperEnabled; private String gradleVersion; + private List gradleArguments; + private List gradleJvmArguments; + private String gradleHome; private boolean importMavenEnabled; private boolean mavenDownloadSources; private boolean implementationsCodeLensEnabled; @@ -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; @@ -453,6 +470,12 @@ public static Preferences createFrom(Map configuration) { prefs.setGradleWrapperEnabled(gradleWrapperEnabled); String gradleVersion = getString(configuration, GRADLE_VERSION); prefs.setGradleVersion(gradleVersion); + List gradleArguments = getList(configuration, GRADLE_ARGUMENTS); + prefs.setGradleArguments(gradleArguments); + List 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); @@ -562,6 +585,21 @@ public Preferences setGradleVersion(String gradleVersion) { return this; } + public Preferences setGradleArguments(List arguments) { + this.gradleArguments = arguments == null ? new ArrayList<>() : arguments; + return this; + } + + public Preferences setGradleJvmArguments(List 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; @@ -775,6 +813,18 @@ public String getGradleVersion() { return gradleVersion; } + public List getGradleArguments() { + return gradleArguments == null ? new ArrayList<>() : gradleArguments; + } + + public List getGradleJvmArguments() { + return gradleJvmArguments == null ? new ArrayList<>() : gradleJvmArguments; + } + + public String getGradleHome() { + return gradleHome; + } + public String getFormatterUrl() { return formatterUrl; } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java index 548cb906fb..47209cbfc7 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporterTest.java @@ -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; @@ -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 * @@ -187,7 +180,7 @@ public void testWorkspaceSettings() throws Exception { } @Test - public void testGradleUserHome() throws Exception { + public void testGradleUserHome() { Map env = new HashMap<>(); Properties sysprops = new Properties(); File file = null; @@ -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) { @@ -240,6 +233,59 @@ public void testGradlePropertiesFile() throws Exception { assertTrue(projectsManager.isBuildFile(file)); } + @Test + public void testGradleHomePreference() { + String home = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleHome(); + Map 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 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 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");