Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support get and update active profiles of the Maven projects #3158

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions org.eclipse.jdt.ls.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@
<command
id="java.project.updateClassPaths">
</command>
<command
id="java.project.updateSettings">
</command>
<command
id="java.project.isTestFile">
</command>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
ProjectCommand.updateClasspaths(projectUri, entries.getClasspathEntries(), monitor);
return null;
}
case "java.project.updateSettings": {
String projectUri = (String) arguments.get(0);
Map<String, Object> 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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {

Expand All @@ -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";

/**
Expand All @@ -103,6 +108,7 @@ public class ProjectCommand {
* may not be equal to the output path of each source root.</li>
* <li>"org.eclipse.jdt.ls.core.referencedLibraries": Get all the referenced library files of the given Java project.</li>
* <li>"org.eclipse.jdt.ls.core.classpathEntries": Get all the classpath entries of the given Java project.</li>
* <li>"org.eclipse.m2e.core.selectedProfiles": Get the selected profiles of the given Maven project.</li>
* </ul>
* @return A <code>Map<string, string></code> with all the setting keys and
* their values.
Expand Down Expand Up @@ -191,6 +197,12 @@ public static Map<String, Object> getProjectSettings(String uri, List<String> 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;
Expand Down Expand Up @@ -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<String, Object> options) throws CoreException, URISyntaxException {
IJavaProject javaProject = getJavaProjectFromUri(projectUri);
IProject project = javaProject.getProject();
for (Map.Entry<String, Object> 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> settingKeys = Arrays.asList(KEY);
Map<String, Object> options = ProjectCommand.getProjectSettings(uriString, settingKeys);

assertEquals("", options.get(KEY));

Map<String, Object> 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();
Expand Down
Loading