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

Add support for externally provided 'lifecycle-mapping-metadata.xml' file #3005

Merged
merged 1 commit into from
Jan 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
Expand Down Expand Up @@ -73,6 +74,7 @@
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.MessageType;
import org.osgi.framework.Bundle;

/**
* Preferences model
Expand Down Expand Up @@ -314,6 +316,11 @@ public class Preferences {
*/
public static final String MAVEN_GLOBAL_SETTINGS_KEY = "java.configuration.maven.globalSettings";

/**
* Preference key for Maven lifecycle mappings xml location.
*/
public static final String MAVEN_LIFECYCLE_MAPPINGS_KEY = "java.configuration.maven.lifecycleMappings";

public static final String MAVEN_NOT_COVERED_PLUGIN_EXECUTION_SEVERITY = "java.configuration.maven.notCoveredPluginExecutionSeverity";

public static final String MAVEN_DEFAULT_MOJO_EXECUTION_ACTION = "java.configuration.maven.defaultMojoExecutionAction";
Expand Down Expand Up @@ -504,6 +511,8 @@ public class Preferences {
public static final String JAVA_COMPILE_NULLANALYSIS_NONNULLBYDEFAULT = "java.compile.nullAnalysis.nonnullbydefault";
public static final String JAVA_COMPILE_NULLANALYSIS_MODE = "java.compile.nullAnalysis.mode";

public static final String LIFECYCLE_MAPPING_METADATA_SOURCE_NAME = "lifecycle-mapping-metadata.xml";

/**
* Preference key for list of cleanups to run on save
*/
Expand Down Expand Up @@ -632,6 +641,7 @@ public class Preferences {

private String mavenUserSettings;
private String mavenGlobalSettings;
private String mavenLifecycleMappings;
private String mavenNotCoveredPluginExecutionSeverity;
private String mavenDefaultMojoExecutionAction;

Expand Down Expand Up @@ -1142,6 +1152,9 @@ public static Preferences createFrom(Map<String, Object> configuration) {
String mavenGlobalSettings = getString(configuration, MAVEN_GLOBAL_SETTINGS_KEY, null);
prefs.setMavenGlobalSettings(mavenGlobalSettings);

String mavenLifecycleMappings = getString(configuration, MAVEN_LIFECYCLE_MAPPINGS_KEY, null);
prefs.setMavenLifecycleMappings(mavenLifecycleMappings);

String mavenNotCoveredPluginExecution = getString(configuration, MAVEN_NOT_COVERED_PLUGIN_EXECUTION_SEVERITY, IGNORE);
prefs.setMavenNotCoveredPluginExecutionSeverity(mavenNotCoveredPluginExecution);

Expand Down Expand Up @@ -1913,6 +1926,22 @@ public String getMavenGlobalSettings() {
return mavenGlobalSettings;
}

public Preferences setMavenLifecycleMappings(String mavenLifecycleMappings) {
if (mavenLifecycleMappings == null || mavenLifecycleMappings.isBlank()) {
Bundle bundle = Platform.getBundle("org.eclipse.m2e.core");
if (bundle != null) {
IPath stateLocation = Platform.getStateLocation(bundle);
mavenLifecycleMappings = stateLocation.append(LIFECYCLE_MAPPING_METADATA_SOURCE_NAME).toString();
}
}
this.mavenLifecycleMappings = ResourceUtils.expandPath(mavenLifecycleMappings);
return this;
}

public String getMavenLifecycleMappings() {
return mavenLifecycleMappings;
}

public String getMavenNotCoveredPluginExecutionSeverity() {
return mavenNotCoveredPluginExecutionSeverity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.embedder.MavenProperties;
import org.eclipse.m2e.core.internal.lifecyclemapping.LifecycleMappingFactory;
import org.eclipse.m2e.core.internal.preferences.MavenConfigurationImpl;
import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants;
import org.eclipse.m2e.core.internal.preferences.ProblemSeverity;
Expand Down Expand Up @@ -83,25 +84,40 @@ public static void initializeMavenPreferences() {
public void update(Preferences preferences) {
super.update(preferences);

boolean updateMavenProjects = false;
String newMavenSettings = preferences.getMavenUserSettings();
String oldMavenSettings = getMavenConfiguration().getUserSettingsFile();
if (!Objects.equals(newMavenSettings, oldMavenSettings)) {
try {
getMavenConfiguration().setUserSettingsFile(newMavenSettings);
updateMavenProjects = true;
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("failed to set Maven settings", e);
preferences.setMavenUserSettings(oldMavenSettings);
}
}
boolean updateMavenProjects = false;
String newMavenGlobalSettings = preferences.getMavenGlobalSettings();
String oldMavenGlobalSettings = getMavenConfiguration().getGlobalSettingsFile();
if (!Objects.equals(newMavenGlobalSettings, oldMavenGlobalSettings)) {
try {
getMavenConfiguration().setGlobalSettingsFile(newMavenGlobalSettings);
updateMavenProjects = true;
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("failed to set Maven global settings", e);
preferences.setMavenUserSettings(oldMavenSettings);
preferences.setMavenGlobalSettings(oldMavenGlobalSettings);
}
}
String newMavenLifecycleMappings = preferences.getMavenLifecycleMappings();
String oldMavenLifecycleMappings = getMavenConfiguration().getWorkspaceLifecycleMappingMetadataFile();
if (!Objects.equals(newMavenLifecycleMappings, oldMavenLifecycleMappings)) {
try {
getMavenConfiguration().setWorkspaceLifecycleMappingMetadataFile(newMavenLifecycleMappings);
// reloads lifcycle mapping. See org.eclipse.m2e.core.ui.internal.preferences.LifecycleMappingPreferencePage.performOK()
LifecycleMappingFactory.getWorkspaceMetadata(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this line do and why is it needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then you should consider adding a comment to clarify the purpose of this code. Without it, other maintainers may find it hard to understand what it does from the method call alone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

updateMavenProjects = true;
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("failed to set Maven lifecycle mappings", e);
preferences.setMavenLifecycleMappings(oldMavenLifecycleMappings);
}
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
import org.apache.maven.settings.Settings;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jdt.core.manipulation.JavaManipulation;
Expand All @@ -51,7 +53,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.osgi.framework.Bundle;

@RunWith(MockitoJUnitRunner.class)
public class PreferenceManagerTest {
Expand Down Expand Up @@ -116,6 +120,46 @@ public void testUpdateMavenGlobalSettings() throws Exception {
verify(mavenConfig).setGlobalSettingsFile(null);
}

@Test
public void testUpdateMavenLifecycleMappings() throws Exception {
String path = "/foo/bar.xml";
Preferences preferences = Preferences.createFrom(Collections.singletonMap(Preferences.MAVEN_LIFECYCLE_MAPPINGS_KEY, path));
preferenceManager.update(preferences);
verify(mavenConfig).setWorkspaceLifecycleMappingMetadataFile(path);

//check setting the same path doesn't call Maven's config update
reset(mavenConfig);
when(mavenConfig.getWorkspaceLifecycleMappingMetadataFile()).thenReturn(path);
when(mavenConfig.getNotCoveredMojoExecutionSeverity()).thenReturn("ignore");
preferenceManager.update(preferences);
verify(mavenConfig, never()).setGlobalSettingsFile(anyString());

//check setting null is allowed
reset(mavenConfig);
Mockito.lenient().when(mavenConfig.getWorkspaceLifecycleMappingMetadataFile()).thenReturn(path);
IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(IMavenConstants.PLUGIN_ID);
String oldMappings = prefs.get(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION, null);
try {
prefs.put(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION, path);
when(mavenConfig.getNotCoveredMojoExecutionSeverity()).thenReturn("ignore");
String defaultPath = null;
Bundle bundle = Platform.getBundle("org.eclipse.m2e.core");
if (bundle != null) {
IPath stateLocation = Platform.getStateLocation(bundle);
defaultPath = stateLocation.append(Preferences.LIFECYCLE_MAPPING_METADATA_SOURCE_NAME).toString();
}
preferences.setMavenLifecycleMappings(null);
preferenceManager.update(preferences);
verify(mavenConfig).setWorkspaceLifecycleMappingMetadataFile(defaultPath);
} finally {
if (oldMappings == null) {
prefs.remove(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION);
} else {
prefs.put(MavenPreferenceConstants.P_WORKSPACE_MAPPINGS_LOCATION, oldMappings);
}
}
}

@Test
public void testInitialize() throws Exception {
PreferenceManager.initialize();
Expand Down