You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ep...@apache.org on 2006/02/24 02:46:57 UTC

svn commit: r380295 - in /maven/repository-manager/trunk/maven-repository-discovery: ./ src/main/java/org/apache/maven/repository/discovery/ src/test/java/org/apache/maven/repository/discovery/

Author: epunzalan
Date: Thu Feb 23 17:46:55 2006
New Revision: 380295

URL: http://svn.apache.org/viewcvs?rev=380295&view=rev
Log:
PR: MRM-41
Submitted by: John Tolentino

Patch to discover standalone poms

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

Modified: maven/repository-manager/trunk/maven-repository-discovery/pom.xml
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-discovery/pom.xml?rev=380295&r1=380294&r2=380295&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-discovery/pom.xml (original)
+++ maven/repository-manager/trunk/maven-repository-discovery/pom.xml Thu Feb 23 17:46:55 2006
@@ -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>

Modified: maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java?rev=380295&r1=380294&r2=380295&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java (original)
+++ maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java Thu Feb 23 17:46:55 2006
@@ -46,6 +46,20 @@
     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.
      *
      * @return the paths as Strings.

Modified: maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java?rev=380295&r1=380294&r2=380295&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java (original)
+++ maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java Thu Feb 23 17:46:55 2006
@@ -20,10 +20,16 @@
 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 @@
     extends AbstractArtifactDiscoverer
     implements ArtifactDiscoverer
 {
+    private final static String POM = ".pom";
+
+    private final static String DELIM = "\\";
+
     /**
      * @plexus.requirement
      */
@@ -74,5 +84,202 @@
         }
 
         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;
     }
 }

Modified: maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java?rev=380295&r1=380294&r2=380295&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java (original)
+++ maven/repository-manager/trunk/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java Thu Feb 23 17:46:55 2006
@@ -19,8 +19,12 @@
 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 @@
     extends AbstractArtifactDiscoverer
     implements ArtifactDiscoverer
 {
+    private final static String POM = ".pom";
+    private final static String DELIM = "\\";
+
     /**
      * @plexus.requirement
      */
@@ -66,6 +73,202 @@
         }
 
         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;
     }
 
     /**

Modified: maven/repository-manager/trunk/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java
URL: http://svn.apache.org/viewcvs/maven/repository-manager/trunk/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java?rev=380295&r1=380294&r2=380295&view=diff
==============================================================================
--- maven/repository-manager/trunk/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java (original)
+++ maven/repository-manager/trunk/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java Thu Feb 23 17:46:55 2006
@@ -356,6 +356,29 @@
         }
     }
 
+    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" );