From 8ba07b951346c1164f86f40925cd01dee32bbbef Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Fri, 24 Feb 2006 01:46:55 +0000 Subject: [PATCH] PR: MRM-41 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 | 4 + .../discovery/ArtifactDiscoverer.java | 14 ++ .../discovery/DefaultArtifactDiscoverer.java | 207 ++++++++++++++++++ .../discovery/LegacyArtifactDiscoverer.java | 203 +++++++++++++++++ .../DefaultArtifactDiscovererTest.java | 23 ++ 5 files changed, 451 insertions(+) diff --git a/maven-repository-discovery/pom.xml b/maven-repository-discovery/pom.xml index d13597353..dfd1a8b7f 100755 --- a/maven-repository-discovery/pom.xml +++ b/maven-repository-discovery/pom.xml @@ -49,5 +49,9 @@ org.apache.maven maven-artifact-manager + + org.apache.maven + maven-model + diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java index 935705fe9..fc00fb4ef 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java @@ -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. * diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java index fc0e97639..42de10bf3 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultArtifactDiscoverer.java @@ -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; + } } diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java index 0bd2fff5e..e592eafe4 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/LegacyArtifactDiscoverer.java @@ -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 */ diff --git a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java index 9d21424f9..84bba71e5 100644 --- a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java +++ b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java @@ -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" ); -- 2.39.5