diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java index 676f38bb2..f28ca0c0f 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java @@ -18,6 +18,7 @@ package org.apache.maven.repository.discovery; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.DirectoryScanner; +import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.util.ArrayList; @@ -55,24 +56,18 @@ public abstract class AbstractArtifactDiscoverer */ protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns ) { - String[] blacklisted; + List allExcludes = new ArrayList(); + allExcludes.addAll( FileUtils.getDefaultExcludesAsList() ); + allExcludes.addAll( Arrays.asList( STANDARD_DISCOVERY_EXCLUDES ) ); + if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 ) { - blacklisted = blacklistedPatterns.split( "," ); + allExcludes.addAll( Arrays.asList( blacklistedPatterns.split( "," ) ) ); } - else - { - blacklisted = EMPTY_STRING_ARRAY; - } - - String[] allExcludes = new String[STANDARD_DISCOVERY_EXCLUDES.length + blacklisted.length]; - - System.arraycopy( STANDARD_DISCOVERY_EXCLUDES, 0, allExcludes, 0, STANDARD_DISCOVERY_EXCLUDES.length ); - System.arraycopy( blacklisted, 0, allExcludes, STANDARD_DISCOVERY_EXCLUDES.length, blacklisted.length ); DirectoryScanner scanner = new DirectoryScanner(); scanner.setBasedir( repositoryBase ); - scanner.setExcludes( allExcludes ); + scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) ); scanner.scan(); 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 410434d3f..b7883e54d 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 @@ -18,11 +18,11 @@ package org.apache.maven.repository.discovery; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; +import org.codehaus.plexus.util.StringUtils; import java.io.File; import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.StringTokenizer; @@ -93,27 +93,12 @@ public class DefaultArtifactDiscoverer pathParts.remove( 0 ); // the remaining are the groupId. - StringBuffer groupBuffer = new StringBuffer(); + Collections.reverse( pathParts ); + String groupId = StringUtils.join( pathParts.iterator(), "." ); - boolean firstPart = true; - for ( Iterator it = pathParts.iterator(); it.hasNext(); ) - { - String part = (String) it.next(); + result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar" ); - groupBuffer.append( part ); - - if ( firstPart ) - { - firstPart = false; - } - else if ( it.hasNext() ) - { - groupBuffer.append( "." ); - } - } - - result = artifactFactory.createArtifact( groupBuffer.toString(), artifactId, version, Artifact.SCOPE_RUNTIME, - "jar" ); + result.setFile( new File( path ) ); return result; } 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 ce2d1abd5..5a279e04d 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 @@ -16,6 +16,8 @@ package org.apache.maven.repository.discovery; * limitations under the License. */ +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; import org.codehaus.plexus.PlexusTestCase; import java.io.File; @@ -33,6 +35,8 @@ public class DefaultArtifactDiscovererTest { private ArtifactDiscoverer discoverer; + private ArtifactFactory factory; + private File repositoryLocation; protected void setUp() @@ -42,29 +46,90 @@ public class DefaultArtifactDiscovererTest discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "default" ); + factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + repositoryLocation = getTestFile( "src/test/repository" ); } public void testDefaultExcludes() { List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false ); - assertNotNull( "Check artifacts returned", artifacts ); - assertTrue( "Check no artifacts returned", artifacts.isEmpty() ); + assertNotNull( "Check artifacts not null", artifacts ); boolean found = false; - for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext(); ) + for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; ) { String path = (String) i.next(); - if ( !path.startsWith( ".svn" ) ) - { - assertEquals( "Check the excluded path", "KEYS", path ); - if ( found ) - { - fail( "KEYS entry found twice" ); - } - found = true; - } + found = path.indexOf( ".svn" ) >= 0; } assertTrue( "Check exclusion was found", found ); + + for ( Iterator i = artifacts.iterator(); i.hasNext(); ) + { + Artifact a = (Artifact) i.next(); + assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 ); + } } + + public void testStandardExcludes() + { + List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false ); + assertNotNull( "Check artifacts not null", artifacts ); + boolean found = false; + for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; ) + { + String path = (String) i.next(); + + found = path.equals( "KEYS" ); + } + assertTrue( "Check exclusion was found", found ); + + for ( Iterator i = artifacts.iterator(); i.hasNext(); ) + { + Artifact a = (Artifact) i.next(); + assertFalse( "Check not KEYS", a.getFile().getName().equals( "KEYS" ) ); + } + } + + public void testBlacklistedExclude() + { + List artifacts = discoverer.discoverArtifacts( repositoryLocation, "javax/**", false ); + assertNotNull( "Check artifacts not null", artifacts ); + boolean found = false; + for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; ) + { + String path = (String) i.next(); + + found = path.replace( '\\', '/' ).equals( "javax/sql/jdbc/2.0/jdbc-2.0.jar" ); + } + assertTrue( "Check exclusion was found", found ); + + assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) ); + } + + public void testSnapshotInclusion() + { + List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, true ); + assertNotNull( "Check artifacts not null", artifacts ); + + assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) ); + assertTrue( "Check snapshot included", + artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) ); + } + + public void testSnapshotExclusion() + { + List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false ); + assertNotNull( "Check artifacts not null", artifacts ); + + assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) ); + assertFalse( "Check snapshot included", + artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) ); + } + + private Artifact createArtifact( String groupId, String artifactId, String version ) + { + return factory.createArtifact( groupId, artifactId, version, null, "jar" ); + } + } diff --git a/maven-repository-discovery/src/test/repository/javax/sql/jdbc/2.0/jdbc-2.0.jar b/maven-repository-discovery/src/test/repository/javax/sql/jdbc/2.0/jdbc-2.0.jar new file mode 100644 index 000000000..e69de29bb diff --git a/maven-repository-discovery/src/test/repository/org/apache/maven/test/1.0-SNAPSHOT/maven-test-1.0-20050611.112233-1.jar b/maven-repository-discovery/src/test/repository/org/apache/maven/test/1.0-SNAPSHOT/maven-test-1.0-20050611.112233-1.jar new file mode 100644 index 000000000..e69de29bb