1 package org.apache.maven.repository.discovery;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.StringTokenizer;
31 * Artifact discoverer for the legacy repository layout (Maven 1.x).
34 * @author Brett Porter
36 public class LegacyArtifactDiscoverer
37 extends AbstractArtifactDiscoverer
38 implements ArtifactDiscoverer
40 private ArtifactFactory artifactFactory;
42 public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean includeSnapshots )
44 List artifacts = new ArrayList();
46 String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
48 for ( int i = 0; i < artifactPaths.length; i++ )
50 String path = artifactPaths[i];
52 Artifact artifact = buildArtifact( path );
53 if ( artifact != null )
55 if ( includeSnapshots || !artifact.isSnapshot() )
57 artifacts.add( artifact );
65 private Artifact buildArtifact( String path )
67 StringTokenizer tokens = new StringTokenizer( path, "/\\" );
69 int numberOfTokens = tokens.countTokens();
71 if ( numberOfTokens != 3 )
73 addKickedOutPath( path );
78 String groupId = tokens.nextToken();
80 String type = tokens.nextToken();
82 if ( !type.endsWith( "s" ) )
84 addKickedOutPath( path );
88 type = type.substring( 0, type.length() - 1 );
90 // contains artifactId, version, classifier, and extension.
91 String avceGlob = tokens.nextToken();
93 LinkedList avceTokenList = new LinkedList();
95 StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
96 while ( avceTokenizer.hasMoreTokens() )
98 avceTokenList.addLast( avceTokenizer.nextToken() );
101 String lastAvceToken = (String) avceTokenList.removeLast();
103 // TODO: share with other discoverer, use artifact handlers instead
104 if ( lastAvceToken.endsWith( ".tar.gz" ) )
106 type = "distribution-tgz";
108 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
110 avceTokenList.addLast( lastAvceToken );
112 else if ( lastAvceToken.endsWith( "sources.jar" ) )
114 type = "java-source";
116 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
118 avceTokenList.addLast( lastAvceToken );
120 else if ( lastAvceToken.endsWith( ".zip" ) )
122 type = "distribution-zip";
124 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
126 avceTokenList.addLast( lastAvceToken );
130 int extPos = lastAvceToken.lastIndexOf( '.' );
134 String ext = lastAvceToken.substring( extPos + 1 );
135 if ( type.equals( ext ) )
137 lastAvceToken = lastAvceToken.substring( 0, extPos );
139 avceTokenList.addLast( lastAvceToken );
143 addKickedOutPath( path );
151 addKickedOutPath( path );
157 // TODO: this is obscene - surely a better way?
158 String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
159 "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
160 "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
161 "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
162 "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
163 "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
164 "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)";
166 // let's discover the version, and whatever's leftover will be either
167 // a classifier, or part of the artifactId, depending on position.
168 // Since version is at the end, we have to move in from the back.
169 Collections.reverse( avceTokenList );
171 StringBuffer classifierBuffer = new StringBuffer();
172 StringBuffer versionBuffer = new StringBuffer();
174 boolean firstVersionTokenEncountered = false;
175 boolean firstToken = true;
177 int tokensIterated = 0;
178 for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
180 String token = (String) it.next();
182 boolean tokenIsVersionPart = token.matches( validVersionParts );
184 StringBuffer bufferToUpdate;
186 // NOTE: logic in code is reversed, since we're peeling off the back
187 // Any token after the last versionPart will be in the classifier.
188 // Any token UP TO first non-versionPart is part of the version.
189 if ( !tokenIsVersionPart )
191 if ( firstVersionTokenEncountered )
197 bufferToUpdate = classifierBuffer;
202 firstVersionTokenEncountered = true;
204 bufferToUpdate = versionBuffer;
213 bufferToUpdate.insert( 0, '-' );
216 bufferToUpdate.insert( 0, token );
221 getLogger().debug( "After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer +
222 "\'\no Classifier Buffer: \'" + classifierBuffer + "\'\no Number of Tokens Iterated: " + tokensIterated );
224 // Now, restore the proper ordering so we can build the artifactId.
225 Collections.reverse( avceTokenList );
228 "Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + avceTokenList );
230 // if we didn't find a version, then punt. Use the last token
231 // as the version, and set the classifier empty.
232 if ( versionBuffer.length() < 1 )
234 if ( avceTokenList.size() > 1 )
236 int lastIdx = avceTokenList.size() - 1;
238 versionBuffer.append( avceTokenList.get( lastIdx ) );
239 avceTokenList.remove( lastIdx );
243 getLogger().debug( "Cannot parse version from artifact path: \'" + path + "\'." );
245 "artifact-version-classifier-extension remaining tokens is: \'" + avceTokenList + "\'" );
248 classifierBuffer.setLength( 0 );
252 getLogger().debug( "Removing " + tokensIterated + " tokens from avce token list." );
254 // if everything is kosher, then pop off all the classifier and
255 // version tokens, leaving the naked artifact id in the list.
256 avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
259 getLogger().debug( "Now, remainder of avce token list is:\n" + avceTokenList );
261 StringBuffer artifactIdBuffer = new StringBuffer();
264 for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
266 String token = (String) it.next();
274 artifactIdBuffer.append( '-' );
277 artifactIdBuffer.append( token );
280 String artifactId = artifactIdBuffer.toString();
282 int lastVersionCharIdx = versionBuffer.length() - 1;
283 if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
285 versionBuffer.setLength( lastVersionCharIdx );
288 String version = versionBuffer.toString();
290 if ( version.length() < 1 )
292 addKickedOutPath( path );
297 getLogger().debug( "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" +
298 "artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" + version + "\'\n" +
299 "classifier: \'" + classifierBuffer + "\'" );
301 Artifact result = null;
303 if ( classifierBuffer.length() > 0 )
305 getLogger().debug( "Creating artifact with classifier." );
307 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
308 classifierBuffer.toString() );
312 result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
315 result.setFile( new File( path ) );