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 maven.multiModuleProjectDirectory property #2976

Merged
merged 1 commit into from
Nov 24, 2023
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 @@ -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;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
List<String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@
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;
import static org.mockito.Mockito.reset;
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;
Expand Down Expand Up @@ -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<IPath> 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<String> 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;
Expand Down