From 72dcd5024389b82c0d19f6e0c112864f4a9626d3 Mon Sep 17 00:00:00 2001 From: Snjezana Peco Date: Thu, 21 Dec 2023 20:10:19 +0100 Subject: [PATCH] Add support for externally provided 'lifecycle-mapping-metadata.xml' file Signed-off-by: Snjezana Peco --- .../internal/preferences/Preferences.java | 29 ++++++++++++ .../StandardPreferenceManager.java | 20 ++++++++- .../preferences/PreferenceManagerTest.java | 44 +++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) 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 48b5b8ace8..d906360f9b 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 @@ -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; @@ -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 @@ -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"; @@ -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 */ @@ -632,6 +641,7 @@ public class Preferences { private String mavenUserSettings; private String mavenGlobalSettings; + private String mavenLifecycleMappings; private String mavenNotCoveredPluginExecutionSeverity; private String mavenDefaultMojoExecutionAction; @@ -1142,6 +1152,9 @@ public static Preferences createFrom(Map 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); @@ -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; } diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/StandardPreferenceManager.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/StandardPreferenceManager.java index 38e84bdb34..d518c45a0f 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/StandardPreferenceManager.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/StandardPreferenceManager.java @@ -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; @@ -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); + updateMavenProjects = true; + } catch (CoreException e) { + JavaLanguageServerPlugin.logException("failed to set Maven lifecycle mappings", e); + preferences.setMavenLifecycleMappings(oldMavenLifecycleMappings); } } try { diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManagerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManagerTest.java index 3a50042caa..32bd377a88 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManagerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManagerTest.java @@ -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; @@ -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 { @@ -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();