diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 4e1cd0f9af..f7dbf77dbc 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -187,16 +187,52 @@ protected Map getProjectsForMavenReactor( MavenExecutionReq List files = Arrays.asList( request.getPom().getAbsoluteFile() ); - Map projects = collectProjects( files, request ); + List projects = new ArrayList(); - return projects; + collectProjects( projects, files, request ); + + Map index = new LinkedHashMap(); + Map> collisions = new LinkedHashMap>(); + + for ( MavenProject project : projects ) + { + String projectId = ArtifactUtils.key( project.getGroupId(), project.getArtifactId(), project.getVersion() ); + + MavenProject collision = index.get( projectId ); + + if ( collision == null ) + { + index.put( projectId, project ); + } + else + { + List pomFiles = collisions.get( projectId ); + + if ( pomFiles == null ) + { + pomFiles = new ArrayList( Arrays.asList( collision.getFile(), project.getFile() ) ); + collisions.put( projectId, pomFiles ); + } + else + { + pomFiles.add( project.getFile() ); + } + } + } + + if ( !collisions.isEmpty() ) + { + throw new org.apache.maven.DuplicateProjectException( "Two or more projects in the reactor" + + " have the same identifier, please make sure that ::" + + " is unique for each project: " + collisions, collisions ); + } + + return index; } - private Map collectProjects( List files, MavenExecutionRequest request ) + private void collectProjects( List projects, List files, MavenExecutionRequest request ) throws MavenExecutionException, ProjectBuildingException { - Map projects = new LinkedHashMap(); - for ( File file : files ) { MavenProject project = projectBuilder.build( file, request.getProjectBuildingRequest() ); @@ -245,15 +281,11 @@ else if ( moduleFile.isDirectory() ) moduleFiles.add( moduleFile ); } - Map collectedProjects = collectProjects( moduleFiles, request ); - - projects.putAll( collectedProjects ); + collectProjects( projects, moduleFiles, request ); } - - projects.put( ArtifactUtils.key( project.getGroupId(), project.getArtifactId(), project.getVersion() ), project ); - } - return projects; + projects.add( project ); + } } private void validateActivatedProfiles( List projects, List activeProfileIds ) diff --git a/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java b/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java new file mode 100644 index 0000000000..c58749dde2 --- /dev/null +++ b/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java @@ -0,0 +1,61 @@ +package org.apache.maven; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * Signals a collision of two or more projects with the same g:a:v during a reactor build. + * + * @author Benjamin Bentmann + */ +public class DuplicateProjectException + extends MavenExecutionException +{ + + private Map> collisions; + + /** + * Creates a new exception with specified details. + * + * @param message The message text, may be {@code null}. + * @param collisions The POM files of the projects that collided, indexed by their g:a:v, may be {@code null}. + */ + public DuplicateProjectException( String message, Map> collisions ) + { + super( message, (File) null ); + + this.collisions = ( collisions != null ) ? collisions : new LinkedHashMap>(); + } + + /** + * Gets the POM files of the projects that collided. + * + * @return The POM files of the projects that collided, indexed by their g:a:v, never {@code null}. + */ + public Map> getCollisions() + { + return collisions; + } + +}