]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-41
authorEdwin L. Punzalan <epunzalan@apache.org>
Fri, 24 Feb 2006 01:46:55 +0000 (01:46 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Fri, 24 Feb 2006 01:46:55 +0000 (01:46 +0000)
Submitted by: John Tolentino

Patch to discover standalone poms

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@380295 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-discovery/pom.xml
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java
maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java
maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java

index d1359735313485848223d62fd55e38037e4d9c0e..dfd1a8b7f131a1004a31bae7edad4e619a3d297d 100755 (executable)
@@ -49,5 +49,9 @@
       <groupId>org.apache.maven</groupId>
       <artifactId>maven-artifact-manager</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-model</artifactId>
+    </dependency>
   </dependencies>
 </project>
index 935705fe9e1a17ed01fcf5c8d54cf1bb671230eb..fc00fb4ef42a758aca82090bedadb1d8d9083e73 100644 (file)
@@ -45,6 +45,20 @@ public interface ArtifactDiscoverer
      */
     List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots );
 
+    /**
+     * Discover standalone POM artifacts in the repository.
+     *
+     * @param repository          the location of the repository
+     * @param blacklistedPatterns pattern that lists any files to prevent from being included when scanning
+     * @param includeSnapshots    whether to discover snapshots
+     * @return the list of artifacts discovered
+     * @todo replace repositoryBase with wagon repository
+     * @todo do we want blacklisted patterns in another form? Part of the object construction?
+     * @todo should includeSnapshots be configuration on the component?
+     * @todo instead of a returned list, should a listener be passed in?
+     */
+    List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots );
+
     /**
      * Get the list of paths kicked out during the discovery process.
      *
index fc0e97639480fbdbd942ce0fa87d47b9e3b20f35..42de10bf3a69d28cc8becab930e24e9a8856175a 100644 (file)
@@ -20,10 +20,16 @@ import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.repository.ArtifactUtils;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.model.Model;
+import org.codehaus.plexus.util.StringUtils;
 
 import java.io.File;
+import java.io.FileReader;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.StringTokenizer;
 
 /**
  * Artifact discoverer for the new repository layout (Maven 2.0+).
@@ -36,6 +42,10 @@ public class DefaultArtifactDiscoverer
     extends AbstractArtifactDiscoverer
     implements ArtifactDiscoverer
 {
+    private final static String POM = ".pom";
+
+    private final static String DELIM = "\\";
+
     /**
      * @plexus.requirement
      */
@@ -75,4 +85,201 @@ public class DefaultArtifactDiscoverer
 
         return artifacts;
     }
+
+    public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns,
+                                        boolean convertSnapshots )
+    {
+        List artifacts = new ArrayList();
+
+        File repositoryBase = new File( repository.getBasedir() );
+
+        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
+
+        for ( int i = 0; i < artifactPaths.length; i++ )
+        {
+            String path = artifactPaths[i];
+
+            if ( path.toLowerCase().endsWith( POM ) )
+            {
+                Artifact pomArtifact = buildArtifact( path );
+
+                MavenXpp3Reader mavenReader = new MavenXpp3Reader();
+                String filename = repositoryBase.getAbsolutePath() + DELIM + path;
+                try
+                {
+                    Model model = mavenReader.read( new FileReader( filename ) );
+                    if ( ( pomArtifact != null ) && ( "pom".equals( model.getPackaging() ) ) )
+                    {
+                        if ( convertSnapshots || !pomArtifact.isSnapshot() )
+                        {
+                            artifacts.add( pomArtifact );
+                        }
+                    }
+                }
+                catch ( Exception e )
+                {
+                    System.out.println( "error reading file: " + filename );
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return artifacts;
+    }
+
+    private Artifact buildArtifact( String path )
+    {
+        List pathParts = new ArrayList();
+        StringTokenizer st = new StringTokenizer( path, "/\\" );
+        while ( st.hasMoreTokens() )
+        {
+            pathParts.add( st.nextToken() );
+        }
+
+        Collections.reverse( pathParts );
+
+        Artifact finalResult = null;
+        if ( pathParts.size() < 4 )
+        {
+            addKickedOutPath( path );
+        }
+        else
+        {
+            // the actual artifact filename.
+            String filename = (String) pathParts.remove( 0 );
+
+            // the next one is the version.
+            String version = (String) pathParts.remove( 0 );
+
+            // the next one is the artifactId.
+            String artifactId = (String) pathParts.remove( 0 );
+
+            // the remaining are the groupId.
+            Collections.reverse( pathParts );
+            String groupId = StringUtils.join( pathParts.iterator(), "." );
+
+            String remainingFilename = filename;
+            if ( !remainingFilename.startsWith( artifactId + "-" ) )
+            {
+                addKickedOutPath( path );
+            }
+            else
+            {
+                remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
+
+                String classifier = null;
+
+                // TODO: use artifact handler, share with legacy discoverer
+                String type = null;
+                if ( remainingFilename.endsWith( ".tar.gz" ) )
+                {
+                    type = "distribution-tgz";
+                    remainingFilename =
+                        remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() );
+                }
+                else if ( remainingFilename.endsWith( ".zip" ) )
+                {
+                    type = "distribution-zip";
+                    remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() );
+                }
+                else if ( remainingFilename.endsWith( "-sources.jar" ) )
+                {
+                    type = "java-source";
+                    classifier = "sources";
+                    remainingFilename =
+                        remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() );
+                }
+                else
+                {
+                    int index = remainingFilename.lastIndexOf( "." );
+                    if ( index < 0 )
+                    {
+                        addKickedOutPath( path );
+                    }
+                    else
+                    {
+                        type = remainingFilename.substring( index + 1 );
+                        remainingFilename = remainingFilename.substring( 0, index );
+                    }
+                }
+
+                if ( type != null )
+                {
+                    Artifact result;
+
+                    if ( classifier == null )
+                    {
+                        result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME,
+                                                                 type );
+                    }
+                    else
+                    {
+                        result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
+                                                                               classifier );
+                    }
+
+                    if ( result.isSnapshot() )
+                    {
+                        // version is XXX-SNAPSHOT, filename is XXX-yyyyMMdd.hhmmss-b
+                        int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
+                        if ( classifierIndex >= 0 )
+                        {
+                            classifier = remainingFilename.substring( classifierIndex + 1 );
+                            remainingFilename = remainingFilename.substring( 0, classifierIndex );
+                            result = artifactFactory.createArtifactWithClassifier( groupId, artifactId,
+                                                                                   remainingFilename, type,
+                                                                                   classifier );
+                        }
+                        else
+                        {
+                            result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename,
+                                                                     Artifact.SCOPE_RUNTIME, type );
+                        }
+
+                        // poor encapsulation requires we do this to populate base version
+                        if ( !result.isSnapshot() )
+                        {
+                            addKickedOutPath( path );
+                        }
+                        else if ( !result.getBaseVersion().equals( version ) )
+                        {
+                            addKickedOutPath( path );
+                        }
+                        else
+                        {
+                            finalResult = result;
+                        }
+                    }
+                    else if ( !remainingFilename.startsWith( version ) )
+                    {
+                        addKickedOutPath( path );
+                    }
+                    else if ( !remainingFilename.equals( version ) )
+                    {
+                        if ( remainingFilename.charAt( version.length() ) != '-' )
+                        {
+                            addKickedOutPath( path );
+                        }
+                        else
+                        {
+                            classifier = remainingFilename.substring( version.length() + 1 );
+                            finalResult = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version,
+                                                                                        type, classifier );
+                        }
+                    }
+                    else
+                    {
+                        finalResult = result;
+                    }
+                }
+            }
+        }
+
+        if ( finalResult != null )
+        {
+            finalResult.setFile( new File( path ) );
+        }
+
+        return finalResult;
+    }
 }
index 0bd2fff5e59738f193906e29507b0962e69f06f9..e592eafe4e2465a93d3b0492f200d8f78545f123 100644 (file)
@@ -19,8 +19,12 @@ package org.apache.maven.repository.discovery;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.model.Model;
+import org.codehaus.plexus.util.StringUtils;
 
 import java.io.File;
+import java.io.FileReader;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
@@ -39,6 +43,9 @@ public class LegacyArtifactDiscoverer
     extends AbstractArtifactDiscoverer
     implements ArtifactDiscoverer
 {
+    private final static String POM = ".pom";
+    private final static String DELIM = "\\";
+
     /**
      * @plexus.requirement
      */
@@ -68,6 +75,202 @@ public class LegacyArtifactDiscoverer
         return artifacts;
     }
 
+    public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns, boolean convertSnapshots )
+    {
+        List artifacts = new ArrayList();
+
+        File repositoryBase = new File( repository.getBasedir() );
+
+        String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
+
+        for ( int i = 0; i < artifactPaths.length; i++ )
+        {
+            String path = artifactPaths[i];
+
+            if ( path.toLowerCase().endsWith( POM ) )
+            {
+                Artifact pomArtifact = buildArtifact( path );
+
+                MavenXpp3Reader mavenReader = new MavenXpp3Reader();
+                String filename = repositoryBase.getAbsolutePath() + DELIM + path;
+                try
+                {
+                    Model model = mavenReader.read( new FileReader( filename ) );
+                    if ( ( pomArtifact != null ) && ( "pom".equals(model.getPackaging()) ) )
+                    {
+                        if ( convertSnapshots || !pomArtifact.isSnapshot() )
+                        {
+                            artifacts.add( pomArtifact );
+                        }
+                    }
+                }
+                catch (Exception e)
+                {
+                    System.out.println( "error reading file: " +  filename );
+                    e.printStackTrace();
+                }
+            }
+        }
+
+        return artifacts;
+    }
+
+    private Artifact buildArtifact( String path )
+    {
+        List pathParts = new ArrayList();
+        StringTokenizer st = new StringTokenizer( path, "/\\" );
+        while ( st.hasMoreTokens() )
+        {
+            pathParts.add( st.nextToken() );
+        }
+
+        Collections.reverse( pathParts );
+
+        Artifact finalResult = null;
+        if ( pathParts.size() < 4 )
+        {
+            addKickedOutPath( path );
+        }
+        else
+        {
+            // the actual artifact filename.
+            String filename = (String) pathParts.remove( 0 );
+
+            // the next one is the version.
+            String version = (String) pathParts.remove( 0 );
+
+            // the next one is the artifactId.
+            String artifactId = (String) pathParts.remove( 0 );
+
+            // the remaining are the groupId.
+            Collections.reverse( pathParts );
+            String groupId = StringUtils.join( pathParts.iterator(), "." );
+
+            String remainingFilename = filename;
+            if ( !remainingFilename.startsWith( artifactId + "-" ) )
+            {
+                addKickedOutPath( path );
+            }
+            else
+            {
+                remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
+
+                String classifier = null;
+
+                // TODO: use artifact handler, share with legacy discoverer
+                String type = null;
+                if ( remainingFilename.endsWith( ".tar.gz" ) )
+                {
+                    type = "distribution-tgz";
+                    remainingFilename =
+                        remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() );
+                }
+                else if ( remainingFilename.endsWith( ".zip" ) )
+                {
+                    type = "distribution-zip";
+                    remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() );
+                }
+                else if ( remainingFilename.endsWith( "-sources.jar" ) )
+                {
+                    type = "java-source";
+                    classifier = "sources";
+                    remainingFilename =
+                        remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() );
+                }
+                else
+                {
+                    int index = remainingFilename.lastIndexOf( "." );
+                    if ( index < 0 )
+                    {
+                        addKickedOutPath( path );
+                    }
+                    else
+                    {
+                        type = remainingFilename.substring( index + 1 );
+                        remainingFilename = remainingFilename.substring( 0, index );
+                    }
+                }
+
+                if ( type != null )
+                {
+                    Artifact result;
+
+                    if ( classifier == null )
+                    {
+                        result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME,
+                                                                 type );
+                    }
+                    else
+                    {
+                        result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
+                                                                               classifier );
+                    }
+
+                    if ( result.isSnapshot() )
+                    {
+                        // version is XXX-SNAPSHOT, filename is XXX-yyyyMMdd.hhmmss-b
+                        int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
+                        if ( classifierIndex >= 0 )
+                        {
+                            classifier = remainingFilename.substring( classifierIndex + 1 );
+                            remainingFilename = remainingFilename.substring( 0, classifierIndex );
+                            result = artifactFactory.createArtifactWithClassifier( groupId, artifactId,
+                                                                                   remainingFilename, type,
+                                                                                   classifier );
+                        }
+                        else
+                        {
+                            result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename,
+                                                                     Artifact.SCOPE_RUNTIME, type );
+                        }
+
+                        // poor encapsulation requires we do this to populate base version
+                        if ( !result.isSnapshot() )
+                        {
+                            addKickedOutPath( path );
+                        }
+                        else if ( !result.getBaseVersion().equals( version ) )
+                        {
+                            addKickedOutPath( path );
+                        }
+                        else
+                        {
+                            finalResult = result;
+                        }
+                    }
+                    else if ( !remainingFilename.startsWith( version ) )
+                    {
+                        addKickedOutPath( path );
+                    }
+                    else if ( !remainingFilename.equals( version ) )
+                    {
+                        if ( remainingFilename.charAt( version.length() ) != '-' )
+                        {
+                            addKickedOutPath( path );
+                        }
+                        else
+                        {
+                            classifier = remainingFilename.substring( version.length() + 1 );
+                            finalResult = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version,
+                                                                                        type, classifier );
+                        }
+                    }
+                    else
+                    {
+                        finalResult = result;
+                    }
+                }
+            }
+        }
+
+        if ( finalResult != null )
+        {
+            finalResult.setFile( new File( path ) );
+        }
+
+        return finalResult;
+    }
+
     /**
      * @noinspection CollectionDeclaredAsConcreteClass
      */
index 9d21424f967677843770d4c09bf9fa7101685063..84bba71e50b7311e021eb23e5dfcc40cc73c4527 100644 (file)
@@ -356,6 +356,29 @@ public class DefaultArtifactDiscovererTest
         }
     }
 
+    public void testStandalonePoms()
+    {
+        List artifacts = discoverer.discoverStandalonePoms( repository, null, false );
+        assertEquals( 4, artifacts.size() );
+        Iterator itr = artifacts.iterator();
+        Artifact artifact = (Artifact) itr.next();
+        assertEquals( "org.apache.maven", artifact.getGroupId() );
+        assertEquals( "B", artifact.getArtifactId() );
+        assertEquals( "1.0", artifact.getVersion() );
+        artifact = (Artifact) itr.next();
+        assertEquals( "org.apache.maven", artifact.getGroupId() );
+        assertEquals( "B", artifact.getArtifactId() );
+        assertEquals( "2.0", artifact.getVersion() );
+        artifact = (Artifact) itr.next();
+        assertEquals( "org.apache.maven", artifact.getGroupId() );
+        assertEquals( "discovery", artifact.getArtifactId() );
+        assertEquals( "1.0", artifact.getVersion() );
+        artifact = (Artifact) itr.next();
+        assertEquals( "org.apache.testgroup", artifact.getGroupId() );
+        assertEquals( "discovery", artifact.getArtifactId() );
+        assertEquals( "1.0", artifact.getVersion() );
+    }
+
     private Artifact createArtifact( String groupId, String artifactId, String version )
     {
         return factory.createArtifact( groupId, artifactId, version, null, "jar" );