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 2eadac324a..68ef01dabb 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 @@ -12,6 +12,8 @@ *******************************************************************************/ package org.eclipse.jdt.ls.core.internal.preferences; +import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Objects; @@ -23,6 +25,7 @@ import org.eclipse.core.resources.IWorkspaceDescription; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; @@ -34,6 +37,7 @@ import org.eclipse.m2e.core.MavenPlugin; 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.preferences.MavenConfigurationImpl; import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants; import org.eclipse.m2e.core.internal.preferences.ProblemSeverity; @@ -51,6 +55,7 @@ public class StandardPreferenceManager extends PreferenceManager { private static final String JAVALS_PROFILE = "javals.profile"; public static final String M2E_DISABLE_TEST_CLASSPATH_FLAG = "m2e.disableTestClasspathFlag"; private static final String M2E_APT_ID = MavenJdtAptPlugin.PLUGIN_ID; + public static final String MAVEN_MULTI_MODULE_PROJECT_DIRECTORY = "maven.multiModuleProjectDirectory"; private IMavenConfiguration mavenConfig; public StandardPreferenceManager() { @@ -102,24 +107,52 @@ public void update(Preferences preferences) { try { Settings mavenSettings = MavenPlugin.getMaven().getSettings(); boolean oldDisableTest = false; + String multiModuleProjectDirectory = null; + String systemMmpd = System.getProperty(MAVEN_MULTI_MODULE_PROJECT_DIRECTORY); List activeProfilesIds = mavenSettings.getActiveProfiles(); for (org.apache.maven.settings.Profile settingsProfile : mavenSettings.getProfiles()) { if ((settingsProfile.getActivation() != null && settingsProfile.getActivation().isActiveByDefault()) || activeProfilesIds.contains(settingsProfile.getId())) { if (TRUE.equals(settingsProfile.getProperties().get(M2E_DISABLE_TEST_CLASSPATH_FLAG))) { oldDisableTest = true; + } + if (systemMmpd != null) { + Object mmpd = settingsProfile.getProperties().get(MAVEN_MULTI_MODULE_PROJECT_DIRECTORY); + if (mmpd instanceof String s) { + multiModuleProjectDirectory = s; + } + } + if (oldDisableTest && multiModuleProjectDirectory != null) { break; } } } - if (oldDisableTest != preferences.isMavenDisableTestClasspathFlag()) { + if ((systemMmpd == null && multiModuleProjectDirectory == null) || (oldDisableTest != preferences.isMavenDisableTestClasspathFlag())) { mavenSettings.getProfiles().removeIf(p -> JAVALS_PROFILE.equals(p.getId())); - if (preferences.isMavenDisableTestClasspathFlag()) { + if (preferences.isMavenDisableTestClasspathFlag() || ((systemMmpd == null && multiModuleProjectDirectory == null))) { Profile profile = new Profile(); profile.setId(JAVALS_PROFILE); Activation activation = new Activation(); activation.setActiveByDefault(true); profile.setActivation(activation); profile.getProperties().put(M2E_DISABLE_TEST_CLASSPATH_FLAG, String.valueOf(preferences.isMavenDisableTestClasspathFlag())); + if (preferences.getRootPaths() != null) { + for (IPath path : preferences.getRootPaths()) { + File f = MavenProperties.computeMultiModuleProjectDirectory(path.toFile()); + if (f != null) { + try { + multiModuleProjectDirectory = f.getCanonicalPath(); + } catch (IOException e) { + multiModuleProjectDirectory = f.getAbsolutePath(); + } + break; + } + } + } + if (multiModuleProjectDirectory != null) { + profile.getProperties().put(MAVEN_MULTI_MODULE_PROJECT_DIRECTORY, multiModuleProjectDirectory); + } else { + profile.getProperties().remove(MAVEN_MULTI_MODULE_PROJECT_DIRECTORY); + } mavenSettings.addProfile(profile); } for (IProject project : ProjectUtils.getAllProjects()) { 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 b7ea36d54d..3a50042caa 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 @@ -15,6 +15,7 @@ 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.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.never; @@ -22,13 +23,18 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.io.File; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import org.apache.commons.io.FileUtils; import org.apache.maven.settings.Settings; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.jdt.core.JavaCore; @@ -238,6 +244,46 @@ public void testMavenDisableTestFlag() throws Exception { } } + @Test + public void testMavenMultipleModuleProjectDirectory() throws Exception { + try { + PreferenceManager.initialize(); + Preferences preferences = new Preferences(); + preferenceManager.update(preferences); + assertNull(getMultipleModuleProjectDirectory()); + preferences = new Preferences(); + Collection rootPaths = new ArrayList<>(); + File dir = new File("target", "workingProjects"); + dir = new File(dir, "test"); + File dotMvn = new File(dir, ".mvn"); + FileUtils.forceMkdir(dotMvn); + IPath rootPath = new org.eclipse.core.runtime.Path(dir.getAbsolutePath()); + rootPaths.add(rootPath); + preferences.setRootPaths(rootPaths); + preferenceManager.update(preferences); + assertEquals(dir.getAbsolutePath(), getMultipleModuleProjectDirectory()); + } finally { + Preferences preferences = new Preferences(); + preferenceManager.update(preferences); + assertNull(getMultipleModuleProjectDirectory()); + } + } + + private String getMultipleModuleProjectDirectory() throws CoreException { + Settings mavenSettings = MavenPlugin.getMaven().getSettings(); + String multipleModuleProjectDirectory = null; + List activeProfilesIds = mavenSettings.getActiveProfiles(); + for (org.apache.maven.settings.Profile settingsProfile : mavenSettings.getProfiles()) { + if ((settingsProfile.getActivation() != null && settingsProfile.getActivation().isActiveByDefault()) || activeProfilesIds.contains(settingsProfile.getId())) { + if (settingsProfile.getProperties().get(StandardPreferenceManager.MAVEN_MULTI_MODULE_PROJECT_DIRECTORY) instanceof String s) { + multipleModuleProjectDirectory = s; + break; + } + } + } + return multipleModuleProjectDirectory; + } + private boolean getDisableTestFlag() throws CoreException { Settings mavenSettings = MavenPlugin.getMaven().getSettings(); boolean disableTest = false;