MartinKanters commented on a change in pull request #429: URL: https://github.com/apache/maven/pull/429#discussion_r565148646
########## File path: maven-core/src/main/java/org/apache/maven/DefaultMaven.java ########## @@ -493,25 +508,88 @@ private void validatePrerequisitesForNonMavenPluginProjects( List<MavenProject> } } - private void validateActivatedProfiles( List<MavenProject> projects, List<String> activeProfileIds ) + /** + * Get all profiles that are detected in either the projects, any parent of the projects or the settings. + * @param session The Maven session + * @return A {@link Set} of profile identifiers, never {@code null}. + */ + private Set<String> getAllProfiles( MavenSession session ) { - Collection<String> notActivatedProfileIds = new LinkedHashSet<>( activeProfileIds ); - - for ( MavenProject project : projects ) + final Set<MavenProject> projectsIncludingParents = new HashSet<>(); + for ( MavenProject project : session.getProjects() ) { - for ( List<String> profileIds : project.getInjectedProfileIds().values() ) + boolean isAdded = projectsIncludingParents.add( project ); + MavenProject parent = project.getParent(); + while ( isAdded && parent != null ) { - notActivatedProfileIds.removeAll( profileIds ); + isAdded = projectsIncludingParents.add( parent ); + parent = parent.getParent(); } } - for ( String notActivatedProfileId : notActivatedProfileIds ) + final Stream<String> projectProfiles = projectsIncludingParents.stream() + .map( MavenProject::getModel ) + .map( Model::getProfiles ) + .flatMap( Collection::stream ) + .map( Profile::getId ); + final Stream<String> settingsProfiles = session.getSettings().getProfiles().stream() + .map( IdentifiableBase::getId ); + + return Stream.concat( projectProfiles, settingsProfiles ).collect( toSet() ); + } + + /** + * Check whether the required profiles were found in any of the projects we're building or the Maven settings. + * @param session the Maven session. + * @param profileActivation the requested optional and required profiles. + */ + private void validateRequiredProfiles( MavenSession session, ProfileActivation profileActivation ) + { + final Set<String> allDetectedProfiles = getAllProfiles( session ); + + final Set<String> requiredProfiles = new HashSet<>( ); + requiredProfiles.addAll( profileActivation.getRequiredActiveProfileIds() ); + requiredProfiles.addAll( profileActivation.getRequiredInactiveProfileIds() ); + + // Check whether the required profiles were found in any of the projects we're building. + final Set<String> notFoundRequiredProfiles = requiredProfiles.stream() + .filter( rap -> !allDetectedProfiles.contains( rap ) ) + .collect( toSet() ); + + if ( !notFoundRequiredProfiles.isEmpty() ) { - logger.warn( "The requested profile \"" + notActivatedProfileId - + "\" could not be activated because it does not exist." ); + final String profileIds = notFoundRequiredProfiles.stream() + .map( p -> String.format( "\"%s\"", p ) ) + .collect( joining( ", " ) ); + final String message = String.format( + "The requested profile(s) %s could not be activated or de-activated because they do not exist.", + profileIds + ); + addExceptionToResult( session.getResult(), new MissingProfileException( message ) ); } } + /** + * Check whether any of the requested optional profiles were not activated or deactivated. + * @param session the Maven session. + * @param profileActivation the requested optional and required profiles. + */ + private void validateOptionalProfiles( MavenSession session, ProfileActivation profileActivation ) + { + final Set<String> allDetectedProfiles = getAllProfiles( session ); + + final Set<String> optionalProfiles = new HashSet<>( ); + optionalProfiles.addAll( profileActivation.getOptionalActiveProfileIds() ); + optionalProfiles.addAll( profileActivation.getOptionalInactiveProfileIds() ); + + final Set<String> notFoundOptionalProfiles = optionalProfiles.stream() + .filter( rap -> !allDetectedProfiles.contains( rap ) ) + .collect( toSet() ); + + notFoundOptionalProfiles.forEach( profile -> logger.warn( "The requested profile \"" + profile Review comment: Good point, I'm not sure how it ended up inconsistent. I will make this into a one-line collection logger as well. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org