private Artifact buildArtifact( String path )
{
- try
- {
- StringTokenizer tokens = new StringTokenizer( path, "/\\" );
-
- int numberOfTokens = tokens.countTokens();
-
- if ( numberOfTokens != 3 )
- {
- addKickedOutPath( path );
-
- return null;
- }
+ StringTokenizer tokens = new StringTokenizer( path, "/\\" );
- String groupId = tokens.nextToken();
+ int numberOfTokens = tokens.countTokens();
- String type = tokens.nextToken();
-
- if ( type.endsWith( "s" ) )
- {
- type = type.substring( 0, type.length() - 1 );
- }
-
- // contains artifactId, version, classifier, and extension.
- String avceGlob = tokens.nextToken();
+ if ( numberOfTokens != 3 )
+ {
+ addKickedOutPath( path );
- LinkedList avceTokenList = new LinkedList();
+ return null;
+ }
- StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
- while ( avceTokenizer.hasMoreTokens() )
- {
- avceTokenList.addLast( avceTokenizer.nextToken() );
- }
+ String groupId = tokens.nextToken();
- String lastAvceToken = (String) avceTokenList.removeLast();
+ String type = tokens.nextToken();
- if ( lastAvceToken.endsWith( ".tar.gz" ) )
- {
- type = "distribution-tgz";
+ if ( type.endsWith( "s" ) )
+ {
+ type = type.substring( 0, type.length() - 1 );
+ }
- lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
+ // contains artifactId, version, classifier, and extension.
+ String avceGlob = tokens.nextToken();
- avceTokenList.addLast( lastAvceToken );
- }
- else if ( lastAvceToken.endsWith( "sources.jar" ) )
- {
- type = "java-source";
+ LinkedList avceTokenList = new LinkedList();
- lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
+ StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
+ while ( avceTokenizer.hasMoreTokens() )
+ {
+ avceTokenList.addLast( avceTokenizer.nextToken() );
+ }
- avceTokenList.addLast( lastAvceToken );
- }
- else if ( lastAvceToken.endsWith( ".zip" ) )
- {
- type = "distribution-zip";
+ String lastAvceToken = (String) avceTokenList.removeLast();
- lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
+ if ( lastAvceToken.endsWith( ".tar.gz" ) )
+ {
+ type = "distribution-tgz";
- avceTokenList.addLast( lastAvceToken );
- }
- else
- {
- int extPos = lastAvceToken.lastIndexOf( '.' );
+ lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
- if ( extPos > 0 )
- {
- String ext = lastAvceToken.substring( extPos + 1 );
- if ( type.equals( ext ) )
- {
- lastAvceToken = lastAvceToken.substring( 0, extPos );
-
- avceTokenList.addLast( lastAvceToken );
- }
- else
- {
- addKickedOutPath( path );
-
- return null;
- }
- }
- }
+ avceTokenList.addLast( lastAvceToken );
+ }
+ else if ( lastAvceToken.endsWith( "sources.jar" ) )
+ {
+ type = "java-source";
- // TODO: this is obscene - surely a better way?
- String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
- "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
- "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
- "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
- "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
- "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
- "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)";
+ lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
- // let's discover the version, and whatever's leftover will be either
- // a classifier, or part of the artifactId, depending on position.
- // Since version is at the end, we have to move in from the back.
- Collections.reverse( avceTokenList );
+ avceTokenList.addLast( lastAvceToken );
+ }
+ else if ( lastAvceToken.endsWith( ".zip" ) )
+ {
+ type = "distribution-zip";
- StringBuffer classifierBuffer = new StringBuffer();
- StringBuffer versionBuffer = new StringBuffer();
+ lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
- boolean firstVersionTokenEncountered = false;
- boolean firstToken = true;
+ avceTokenList.addLast( lastAvceToken );
+ }
+ else
+ {
+ int extPos = lastAvceToken.lastIndexOf( '.' );
- int tokensIterated = 0;
- for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+ if ( extPos > 0 )
{
- String token = (String) it.next();
-
- boolean tokenIsVersionPart = token.matches( validVersionParts );
-
- StringBuffer bufferToUpdate;
-
- // NOTE: logic in code is reversed, since we're peeling off the back
- // Any token after the last versionPart will be in the classifier.
- // Any token UP TO first non-versionPart is part of the version.
- if ( !tokenIsVersionPart )
+ String ext = lastAvceToken.substring( extPos + 1 );
+ if ( type.equals( ext ) )
{
- if ( firstVersionTokenEncountered )
- {
- break;
- }
- else
- {
- bufferToUpdate = classifierBuffer;
- }
+ lastAvceToken = lastAvceToken.substring( 0, extPos );
+
+ avceTokenList.addLast( lastAvceToken );
}
else
{
- firstVersionTokenEncountered = true;
+ addKickedOutPath( path );
- bufferToUpdate = versionBuffer;
+ return null;
}
+ }
+ }
- if ( firstToken )
- {
- firstToken = false;
- }
- else
- {
- bufferToUpdate.insert( 0, '-' );
- }
+ // TODO: this is obscene - surely a better way?
+ String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
+ "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
+ "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
+ "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
+ "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
+ "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
+ "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)";
- bufferToUpdate.insert( 0, token );
+ // let's discover the version, and whatever's leftover will be either
+ // a classifier, or part of the artifactId, depending on position.
+ // Since version is at the end, we have to move in from the back.
+ Collections.reverse( avceTokenList );
- tokensIterated++;
- }
+ StringBuffer classifierBuffer = new StringBuffer();
+ StringBuffer versionBuffer = new StringBuffer();
- getLogger().debug( "After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer +
- "\'\no Classifier Buffer: \'" + classifierBuffer + "\'\no Number of Tokens Iterated: " +
- tokensIterated );
+ boolean firstVersionTokenEncountered = false;
+ boolean firstToken = true;
- // Now, restore the proper ordering so we can build the artifactId.
- Collections.reverse( avceTokenList );
+ int tokensIterated = 0;
+ for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+ {
+ String token = (String) it.next();
+
+ boolean tokenIsVersionPart = token.matches( validVersionParts );
- getLogger().debug(
- "Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + avceTokenList );
+ StringBuffer bufferToUpdate;
- // if we didn't find a version, then punt. Use the last token
- // as the version, and set the classifier empty.
- if ( versionBuffer.length() < 1 )
+ // NOTE: logic in code is reversed, since we're peeling off the back
+ // Any token after the last versionPart will be in the classifier.
+ // Any token UP TO first non-versionPart is part of the version.
+ if ( !tokenIsVersionPart )
{
- if ( avceTokenList.size() > 1 )
+ if ( firstVersionTokenEncountered )
{
- int lastIdx = avceTokenList.size() - 1;
-
- versionBuffer.append( avceTokenList.get( lastIdx ) );
- avceTokenList.remove( lastIdx );
+ break;
}
else
{
- getLogger().debug( "Cannot parse version from artifact path: \'" + path + "\'." );
- getLogger().debug(
- "artifact-version-classifier-extension remaining tokens is: \'" + avceTokenList + "\'" );
+ bufferToUpdate = classifierBuffer;
}
-
- classifierBuffer.setLength( 0 );
}
else
{
- getLogger().debug( "Removing " + tokensIterated + " tokens from avce token list." );
+ firstVersionTokenEncountered = true;
- // if everything is kosher, then pop off all the classifier and
- // version tokens, leaving the naked artifact id in the list.
- avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
+ bufferToUpdate = versionBuffer;
}
- getLogger().debug( "Now, remainder of avce token list is:\n" + avceTokenList );
+ if ( firstToken )
+ {
+ firstToken = false;
+ }
+ else
+ {
+ bufferToUpdate.insert( 0, '-' );
+ }
- StringBuffer artifactIdBuffer = new StringBuffer();
+ bufferToUpdate.insert( 0, token );
- firstToken = true;
- for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
- {
- String token = (String) it.next();
+ tokensIterated++;
+ }
- if ( firstToken )
- {
- firstToken = false;
- }
- else
- {
- artifactIdBuffer.append( '-' );
- }
+ getLogger().debug( "After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer +
+ "\'\no Classifier Buffer: \'" + classifierBuffer + "\'\no Number of Tokens Iterated: " + tokensIterated );
- artifactIdBuffer.append( token );
- }
+ // Now, restore the proper ordering so we can build the artifactId.
+ Collections.reverse( avceTokenList );
- String artifactId = artifactIdBuffer.toString();
+ getLogger().debug(
+ "Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + avceTokenList );
- int lastVersionCharIdx = versionBuffer.length() - 1;
- if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
+ // if we didn't find a version, then punt. Use the last token
+ // as the version, and set the classifier empty.
+ if ( versionBuffer.length() < 1 )
+ {
+ if ( avceTokenList.size() > 1 )
{
- versionBuffer.setLength( lastVersionCharIdx );
- }
-
- String version = versionBuffer.toString();
+ int lastIdx = avceTokenList.size() - 1;
- if ( version.length() < 1 )
+ versionBuffer.append( avceTokenList.get( lastIdx ) );
+ avceTokenList.remove( lastIdx );
+ }
+ else
{
- version = null;
+ getLogger().debug( "Cannot parse version from artifact path: \'" + path + "\'." );
+ getLogger().debug(
+ "artifact-version-classifier-extension remaining tokens is: \'" + avceTokenList + "\'" );
}
- getLogger().debug( "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" +
- "artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" + version + "\'\n" +
- "classifier: \'" + classifierBuffer + "\'" );
+ classifierBuffer.setLength( 0 );
+ }
+ else
+ {
+ getLogger().debug( "Removing " + tokensIterated + " tokens from avce token list." );
+
+ // if everything is kosher, then pop off all the classifier and
+ // version tokens, leaving the naked artifact id in the list.
+ avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
+ }
- Artifact result = null;
+ getLogger().debug( "Now, remainder of avce token list is:\n" + avceTokenList );
- if ( classifierBuffer.length() > 0 )
- {
- getLogger().debug( "Creating artifact with classifier." );
+ StringBuffer artifactIdBuffer = new StringBuffer();
- result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
- classifierBuffer.toString() );
+ firstToken = true;
+ for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+ {
+ String token = (String) it.next();
+
+ if ( firstToken )
+ {
+ firstToken = false;
}
else
{
- if ( StringUtils.isNotEmpty( groupId ) && StringUtils.isNotEmpty( artifactId ) &&
- StringUtils.isNotEmpty( version ) && StringUtils.isNotEmpty( type ) )
- {
- result =
- artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
- }
+ artifactIdBuffer.append( '-' );
}
-// getLogger().debug(
-// "Resulting artifact is: " + result + " and has classifier of: "
-// + result.getClassifier() + "\n\n" );
+ artifactIdBuffer.append( token );
+ }
+
+ String artifactId = artifactIdBuffer.toString();
+
+ int lastVersionCharIdx = versionBuffer.length() - 1;
+ if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
+ {
+ versionBuffer.setLength( lastVersionCharIdx );
+ }
+
+ String version = versionBuffer.toString();
- return result;
+ if ( version.length() < 1 )
+ {
+ version = null;
}
- catch ( RuntimeException e )
+
+ getLogger().debug( "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" +
+ "artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" + version + "\'\n" +
+ "classifier: \'" + classifierBuffer + "\'" );
+
+ Artifact result = null;
+
+ if ( classifierBuffer.length() > 0 )
{
- getLogger().error( "While parsing artifact path: \'" + path + "\'...", e );
+ getLogger().debug( "Creating artifact with classifier." );
- throw e;
+ result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
+ classifierBuffer.toString() );
}
+ else
+ {
+ if ( StringUtils.isNotEmpty( groupId ) && StringUtils.isNotEmpty( artifactId ) &&
+ StringUtils.isNotEmpty( version ) && StringUtils.isNotEmpty( type ) )
+ {
+ result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
+ }
+ }
+
+ result.setFile( new File( path ) );
+
+ return result;
}
}
--- /dev/null
+package org.apache.maven.repository.discovery;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * 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;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Test the default artifact discoverer.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ * @version $Id$
+ */
+public class LegacyArtifactDiscovererTest
+ extends PlexusTestCase
+{
+ private ArtifactDiscoverer discoverer;
+
+ private ArtifactFactory factory;
+
+ private File repositoryLocation;
+
+ protected void setUp()
+ throws Exception
+ {
+ super.setUp();
+
+ discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "legacy" );
+
+ factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
+
+ repositoryLocation = getTestFile( "src/test/legacy-repository" );
+ }
+
+ public void testDefaultExcludes()
+ {
+ 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.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.sql/**", 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/jars/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 testKickoutWithShortPath()
+ {
+ List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
+ assertNotNull( "Check artifacts not null", artifacts );
+ boolean found = false;
+ for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+ {
+ String path = (String) i.next();
+
+ found = path.replace( '\\', '/' ).equals( "invalid/invalid-1.0.jar" );
+ }
+ assertTrue( "Check exclusion was found", found );
+
+ for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+ {
+ Artifact a = (Artifact) i.next();
+ assertFalse( "Check not invalid-1.0.jar", a.getFile().getName().equals( "invalid-1.0.jar" ) );
+ }
+ }
+
+ 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" );
+ }
+
+}