View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.HashMap;
32  import java.util.HashSet;
33  import java.util.LinkedHashMap;
34  import java.util.LinkedHashSet;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Set;
38  import java.util.function.Function;
39  import java.util.stream.Stream;
40  
41  import org.apache.maven.api.MonotonicClock;
42  import org.apache.maven.api.Session;
43  import org.apache.maven.api.model.Model;
44  import org.apache.maven.api.model.Prerequisites;
45  import org.apache.maven.api.model.Profile;
46  import org.apache.maven.api.services.Lookup;
47  import org.apache.maven.api.services.LookupException;
48  import org.apache.maven.artifact.ArtifactUtils;
49  import org.apache.maven.execution.BuildResumptionAnalyzer;
50  import org.apache.maven.execution.BuildResumptionDataRepository;
51  import org.apache.maven.execution.BuildResumptionPersistenceException;
52  import org.apache.maven.execution.DefaultMavenExecutionResult;
53  import org.apache.maven.execution.ExecutionEvent;
54  import org.apache.maven.execution.MavenExecutionRequest;
55  import org.apache.maven.execution.MavenExecutionResult;
56  import org.apache.maven.execution.MavenSession;
57  import org.apache.maven.execution.ProfileActivation;
58  import org.apache.maven.execution.ProjectActivation;
59  import org.apache.maven.execution.ProjectDependencyGraph;
60  import org.apache.maven.graph.GraphBuilder;
61  import org.apache.maven.graph.ProjectSelector;
62  import org.apache.maven.internal.impl.DefaultSessionFactory;
63  import org.apache.maven.internal.impl.InternalMavenSession;
64  import org.apache.maven.lifecycle.LifecycleExecutionException;
65  import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
66  import org.apache.maven.lifecycle.internal.LifecycleStarter;
67  import org.apache.maven.model.building.ModelProblem;
68  import org.apache.maven.model.building.Result;
69  import org.apache.maven.model.superpom.SuperPomProvider;
70  import org.apache.maven.plugin.LegacySupport;
71  import org.apache.maven.project.MavenProject;
72  import org.apache.maven.resolver.MavenChainedWorkspaceReader;
73  import org.apache.maven.resolver.RepositorySystemSessionFactory;
74  import org.apache.maven.session.scope.internal.SessionScope;
75  import org.eclipse.aether.RepositorySystemSession;
76  import org.eclipse.aether.RepositorySystemSession.CloseableSession;
77  import org.eclipse.aether.repository.WorkspaceReader;
78  import org.eclipse.sisu.Nullable;
79  import org.slf4j.Logger;
80  import org.slf4j.LoggerFactory;
81  import org.slf4j.helpers.MessageFormatter;
82  
83  import static java.util.stream.Collectors.toSet;
84  
85  /**
86   */
87  @Named
88  @Singleton
89  public class DefaultMaven implements Maven {
90      private final Logger logger = LoggerFactory.getLogger(getClass());
91  
92      private final Lookup lookup;
93  
94      private final ExecutionEventCatapult eventCatapult;
95  
96      private final LegacySupport legacySupport;
97  
98      private final SessionScope sessionScope;
99  
100     private final RepositorySystemSessionFactory repositorySessionFactory;
101 
102     private final GraphBuilder graphBuilder;
103 
104     private final BuildResumptionAnalyzer buildResumptionAnalyzer;
105 
106     private final BuildResumptionDataRepository buildResumptionDataRepository;
107 
108     private final SuperPomProvider superPomProvider;
109 
110     private final DefaultSessionFactory defaultSessionFactory;
111 
112     private final WorkspaceReader ideWorkspaceReader;
113 
114     private final ProjectSelector projectSelector;
115 
116     @Inject
117     @SuppressWarnings("checkstyle:ParameterNumber")
118     public DefaultMaven(
119             Lookup lookup,
120             ExecutionEventCatapult eventCatapult,
121             LegacySupport legacySupport,
122             SessionScope sessionScope,
123             RepositorySystemSessionFactory repositorySessionFactory,
124             @Named(GraphBuilder.HINT) GraphBuilder graphBuilder,
125             BuildResumptionAnalyzer buildResumptionAnalyzer,
126             BuildResumptionDataRepository buildResumptionDataRepository,
127             SuperPomProvider superPomProvider,
128             DefaultSessionFactory defaultSessionFactory,
129             @Nullable @Named("ide") WorkspaceReader ideWorkspaceReader) {
130         this.lookup = lookup;
131         this.eventCatapult = eventCatapult;
132         this.legacySupport = legacySupport;
133         this.sessionScope = sessionScope;
134         this.repositorySessionFactory = repositorySessionFactory;
135         this.graphBuilder = graphBuilder;
136         this.buildResumptionAnalyzer = buildResumptionAnalyzer;
137         this.buildResumptionDataRepository = buildResumptionDataRepository;
138         this.superPomProvider = superPomProvider;
139         this.ideWorkspaceReader = ideWorkspaceReader;
140         this.defaultSessionFactory = defaultSessionFactory;
141         this.projectSelector = new ProjectSelector(); // if necessary switch to DI
142     }
143 
144     @Override
145     public MavenExecutionResult execute(MavenExecutionRequest request) {
146         MavenExecutionResult result;
147 
148         try {
149             result = doExecute(request);
150         } catch (OutOfMemoryError e) {
151             result = addExceptionToResult(new DefaultMavenExecutionResult(), e);
152         } catch (RuntimeException e) {
153             // TODO Hack to make the cycle detection the same for the new graph builder
154             if (e.getCause() instanceof ProjectCycleException) {
155                 result = addExceptionToResult(new DefaultMavenExecutionResult(), e.getCause());
156             } else {
157                 result = addExceptionToResult(
158                         new DefaultMavenExecutionResult(), new InternalErrorException("Internal error: " + e, e));
159             }
160         } finally {
161             legacySupport.setSession(null);
162         }
163 
164         return result;
165     }
166 
167     //
168     // 1) Setup initial properties.
169     //
170     // 2) Validate local repository directory is accessible.
171     //
172     // 3) Create RepositorySystemSession.
173     //
174     // 4) Create MavenSession.
175     //
176     // 5) Execute AbstractLifecycleParticipant.afterSessionStart(session)
177     //
178     // 6) Get reactor projects looking for general POM errors
179     //
180     // 7) Create ProjectDependencyGraph using trimming which takes into account --projects and reactor mode.
181     // This ensures that the projects passed into the ReactorReader are only those specified.
182     //
183     // 8) Create ReactorReader with the getProjectMap( projects ). NOTE that getProjectMap(projects) is the code that
184     // checks for duplicate projects definitions in the build. Ideally this type of duplicate checking should be
185     // part of getting the reactor projects in 6). The duplicate checking is conflated with getProjectMap(projects).
186     //
187     // 9) Execute AbstractLifecycleParticipant.afterProjectsRead(session)
188     //
189     // 10) Create ProjectDependencyGraph without trimming (as trimming was done in 7). A new topological sort is
190     // required after the execution of 9) as the AbstractLifecycleParticipants are free to mutate the MavenProject
191     // instances, which may change dependencies which can, in turn, affect the build order.
192     //
193     // 11) Execute LifecycleStarter.start()
194     //
195     @SuppressWarnings("checkstyle:methodlength")
196     private MavenExecutionResult doExecute(MavenExecutionRequest request) {
197         request.setStartInstant(MonotonicClock.now());
198 
199         MavenExecutionResult result = new DefaultMavenExecutionResult();
200 
201         try {
202             validateLocalRepository(request);
203         } catch (IOException e) {
204             return addExceptionToResult(result, e);
205         }
206 
207         //
208         // We enter the session scope right after the MavenSession creation and before any of the
209         // AbstractLifecycleParticipant lookups
210         // so that @SessionScoped components can be @Injected into AbstractLifecycleParticipants.
211         //
212         sessionScope.enter();
213         MavenChainedWorkspaceReader chainedWorkspaceReader =
214                 new MavenChainedWorkspaceReader(request.getWorkspaceReader(), ideWorkspaceReader);
215         try (CloseableSession closeableSession = newCloseableSession(request, chainedWorkspaceReader)) {
216             MavenSession session = new MavenSession(closeableSession, request, result);
217             session.setSession(defaultSessionFactory.newSession(session));
218 
219             sessionScope.seed(MavenSession.class, session);
220             sessionScope.seed(RepositorySystemSession.class, closeableSession); // fixed in Maven 3.10.x
221             sessionScope.seed(Session.class, session.getSession());
222             sessionScope.seed(InternalMavenSession.class, InternalMavenSession.from(session.getSession()));
223 
224             legacySupport.setSession(session);
225 
226             return doExecute(request, session, result, chainedWorkspaceReader);
227         } finally {
228             sessionScope.exit();
229         }
230     }
231 
232     private MavenExecutionResult doExecute(
233             MavenExecutionRequest request,
234             MavenSession session,
235             MavenExecutionResult result,
236             MavenChainedWorkspaceReader chainedWorkspaceReader) {
237         try {
238             afterSessionStart(session);
239         } catch (MavenExecutionException e) {
240             return addExceptionToResult(result, e);
241         }
242 
243         try {
244             chainedWorkspaceReader.addReader(lookup.lookup(WorkspaceReader.class, ReactorReader.HINT));
245         } catch (LookupException e) {
246             return addExceptionToResult(result, e);
247         }
248 
249         eventCatapult.fire(ExecutionEvent.Type.ProjectDiscoveryStarted, session, null);
250 
251         Result<? extends ProjectDependencyGraph> graphResult = buildGraph(session);
252 
253         if (graphResult.hasErrors()) {
254             return addExceptionToResult(
255                     result, graphResult.getProblems().iterator().next().getException());
256         }
257 
258         try {
259             session.setProjectMap(getProjectMap(session.getProjects()));
260         } catch (DuplicateProjectException e) {
261             return addExceptionToResult(result, e);
262         }
263 
264         try {
265             setupWorkspaceReader(session, chainedWorkspaceReader);
266         } catch (LookupException e) {
267             return addExceptionToResult(result, e);
268         }
269         try {
270             afterProjectsRead(session);
271         } catch (MavenExecutionException e) {
272             return addExceptionToResult(result, e);
273         }
274 
275         //
276         // The projects need to be topologically after the participants have run their afterProjectsRead(session)
277         // because the participant is free to change the dependencies of a project which can potentially change the
278         // topological order of the projects, and therefore can potentially change the build order.
279         //
280         // Note that participants may affect the topological order of the projects, but it is
281         // not expected that a participant will add or remove projects from the session.
282 
283         graphResult = buildGraph(session);
284 
285         if (graphResult.hasErrors()) {
286             return addExceptionToResult(
287                     result, graphResult.getProblems().iterator().next().getException());
288         }
289 
290         try {
291             if (result.hasExceptions()) {
292                 return result;
293             }
294 
295             result.setTopologicallySortedProjects(session.getProjects());
296 
297             result.setProject(session.getTopLevelProject());
298 
299             validatePrerequisitesForNonMavenPluginProjects(session.getProjects());
300 
301             validateRequiredProfiles(session, request.getProfileActivation());
302             if (session.getResult().hasExceptions()) {
303                 return result;
304             }
305 
306             validateOptionalProfiles(session, request.getProfileActivation());
307 
308             LifecycleStarter lifecycleStarter = lookup.lookupOptional(LifecycleStarter.class, request.getBuilderId())
309                     .orElseGet(() -> lookup.lookup(LifecycleStarter.class));
310 
311             lifecycleStarter.execute(session);
312 
313             validateOptionalProjects(request, session);
314             validateOptionalProfiles(session, request.getProfileActivation());
315 
316             if (session.getResult().hasExceptions()) {
317                 addExceptionToResult(result, session.getResult().getExceptions().get(0));
318                 persistResumptionData(result, session);
319                 return result;
320             } else {
321                 session.getAllProjects().stream()
322                         .filter(MavenProject::isExecutionRoot)
323                         .findFirst()
324                         .ifPresent(buildResumptionDataRepository::removeResumptionData);
325             }
326         } finally {
327             try {
328                 afterSessionEnd(session);
329             } catch (MavenExecutionException e) {
330                 addExceptionToResult(result, e);
331             }
332         }
333 
334         return result;
335     }
336 
337     private void setupWorkspaceReader(MavenSession session, MavenChainedWorkspaceReader chainedWorkspaceReader) {
338         // Desired order of precedence for workspace readers before querying the local artifact repositories
339         Set<WorkspaceReader> workspaceReaders = new LinkedHashSet<>();
340         // 1) Reactor workspace reader
341         WorkspaceReader reactorReader = lookup.lookup(WorkspaceReader.class, ReactorReader.HINT);
342         workspaceReaders.add(reactorReader);
343         // 2) Repository system session-scoped workspace reader (contains ide and exec request reader)
344         for (WorkspaceReader repoWorkspaceReader : chainedWorkspaceReader.getReaders()) {
345             if (repoWorkspaceReader != null && repoWorkspaceReader != reactorReader) {
346                 workspaceReaders.add(repoWorkspaceReader);
347             }
348         }
349         // 3) .. n) Project-scoped workspace readers
350         workspaceReaders.addAll(getProjectScopedExtensionComponents(session.getProjects(), WorkspaceReader.class));
351         chainedWorkspaceReader.setReaders(workspaceReaders);
352     }
353 
354     private void afterSessionStart(MavenSession session) throws MavenExecutionException {
355         callListeners(session, AbstractMavenLifecycleParticipant::afterSessionStart);
356     }
357 
358     private void afterProjectsRead(MavenSession session) throws MavenExecutionException {
359         callListeners(session, AbstractMavenLifecycleParticipant::afterProjectsRead);
360     }
361 
362     private void afterSessionEnd(MavenSession session) throws MavenExecutionException {
363         callListeners(session, AbstractMavenLifecycleParticipant::afterSessionEnd);
364     }
365 
366     @FunctionalInterface
367     interface ListenerMethod {
368         void run(AbstractMavenLifecycleParticipant listener, MavenSession session) throws MavenExecutionException;
369     }
370 
371     private void callListeners(MavenSession session, ListenerMethod method) throws MavenExecutionException {
372         ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
373         try {
374             for (AbstractMavenLifecycleParticipant listener :
375                     getExtensionComponents(session.getProjects(), AbstractMavenLifecycleParticipant.class)) {
376                 Thread.currentThread().setContextClassLoader(listener.getClass().getClassLoader());
377                 method.run(listener, session);
378             }
379         } finally {
380             Thread.currentThread().setContextClassLoader(originalClassLoader);
381         }
382     }
383 
384     private void persistResumptionData(MavenExecutionResult result, MavenSession session) {
385         boolean hasLifecycleExecutionExceptions =
386                 result.getExceptions().stream().anyMatch(LifecycleExecutionException.class::isInstance);
387 
388         if (hasLifecycleExecutionExceptions) {
389             MavenProject rootProject = session.getAllProjects().stream()
390                     .filter(MavenProject::isExecutionRoot)
391                     .findFirst()
392                     .orElseThrow(() -> new IllegalStateException("No project in the session is execution root"));
393 
394             buildResumptionAnalyzer.determineBuildResumptionData(result).ifPresent(resumption -> {
395                 try {
396                     buildResumptionDataRepository.persistResumptionData(rootProject, resumption);
397                     result.setCanResume(true);
398                 } catch (BuildResumptionPersistenceException e) {
399                     logger.warn("Could not persist build resumption data", e);
400                 }
401             });
402         }
403     }
404 
405     /**
406      * This method is part of Maven core internal infrastructure and was never
407      * intended for use outside of Maven Core itself. External consumers should
408      * not rely on this method, as it may change or be removed without notice.
409      */
410     @Deprecated(since = "4.0.0")
411     public RepositorySystemSession newRepositorySession(MavenExecutionRequest request) {
412         if (!Boolean.parseBoolean(System.getProperty("maven.newRepositorySession.warningsDisabled", "false"))) {
413             if (logger.isDebugEnabled()) {
414                 logger.warn(
415                         "Deprecated method `DefaultMaven#newRepositorySession(MavenExecutionRequest)` invoked; please inspect the stack trace and report this issue to the authors of the caller code",
416                         new IllegalStateException(
417                                 "Deprecated method `DefaultMaven#newRepositorySession(MavenExecutionRequest)` invoked"));
418             } else {
419                 logger.warn(
420                         "Deprecated method `DefaultMaven#newRepositorySession(MavenExecutionRequest)` invoked; report this issue to the authors of the caller code (use -X to see stack trace)");
421             }
422         }
423         RepositorySystemSession result = newCloseableSession(request, new MavenChainedWorkspaceReader());
424         MavenSession session = new MavenSession(result, request, new DefaultMavenExecutionResult());
425         result.getData().set(MavenSession.class, session); // for legacy code to grab this
426         session.setSession(defaultSessionFactory.newSession(session));
427         return result;
428     }
429 
430     private CloseableSession newCloseableSession(MavenExecutionRequest request, WorkspaceReader workspaceReader) {
431         return repositorySessionFactory
432                 .newRepositorySessionBuilder(request)
433                 .setWorkspaceReader(workspaceReader)
434                 .build();
435     }
436 
437     private void validateLocalRepository(MavenExecutionRequest request) throws IOException {
438         File localRepoDir = request.getLocalRepositoryPath();
439 
440         logger.debug("Using local repository at {}", localRepoDir);
441 
442         localRepoDir.mkdirs();
443 
444         if (!localRepoDir.isDirectory()) {
445             throw new IOException("Could not create local repository at " + localRepoDir);
446         }
447     }
448 
449     private <T> Collection<T> getExtensionComponents(Collection<MavenProject> projects, Class<T> role) {
450         Collection<T> foundComponents = new LinkedHashSet<>();
451         foundComponents.addAll(lookup.lookupList(role));
452         foundComponents.addAll(getProjectScopedExtensionComponents(projects, role));
453         return foundComponents;
454     }
455 
456     protected <T> Collection<T> getProjectScopedExtensionComponents(Collection<MavenProject> projects, Class<T> role) {
457         if (projects == null) {
458             return Collections.emptyList();
459         }
460 
461         Collection<T> foundComponents = new LinkedHashSet<>();
462         Collection<ClassLoader> scannedRealms = new HashSet<>();
463 
464         Thread currentThread = Thread.currentThread();
465         ClassLoader originalContextClassLoader = currentThread.getContextClassLoader();
466         try {
467             for (MavenProject project : projects) {
468                 ClassLoader projectRealm = project.getClassRealm();
469 
470                 if (projectRealm != null && scannedRealms.add(projectRealm)) {
471                     currentThread.setContextClassLoader(projectRealm);
472                     foundComponents.addAll(lookup.lookupList(role));
473                 }
474             }
475             return foundComponents;
476         } finally {
477             currentThread.setContextClassLoader(originalContextClassLoader);
478         }
479     }
480 
481     private MavenExecutionResult addExceptionToResult(MavenExecutionResult result, Throwable e) {
482         if (!result.getExceptions().contains(e)) {
483             result.addException(e);
484         }
485 
486         return result;
487     }
488 
489     private void validatePrerequisitesForNonMavenPluginProjects(List<MavenProject> projects) {
490         for (MavenProject mavenProject : projects) {
491             if (!"maven-plugin".equals(mavenProject.getPackaging())) {
492                 Prerequisites prerequisites =
493                         mavenProject.getModel().getDelegate().getPrerequisites();
494                 if (prerequisites != null && prerequisites.getMaven() != null) {
495                     logger.warn(
496                             "The project {} uses prerequisites"
497                                     + " which is only intended for maven-plugin projects "
498                                     + "but not for non maven-plugin projects. "
499                                     + "For such purposes you should use the maven-enforcer-plugin. "
500                                     + "See https://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html",
501                             mavenProject.getId());
502                 }
503             }
504         }
505     }
506 
507     /**
508      * Get all profiles that are detected in the projects, any parent of the projects, or the settings.
509      * @param session The Maven session
510      * @return A {@link Set} of profile identifiers, never {@code null}.
511      */
512     private Set<String> getAllProfiles(MavenSession session) {
513         final Map<String, Model> superPomModels = new HashMap<>();
514         final Set<MavenProject> projectsIncludingParents = new HashSet<>();
515         for (MavenProject project : session.getProjects()) {
516             superPomModels.computeIfAbsent(
517                     project.getModelVersion(),
518                     v -> superPomProvider.getSuperModel(v).getDelegate());
519             boolean isAdded = projectsIncludingParents.add(project);
520             MavenProject parent = project.getParent();
521             while (isAdded && parent != null) {
522                 isAdded = projectsIncludingParents.add(parent);
523                 parent = parent.getParent();
524             }
525         }
526 
527         final Stream<String> projectProfiles = projectsIncludingParents.stream()
528                 .flatMap(p -> p.getModel().getDelegate().getProfiles().stream())
529                 .map(Profile::getId);
530         final Stream<String> settingsProfiles =
531                 session.getSettings().getProfiles().stream().map(org.apache.maven.settings.Profile::getId);
532         final Stream<String> superPomProfiles = superPomModels.values().stream()
533                 .flatMap(p -> p.getProfiles().stream())
534                 .map(Profile::getId);
535 
536         return Stream.of(projectProfiles, settingsProfiles, superPomProfiles)
537                 .flatMap(Function.identity())
538                 .collect(toSet());
539     }
540 
541     /**
542      * Check whether the required profiles were found in any of the projects we're building or the settings.
543      * @param session the Maven session.
544      * @param profileActivation the requested optional and required profiles.
545      */
546     private void validateRequiredProfiles(MavenSession session, ProfileActivation profileActivation) {
547         final Set<String> allAvailableProfiles = getAllProfiles(session);
548 
549         final Set<String> requiredProfiles = new HashSet<>();
550         requiredProfiles.addAll(profileActivation.getRequiredActiveProfileIds());
551         requiredProfiles.addAll(profileActivation.getRequiredInactiveProfileIds());
552 
553         // Check whether the required profiles were found in any of the projects we're building.
554         final Set<String> notFoundRequiredProfiles = requiredProfiles.stream()
555                 .filter(rap -> !allAvailableProfiles.contains(rap))
556                 .collect(toSet());
557 
558         if (!notFoundRequiredProfiles.isEmpty()) {
559             // Use SLF4J formatter for consistency with warnings reported by logger
560             final String message = MessageFormatter.format(
561                             "The requested profiles {} could not be activated or deactivated because they do not"
562                                     + " exist.",
563                             notFoundRequiredProfiles)
564                     .getMessage();
565             addExceptionToResult(session.getResult(), new MissingProfilesException(message));
566         }
567     }
568 
569     /**
570      * Check whether any of the requested optional projects were not activated or deactivated.
571      * @param request the {@link MavenExecutionRequest}.
572      * @param session the {@link MavenSession}.
573      */
574     private void validateOptionalProjects(MavenExecutionRequest request, MavenSession session) {
575         final ProjectActivation projectActivation = request.getProjectActivation();
576         final Set<String> allOptionalSelectors = new HashSet<>();
577         allOptionalSelectors.addAll(projectActivation.getOptionalActiveProjectSelectors());
578         allOptionalSelectors.addAll(projectActivation.getRequiredActiveProjectSelectors());
579         // We intentionally ignore the results of this method.
580         // As a side effect it will log the optional projects that could not be resolved.
581         projectSelector.getOptionalProjectsBySelectors(request, session.getAllProjects(), allOptionalSelectors);
582     }
583 
584     /**
585      * Check whether any of the requested optional profiles were not activated or deactivated.
586      * @param session the Maven session.
587      * @param profileActivation the requested optional and required profiles.
588      */
589     private void validateOptionalProfiles(MavenSession session, ProfileActivation profileActivation) {
590         final Set<String> allAvailableProfiles = getAllProfiles(session);
591 
592         final Set<String> optionalProfiles = new HashSet<>();
593         optionalProfiles.addAll(profileActivation.getOptionalActiveProfileIds());
594         optionalProfiles.addAll(profileActivation.getOptionalInactiveProfileIds());
595 
596         final Set<String> notFoundOptionalProfiles = optionalProfiles.stream()
597                 .filter(rap -> !allAvailableProfiles.contains(rap))
598                 .collect(toSet());
599 
600         if (!notFoundOptionalProfiles.isEmpty()) {
601             logger.info(
602                     "The requested optional profiles {} could not be activated or deactivated because they do not"
603                             + " exist.",
604                     notFoundOptionalProfiles);
605         }
606     }
607 
608     private Map<String, MavenProject> getProjectMap(Collection<MavenProject> projects)
609             throws DuplicateProjectException {
610         Map<String, MavenProject> index = new LinkedHashMap<>();
611         Map<String, List<File>> collisions = new LinkedHashMap<>();
612 
613         for (MavenProject project : projects) {
614             String projectId = ArtifactUtils.key(project.getGroupId(), project.getArtifactId(), project.getVersion());
615 
616             MavenProject collision = index.get(projectId);
617 
618             if (collision == null) {
619                 index.put(projectId, project);
620             } else {
621                 List<File> pomFiles = collisions.get(projectId);
622 
623                 if (pomFiles == null) {
624                     pomFiles = new ArrayList<>(Arrays.asList(collision.getFile(), project.getFile()));
625                     collisions.put(projectId, pomFiles);
626                 } else {
627                     pomFiles.add(project.getFile());
628                 }
629             }
630         }
631 
632         if (!collisions.isEmpty()) {
633             throw new DuplicateProjectException(
634                     "Two or more projects in the reactor"
635                             + " have the same identifier, please make sure that <groupId>:<artifactId>:<version>"
636                             + " is unique for each project: " + collisions,
637                     collisions);
638         }
639 
640         return index;
641     }
642 
643     private Result<? extends ProjectDependencyGraph> buildGraph(MavenSession session) {
644         Result<? extends ProjectDependencyGraph> graphResult = graphBuilder.build(session);
645         for (ModelProblem problem : graphResult.getProblems()) {
646             if (problem.getSeverity() == ModelProblem.Severity.WARNING) {
647                 logger.warn(problem.getMessage());
648             } else {
649                 logger.error(problem.getMessage());
650             }
651         }
652 
653         if (!graphResult.hasErrors()) {
654             ProjectDependencyGraph projectDependencyGraph = graphResult.get();
655             session.setProjects(projectDependencyGraph.getSortedProjects());
656             session.setAllProjects(projectDependencyGraph.getAllProjects());
657             session.setProjectDependencyGraph(projectDependencyGraph);
658         }
659 
660         return graphResult;
661     }
662 
663     @Deprecated
664     // 5 January 2014
665     protected Logger getLogger() {
666         return logger;
667     }
668 }