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
This commit is contained in:
Edwin L. Punzalan 2006-02-24 01:46:55 +00:00
parent 69e3cfc270
commit 8ba07b9513
5 changed files with 451 additions and 0 deletions

View File

@ -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>

View 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.
*

View 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;
}
}

View 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
*/

View 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" );