From dbaa9be4b5ac6d0fc4322eb0597a5b708a7fd474 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Tue, 21 May 2024 09:46:58 +0800 Subject: [PATCH] Support get and update active profiles of the Maven projects (#3158) Signed-off-by: Sheng Chen --- org.eclipse.jdt.ls.core/plugin.xml | 3 ++ .../internal/JDTDelegateCommandHandler.java | 6 +++ .../internal/commands/ProjectCommand.java | 50 +++++++++++++++++++ .../internal/commands/ProjectCommandTest.java | 20 ++++++++ 4 files changed, 79 insertions(+) diff --git a/org.eclipse.jdt.ls.core/plugin.xml b/org.eclipse.jdt.ls.core/plugin.xml index 57d8c997f9..acb64067a5 100644 --- a/org.eclipse.jdt.ls.core/plugin.xml +++ b/org.eclipse.jdt.ls.core/plugin.xml @@ -85,6 +85,9 @@ + + diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTDelegateCommandHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTDelegateCommandHandler.java index 653225dd2a..19c331731a 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTDelegateCommandHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTDelegateCommandHandler.java @@ -105,6 +105,12 @@ public Object executeCommand(String commandId, List arguments, IProgress ProjectCommand.updateClasspaths(projectUri, entries.getClasspathEntries(), monitor); return null; } + case "java.project.updateSettings": { + String projectUri = (String) arguments.get(0); + Map options = JSONUtility.toModel(arguments.get(1), Map.class); + ProjectCommand.updateProjectSettings(projectUri, options); + return null; + } case "java.project.isTestFile": return ProjectCommand.isTestFile((String) arguments.get(0)); case "java.project.getAll": diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java index f4037b33f4..8cd2fc7cc0 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommand.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Optional; import java.util.Map.Entry; +import java.util.Objects; import java.util.stream.Stream; import org.eclipse.core.resources.IContainer; @@ -74,6 +75,9 @@ import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.SymbolInformation; +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.project.IProjectConfigurationManager; +import org.eclipse.m2e.core.project.ResolverConfiguration; public class ProjectCommand { @@ -83,6 +87,7 @@ public class ProjectCommand { public static final String OUTPUT_PATH = IConstants.PLUGIN_ID + ".outputPath"; public static final String CLASSPATH_ENTRIES = IConstants.PLUGIN_ID + ".classpathEntries"; public static final String REFERENCED_LIBRARIES = IConstants.PLUGIN_ID + ".referencedLibraries"; + public static final String M2E_SELECTED_PROFILES = "org.eclipse.m2e.core.selectedProfiles"; private static final String TEST_SCOPE_VALUE = "test"; /** @@ -103,6 +108,7 @@ public class ProjectCommand { * may not be equal to the output path of each source root. *
  • "org.eclipse.jdt.ls.core.referencedLibraries": Get all the referenced library files of the given Java project.
  • *
  • "org.eclipse.jdt.ls.core.classpathEntries": Get all the classpath entries of the given Java project.
  • + *
  • "org.eclipse.m2e.core.selectedProfiles": Get the selected profiles of the given Maven project.
  • * * @return A Map with all the setting keys and * their values. @@ -191,6 +197,12 @@ public static Map getProjectSettings(String uri, List se } settings.putIfAbsent(key, classpathEntries); break; + case M2E_SELECTED_PROFILES: + // Note, this is an experimental feature, the returned value might be changed. + IProjectConfigurationManager projectManager = MavenPlugin.getProjectConfigurationManager(); + ResolverConfiguration config = (ResolverConfiguration) projectManager.getProjectConfiguration(javaProject.getProject()); + settings.putIfAbsent(key, config.getSelectedProfiles()); + break; default: settings.putIfAbsent(key, javaProject.getOption(key, true)); break; @@ -698,4 +710,42 @@ public JdkUpdateResult(boolean success, String message) { this.message = message; } } + + /** + * Update the options of the given project. + * @param projectUri + * @param options + * @throws CoreException + * @throws URISyntaxException + */ + public static void updateProjectSettings(String projectUri, Map options) throws CoreException, URISyntaxException { + IJavaProject javaProject = getJavaProjectFromUri(projectUri); + IProject project = javaProject.getProject(); + for (Map.Entry entry : options.entrySet()) { + switch (entry.getKey()) { + case M2E_SELECTED_PROFILES: + IProjectConfigurationManager mavenProjectMgr = MavenPlugin.getProjectConfigurationManager(); + ResolverConfiguration config = (ResolverConfiguration) mavenProjectMgr.getProjectConfiguration(project); + String selectedProfiles = (String) entry.getValue(); + selectedProfiles = Arrays.stream(selectedProfiles.split(",")) + .map(String::trim) + .filter(s -> !s.isEmpty()) + .reduce((s1, s2) -> s1 + "," + s2) + .orElse(""); + if (Objects.equals(config.getSelectedProfiles(), selectedProfiles)) { + continue; + } + + config.setSelectedProfiles(selectedProfiles); + config.setResolveWorkspaceProjects(true); + boolean isSet = mavenProjectMgr.setResolverConfiguration(project, config); + if (isSet) { + JavaLanguageServerPlugin.getProjectsManager().updateProject(project, true); + } + break; + default: + break; + } + } + } } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java index 081f4b941e..1e02b261b3 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/commands/ProjectCommandTest.java @@ -447,6 +447,26 @@ public void testUpdateInvalidProjectJdk() throws Exception { assertFalse(updateProjectJdk.success); } + @Test + public void testUpdateMavenProfiles() throws Exception { + final String KEY = "org.eclipse.m2e.core.selectedProfiles"; + + importProjects("maven/salut"); + IProject project = WorkspaceHelper.getProject("salut"); + String uriString = project.getLocationURI().toString(); + List settingKeys = Arrays.asList(KEY); + Map options = ProjectCommand.getProjectSettings(uriString, settingKeys); + + assertEquals("", options.get(KEY)); + + Map updateOptions = new HashMap<>(); + updateOptions.put(KEY, "my profile"); + ProjectCommand.updateProjectSettings(uriString, updateOptions); + + options = ProjectCommand.getProjectSettings(uriString, settingKeys); + assertEquals("my profile", options.get(KEY)); + } + private SymbolInformation buildClassSymbol(IProject project, String fqClassName) throws Exception { String uriString = buildClassfileUri(project, fqClassName); SymbolInformation si = new SymbolInformation();