<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact-manager</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-model</artifactId>
+ </dependency>
</dependencies>
</project>
*/
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.
*
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+).
extends AbstractArtifactDiscoverer
implements ArtifactDiscoverer
{
+ private final static String POM = ".pom";
+
+ private final static String DELIM = "\\";
+
/**
* @plexus.requirement
*/
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;
+ }
}
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;
extends AbstractArtifactDiscoverer
implements ArtifactDiscoverer
{
+ private final static String POM = ".pom";
+ private final static String DELIM = "\\";
+
/**
* @plexus.requirement
*/
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
*/
}
}
+ 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" );