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 project local maven settings #1673

Merged
merged 1 commit into from
Feb 15, 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 @@ -17,6 +17,7 @@
import static org.eclipse.m2e.core.internal.M2EUtils.copyProperties;

import java.io.File;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
Expand All @@ -25,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
Expand Down Expand Up @@ -73,6 +75,7 @@
import org.eclipse.m2e.core.embedder.IComponentLookup;
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
import org.eclipse.m2e.core.embedder.IMavenExecutionContext;
import org.eclipse.m2e.core.embedder.MavenSettingsLocations;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.internal.Messages;

Expand Down Expand Up @@ -149,32 +152,59 @@ protected MavenExecutionRequest newExecutionRequest() throws CoreException {
}
request = DefaultMavenExecutionRequest.copy(parent);
}
if(request == null) {
request = createExecutionRequest(IMavenConfiguration.getWorkspaceConfiguration(),
containerLookup, MavenPlugin.getMaven().getSettings());
request.setBaseDirectory(basedir);
if(request == null || !Objects.equals(request.getMultiModuleProjectDirectory(), multiModuleProjectDirectory)) {
IMavenConfiguration workspaceConfiguration = IMavenConfiguration.getWorkspaceConfiguration();
Optional<MavenProperties> mavenArgs;
try {
mavenArgs = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
} catch(IOException ex) {
throw new CoreException(Status.error("Loading maven.config failed!", ex));
}
MavenSettingsLocations mavenSettingsLocations = mavenArgs
.map(mavenCfg -> mavenCfg.getSettingsLocations(workspaceConfiguration))
.orElseGet(workspaceConfiguration::getSettingsLocations);
if(request == null) {
//create a fresh one ....
request = createExecutionRequest(workspaceConfiguration, containerLookup, mavenSettingsLocations);
request.setBaseDirectory(basedir);
} else {
//update existing configuration, we might have copied from an outer context here, but if the multi-module directory is different, we need to update some things...
Settings settings = MavenPlugin.getMaven().getSettings(mavenSettingsLocations);
File requestLocalRepositoryPath = request.getLocalRepositoryPath();
File settingsLocalRepositoryPath = getLocalRepositoryPath(settings);
if(!pathEquals(requestLocalRepositoryPath, settingsLocalRepositoryPath)) {
updateLocalRepository(request, settingsLocalRepositoryPath, containerLookup);
}
//TODO maybe also need to update user properties?!?
updateSettingsFiles(request, mavenSettingsLocations);
}
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);
}
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);

return request;
}

private boolean pathEquals(File file1, File file2) {
if(Objects.equals(file1, file2)) {
return true;
}
if(file1 != null && file2 != null) {
laeubi marked this conversation as resolved.
Show resolved Hide resolved
try {
return Objects.equals(file1.getCanonicalFile(), file2.getCanonicalFile());
} catch(IOException ex) {
//can't compare then...
}
}
return false;
}

static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenConfiguration, IComponentLookup lookup,
Settings settings) throws CoreException {
MavenSettingsLocations settingsLocations) throws CoreException {
MavenExecutionRequest request = new DefaultMavenExecutionRequest();

// this causes problems with unexpected "stale project configuration" error markers
// need to think how to manage ${maven.build.timestamp} properly inside workspace
//request.setStartTime( new Date() );

if(mavenConfiguration.getGlobalSettingsFile() != null) {
request.setGlobalSettingsFile(new File(mavenConfiguration.getGlobalSettingsFile()));
}
File userSettingsFile = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE;
if(mavenConfiguration.getUserSettingsFile() != null) {
userSettingsFile = new File(mavenConfiguration.getUserSettingsFile());
}
request.setUserSettingsFile(userSettingsFile);
Settings settings = MavenPlugin.getMaven().getSettings(settingsLocations);
updateSettingsFiles(request, settingsLocations);

//and settings are actually derived from IMavenConfiguration

Expand All @@ -183,26 +213,13 @@ static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenCon
userToolchainsFile = new File(mavenConfiguration.getUserToolchainsFile());
}
request.setUserToolchainsFile(userToolchainsFile);

try {
request = lookup.lookup(MavenExecutionRequestPopulator.class).populateFromSettings(request, settings);
} catch(MavenExecutionRequestPopulationException ex) {
throw new CoreException(Status.error(Messages.MavenImpl_error_no_exec_req, ex));
}

String localRepositoryPath = settings.getLocalRepository();
if(localRepositoryPath == null) {
localRepositoryPath = RepositorySystem.defaultUserLocalRepository.getAbsolutePath();
}

ArtifactRepository localRepository;
try {
localRepository = lookup.lookup(RepositorySystem.class).createLocalRepository(new File(localRepositoryPath));
} catch(InvalidRepositoryException ex) {
throw new AssertionError("Should never happen!", ex);
}
request.setLocalRepository(localRepository);
request.setLocalRepositoryPath(localRepository.getBasedir());
updateLocalRepository(request, getLocalRepositoryPath(settings), lookup);
request.setOffline(mavenConfiguration.isOffline());

request.getUserProperties().put("m2e.version", MavenPluginActivator.getVersion()); //$NON-NLS-1$
Expand All @@ -217,6 +234,39 @@ static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenCon
return request;
}

private static void updateSettingsFiles(MavenExecutionRequest request, MavenSettingsLocations settingsLocations) {
File global = settingsLocations.globalSettings();
if(global != null) {
request.setGlobalSettingsFile(global);
}
File userSettingsFile = settingsLocations.userSettings();
if(userSettingsFile == null) {
request.setUserSettingsFile(SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE);
} else {
request.setUserSettingsFile(userSettingsFile);
}
}

private static void updateLocalRepository(MavenExecutionRequest request, File localRepositoryPath,
IComponentLookup lookup) throws CoreException, AssertionError {
ArtifactRepository localRepository;
try {
localRepository = lookup.lookup(RepositorySystem.class).createLocalRepository(localRepositoryPath);
} catch(InvalidRepositoryException ex) {
throw new AssertionError("Should never happen!", ex);
}
request.setLocalRepository(localRepository);
request.setLocalRepositoryPath(localRepository.getBasedir());
}

private static File getLocalRepositoryPath(Settings settings) {
String localRepositoryPath = settings.getLocalRepository();
if(localRepositoryPath == null) {
return RepositorySystem.defaultUserLocalRepository;
}
return new File(localRepositoryPath);
}

@Override
public <V> V execute(ICallable<V> callable, IProgressMonitor monitor) throws CoreException {
return execute(null, callable, monitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
import org.apache.maven.cli.MavenCli;
import org.apache.maven.shared.utils.StringUtils;

import org.eclipse.m2e.core.embedder.IMavenConfiguration;
import org.eclipse.m2e.core.embedder.MavenSettingsLocations;


/**
* Holds Maven Runtime version properties.
Expand Down Expand Up @@ -319,6 +322,30 @@ public String getAlternateUserToolchainsFile() {
return null;
}

public MavenSettingsLocations getSettingsLocations(IMavenConfiguration configuration) {
MavenSettingsLocations configurationLocations = configuration.getSettingsLocations();
String alternateGlobalSettingsFile = getAlternateGlobalSettingsFile();
String alternateUserSettingsFile = getAlternateUserSettingsFile();
if(alternateGlobalSettingsFile == null && alternateUserSettingsFile == null) {
return configurationLocations;
}
File global;
File user;
//actually maven uses "the current working directory" but this does not make much sense in the context of m2e so we choose the root directory of the maven.config
File baseDir = configFile.getParentFile().getParentFile();
if(alternateGlobalSettingsFile == null) {
global = configurationLocations.globalSettings();
} else {
global = new File(baseDir, alternateGlobalSettingsFile);
}
if(alternateUserSettingsFile == null) {
user = configurationLocations.userSettings();
} else {
user = new File(baseDir, alternateUserSettingsFile);
}
return new MavenSettingsLocations(global, user);
}

public static void getCliProperty(String property, BiConsumer<String, String> consumer) {
int index = property.indexOf('=');
if(index <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@

import org.eclipse.m2e.core.embedder.IComponentLookup;
import org.eclipse.m2e.core.embedder.IMavenConfiguration;
import org.eclipse.m2e.core.internal.MavenPluginActivator;
import org.eclipse.m2e.core.internal.Messages;


Expand Down Expand Up @@ -343,12 +342,14 @@ protected void configure() {
container.setLookupRealm(null);
container.setLoggerManager(loggerManager);
thread.setContextClassLoader(container.getContainerRealm());
Optional<MavenProperties> mavenProperties = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
IMavenConfiguration workspaceConfiguration = IMavenConfiguration.getWorkspaceConfiguration();
MavenExecutionRequest request = MavenExecutionContext.createExecutionRequest(mavenConfiguration,
wrap(container), MavenPluginActivator.getDefault().getMaven().getSettings());
wrap(container), mavenProperties.map(mavenCfg -> mavenCfg.getSettingsLocations(workspaceConfiguration))
.orElseGet(workspaceConfiguration::getSettingsLocations));
container.lookup(MavenExecutionRequestPopulator.class).populateDefaults(request);
request.setBaseDirectory(multiModuleProjectDirectory);
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);
Optional<MavenProperties> mavenProperties = MavenProperties.getMavenArgs(multiModuleProjectDirectory);
Properties userProperties = request.getUserProperties();
mavenProperties.ifPresent(prop -> prop.getCliProperties(userProperties::setProperty));
BootstrapCoreExtensionManager resolver = container.lookup(BootstrapCoreExtensionManager.class);
Expand Down
Loading