You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by be...@apache.org on 2009/06/21 21:26:01 UTC

svn commit: r787072 - in /maven/components/trunk/maven-core/src/main/java/org/apache/maven: DefaultMaven.java DuplicateProjectException.java

Author: bentmann
Date: Sun Jun 21 19:26:00 2009
New Revision: 787072

URL: http://svn.apache.org/viewvc?rev=787072&view=rev
Log:
[MNG-1491] Reactor should print out a message if it detects a collision of artifact ids

Added:
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java   (with props)
Modified:
    maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java

Modified: maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java?rev=787072&r1=787071&r2=787072&view=diff
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java (original)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/DefaultMaven.java Sun Jun 21 19:26:00 2009
@@ -187,16 +187,52 @@
         
         List<File> files = Arrays.asList( request.getPom().getAbsoluteFile() );
 
-        Map<String,MavenProject> projects = collectProjects( files, request );
+        List<MavenProject> projects = new ArrayList<MavenProject>();
 
-        return projects;
+        collectProjects( projects, files, request );
+
+        Map<String, MavenProject> index = new LinkedHashMap<String, MavenProject>();
+        Map<String, List<File>> collisions = new LinkedHashMap<String, List<File>>();
+
+        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<File> pomFiles = collisions.get( projectId );
+
+                if ( pomFiles == null )
+                {
+                    pomFiles = new ArrayList<File>( 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 <groupId>:<artifactId>:<version>"
+                + " is unique for each project: " + collisions, collisions );
+        }
+
+        return index;
     }
 
-    private Map<String,MavenProject> collectProjects( List<File> files, MavenExecutionRequest request )
+    private void collectProjects( List<MavenProject> projects, List<File> files, MavenExecutionRequest request )
         throws MavenExecutionException, ProjectBuildingException
     {
-        Map<String,MavenProject> projects = new LinkedHashMap<String,MavenProject>();
-
         for ( File file : files )
         {
             MavenProject project = projectBuilder.build( file, request.getProjectBuildingRequest() );
@@ -245,15 +281,11 @@
                     moduleFiles.add( moduleFile );
                 }
 
-                Map<String,MavenProject> 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<MavenProject> projects, List<String> activeProfileIds )

Added: maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java
URL: http://svn.apache.org/viewvc/maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java?rev=787072&view=auto
==============================================================================
--- maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java (added)
+++ maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java Sun Jun 21 19:26:00 2009
@@ -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<String, List<File>> 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<String, List<File>> collisions )
+    {
+        super( message, (File) null );
+
+        this.collisions = ( collisions != null ) ? collisions : new LinkedHashMap<String, List<File>>();
+    }
+
+    /**
+     * 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<String, List<File>> getCollisions()
+    {
+        return collisions;
+    }
+
+}

Propchange: maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/components/trunk/maven-core/src/main/java/org/apache/maven/DuplicateProjectException.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision