Skip to content

Commit

Permalink
Resolve the local repository path relative to the multi module directory
Browse files Browse the repository at this point in the history
If a user specify a relative path in the settings.xml this currently
leads a bit to unspecified behavior. Java would then resolve it to the
current working directory what might give desired results on the
commandline (but sometimes also unexpected ones) but in the IDE it is
not really controllable.

This now resolves the local repository path against the multi module
directory what is the only user controllable constant directory we can
offer here at the moment.
  • Loading branch information
laeubi committed Feb 17, 2024
1 parent 4568238 commit 94fa546
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,21 @@ protected MavenExecutionRequest newExecutionRequest() throws CoreException {
.orElseGet(workspaceConfiguration::getSettingsLocations);
if(request == null) {
//create a fresh one ....
request = createExecutionRequest(workspaceConfiguration, containerLookup, mavenSettingsLocations);
request = createExecutionRequest(workspaceConfiguration, containerLookup, mavenSettingsLocations,
multiModuleProjectDirectory);
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);
File settingsLocalRepositoryPath = getLocalRepositoryPath(settings, multiModuleProjectDirectory);
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;
}
Expand All @@ -198,16 +199,14 @@ private boolean pathEquals(File file1, File file2) {
}

static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenConfiguration, IComponentLookup lookup,
MavenSettingsLocations settingsLocations) throws CoreException {
MavenSettingsLocations settingsLocations, File multiModuleProjectDirectory) 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() );
Settings settings = MavenPlugin.getMaven().getSettings(settingsLocations);
updateSettingsFiles(request, settingsLocations);

//and settings are actually derived from IMavenConfiguration

File userToolchainsFile = MavenCli.DEFAULT_USER_TOOLCHAINS_FILE;
if(mavenConfiguration.getUserToolchainsFile() != null) {
userToolchainsFile = new File(mavenConfiguration.getUserToolchainsFile());
Expand All @@ -219,7 +218,7 @@ static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenCon
throw new CoreException(Status.error(Messages.MavenImpl_error_no_exec_req, ex));
}

updateLocalRepository(request, getLocalRepositoryPath(settings), lookup);
updateLocalRepository(request, getLocalRepositoryPath(settings, multiModuleProjectDirectory), lookup);
request.setOffline(mavenConfiguration.isOffline());

request.getUserProperties().put("m2e.version", MavenPluginActivator.getVersion()); //$NON-NLS-1$
Expand All @@ -231,6 +230,7 @@ static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenCon
request.setCacheTransferError(true);

request.setGlobalChecksumPolicy(mavenConfiguration.getGlobalChecksumPolicy());
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);
return request;
}

Expand Down Expand Up @@ -259,12 +259,14 @@ private static void updateLocalRepository(MavenExecutionRequest request, File lo
request.setLocalRepositoryPath(localRepository.getBasedir());
}

private static File getLocalRepositoryPath(Settings settings) {
private static File getLocalRepositoryPath(Settings settings, File multiModuleProjectDirectory) {
String localRepositoryPath = settings.getLocalRepository();
if(localRepositoryPath == null) {
return RepositorySystem.defaultUserLocalRepository;
}
return new File(localRepositoryPath);
//Actually maven would resolve these against the current working directory,
//as we have no such thing available the best we can use here is the root folder of the multimodule directory
return new File(multiModuleProjectDirectory, localRepositoryPath).getAbsoluteFile();

This comment has been minimized.

Copy link
@ahoehma
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ protected void configure() {
IMavenConfiguration workspaceConfiguration = IMavenConfiguration.getWorkspaceConfiguration();
MavenExecutionRequest request = MavenExecutionContext.createExecutionRequest(mavenConfiguration,
wrap(container), mavenProperties.map(mavenCfg -> mavenCfg.getSettingsLocations(workspaceConfiguration))
.orElseGet(workspaceConfiguration::getSettingsLocations));
.orElseGet(workspaceConfiguration::getSettingsLocations),
multiModuleProjectDirectory);
container.lookup(MavenExecutionRequestPopulator.class).populateDefaults(request);
request.setBaseDirectory(multiModuleProjectDirectory);
request.setMultiModuleProjectDirectory(multiModuleProjectDirectory);
Expand Down

0 comments on commit 94fa546

Please sign in to comment.