1717import static org .eclipse .m2e .core .internal .M2EUtils .copyProperties ;
1818
1919import java .io .File ;
20+ import java .io .IOException ;
2021import java .util .ArrayDeque ;
2122import java .util .Collections ;
2223import java .util .Deque ;
2526import java .util .List ;
2627import java .util .Map ;
2728import java .util .Objects ;
29+ import java .util .Optional ;
2830import java .util .Properties ;
2931import java .util .Set ;
3032import java .util .function .Function ;
7375import org .eclipse .m2e .core .embedder .IComponentLookup ;
7476import org .eclipse .m2e .core .embedder .IMavenConfiguration ;
7577import org .eclipse .m2e .core .embedder .IMavenExecutionContext ;
78+ import org .eclipse .m2e .core .embedder .MavenSettingsLocations ;
7679import org .eclipse .m2e .core .internal .MavenPluginActivator ;
7780import org .eclipse .m2e .core .internal .Messages ;
7881
@@ -149,60 +152,73 @@ protected MavenExecutionRequest newExecutionRequest() throws CoreException {
149152 }
150153 request = DefaultMavenExecutionRequest .copy (parent );
151154 }
152- if (request == null ) {
153- request = createExecutionRequest (IMavenConfiguration .getWorkspaceConfiguration (),
154- containerLookup , MavenPlugin .getMaven ().getSettings ());
155- request .setBaseDirectory (basedir );
155+ if (request == null || !Objects .equals (request .getMultiModuleProjectDirectory (), multiModuleProjectDirectory )) {
156+ IMavenConfiguration workspaceConfiguration = IMavenConfiguration .getWorkspaceConfiguration ();
157+ Optional <MavenProperties > mavenArgs ;
158+ try {
159+ mavenArgs = MavenProperties .getMavenArgs (multiModuleProjectDirectory );
160+ } catch (IOException ex ) {
161+ throw new CoreException (Status .error ("Loading maven.config failed!" , ex ));
162+ }
163+ MavenSettingsLocations mavenSettingsLocations = mavenArgs
164+ .map (mavenCfg -> mavenCfg .getSettingsLocations (workspaceConfiguration ))
165+ .orElseGet (workspaceConfiguration ::getSettingsLocations );
166+ if (request == null ) {
167+ //create a fresh one ....
168+ request = createExecutionRequest (workspaceConfiguration , containerLookup , mavenSettingsLocations ,
169+ multiModuleProjectDirectory );
170+ request .setBaseDirectory (basedir );
171+ } else {
172+ //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...
173+ Settings settings = MavenPlugin .getMaven ().getSettings (mavenSettingsLocations );
174+ File requestLocalRepositoryPath = request .getLocalRepositoryPath ();
175+ File settingsLocalRepositoryPath = getLocalRepositoryPath (settings , multiModuleProjectDirectory );
176+ if (!pathEquals (requestLocalRepositoryPath , settingsLocalRepositoryPath )) {
177+ updateLocalRepository (request , settingsLocalRepositoryPath , containerLookup );
178+ }
179+ //TODO maybe also need to update user properties?!?
180+ updateSettingsFiles (request , mavenSettingsLocations );
181+ request .setMultiModuleProjectDirectory (multiModuleProjectDirectory );
182+ }
156183 }
157- request .setMultiModuleProjectDirectory (multiModuleProjectDirectory );
158-
159184 return request ;
160185 }
161186
187+ private boolean pathEquals (File file1 , File file2 ) {
188+ if (Objects .equals (file1 , file2 )) {
189+ return true ;
190+ }
191+ if (file1 != null && file2 != null ) {
192+ try {
193+ return Objects .equals (file1 .getCanonicalFile (), file2 .getCanonicalFile ());
194+ } catch (IOException ex ) {
195+ //can't compare then...
196+ }
197+ }
198+ return false ;
199+ }
200+
162201 static MavenExecutionRequest createExecutionRequest (IMavenConfiguration mavenConfiguration , IComponentLookup lookup ,
163- Settings settings ) throws CoreException {
202+ MavenSettingsLocations settingsLocations , File multiModuleProjectDirectory ) throws CoreException {
164203 MavenExecutionRequest request = new DefaultMavenExecutionRequest ();
165-
166204 // this causes problems with unexpected "stale project configuration" error markers
167205 // need to think how to manage ${maven.build.timestamp} properly inside workspace
168206 //request.setStartTime( new Date() );
169-
170- if (mavenConfiguration .getGlobalSettingsFile () != null ) {
171- request .setGlobalSettingsFile (new File (mavenConfiguration .getGlobalSettingsFile ()));
172- }
173- File userSettingsFile = SettingsXmlConfigurationProcessor .DEFAULT_USER_SETTINGS_FILE ;
174- if (mavenConfiguration .getUserSettingsFile () != null ) {
175- userSettingsFile = new File (mavenConfiguration .getUserSettingsFile ());
176- }
177- request .setUserSettingsFile (userSettingsFile );
178-
207+ Settings settings = MavenPlugin .getMaven ().getSettings (settingsLocations );
208+ updateSettingsFiles (request , settingsLocations );
179209 //and settings are actually derived from IMavenConfiguration
180-
181210 File userToolchainsFile = MavenCli .DEFAULT_USER_TOOLCHAINS_FILE ;
182211 if (mavenConfiguration .getUserToolchainsFile () != null ) {
183212 userToolchainsFile = new File (mavenConfiguration .getUserToolchainsFile ());
184213 }
185214 request .setUserToolchainsFile (userToolchainsFile );
186-
187215 try {
188216 request = lookup .lookup (MavenExecutionRequestPopulator .class ).populateFromSettings (request , settings );
189217 } catch (MavenExecutionRequestPopulationException ex ) {
190218 throw new CoreException (Status .error (Messages .MavenImpl_error_no_exec_req , ex ));
191219 }
192220
193- String localRepositoryPath = settings .getLocalRepository ();
194- if (localRepositoryPath == null ) {
195- localRepositoryPath = RepositorySystem .defaultUserLocalRepository .getAbsolutePath ();
196- }
197-
198- ArtifactRepository localRepository ;
199- try {
200- localRepository = lookup .lookup (RepositorySystem .class ).createLocalRepository (new File (localRepositoryPath ));
201- } catch (InvalidRepositoryException ex ) {
202- throw new AssertionError ("Should never happen!" , ex );
203- }
204- request .setLocalRepository (localRepository );
205- request .setLocalRepositoryPath (localRepository .getBasedir ());
221+ updateLocalRepository (request , getLocalRepositoryPath (settings , multiModuleProjectDirectory ), lookup );
206222 request .setOffline (mavenConfiguration .isOffline ());
207223
208224 request .getUserProperties ().put ("m2e.version" , MavenPluginActivator .getVersion ()); //$NON-NLS-1$
@@ -214,9 +230,45 @@ static MavenExecutionRequest createExecutionRequest(IMavenConfiguration mavenCon
214230 request .setCacheTransferError (true );
215231
216232 request .setGlobalChecksumPolicy (mavenConfiguration .getGlobalChecksumPolicy ());
233+ request .setMultiModuleProjectDirectory (multiModuleProjectDirectory );
217234 return request ;
218235 }
219236
237+ private static void updateSettingsFiles (MavenExecutionRequest request , MavenSettingsLocations settingsLocations ) {
238+ File global = settingsLocations .globalSettings ();
239+ if (global != null ) {
240+ request .setGlobalSettingsFile (global );
241+ }
242+ File userSettingsFile = settingsLocations .userSettings ();
243+ if (userSettingsFile == null ) {
244+ request .setUserSettingsFile (SettingsXmlConfigurationProcessor .DEFAULT_USER_SETTINGS_FILE );
245+ } else {
246+ request .setUserSettingsFile (userSettingsFile );
247+ }
248+ }
249+
250+ private static void updateLocalRepository (MavenExecutionRequest request , File localRepositoryPath ,
251+ IComponentLookup lookup ) throws CoreException , AssertionError {
252+ ArtifactRepository localRepository ;
253+ try {
254+ localRepository = lookup .lookup (RepositorySystem .class ).createLocalRepository (localRepositoryPath );
255+ } catch (InvalidRepositoryException ex ) {
256+ throw new AssertionError ("Should never happen!" , ex );
257+ }
258+ request .setLocalRepository (localRepository );
259+ request .setLocalRepositoryPath (localRepository .getBasedir ());
260+ }
261+
262+ private static File getLocalRepositoryPath (Settings settings , File multiModuleProjectDirectory ) {
263+ String localRepositoryPath = settings .getLocalRepository ();
264+ if (localRepositoryPath == null ) {
265+ return RepositorySystem .defaultUserLocalRepository ;
266+ }
267+ //Actually maven would resolve these against the current working directory,
268+ //as we have no such thing available the best we can use here is the root folder of the multimodule directory
269+ return new File (multiModuleProjectDirectory , localRepositoryPath ).getAbsoluteFile ();
270+ }
271+
220272 @ Override
221273 public <V > V execute (ICallable <V > callable , IProgressMonitor monitor ) throws CoreException {
222274 return execute (null , callable , monitor );
0 commit comments