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
39 private ArtifactFactory artifactFactory;
41 public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean includeSnapshots )
43 List artifacts = new ArrayList();
45 String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
47 for ( int i = 0; i < artifactPaths.length; i++ )
49 String path = artifactPaths[i];
51 Artifact artifact = buildArtifact( path );
52 if ( artifact != null )
54 if ( includeSnapshots || !artifact.isSnapshot() )
56 artifacts.add( artifact );
64 private Artifact buildArtifact( String path )
66 StringTokenizer tokens = new StringTokenizer( path, "/\\" );
68 int numberOfTokens = tokens.countTokens();
70 if ( numberOfTokens != 3 )
72 addKickedOutPath( path );
77 String groupId = tokens.nextToken();
79 String type = tokens.nextToken();
81 if ( !type.endsWith( "s" ) )
83 addKickedOutPath( path );
87 type = type.substring( 0, type.length() - 1 );
89 // contains artifactId, version, classifier, and extension.
90 String avceGlob = tokens.nextToken();
92 LinkedList avceTokenList = new LinkedList();
94 StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
95 while ( avceTokenizer.hasMoreTokens() )
97 avceTokenList.addLast( avceTokenizer.nextToken() );
100 String lastAvceToken = (String) avceTokenList.removeLast();
102 // TODO: share with other discoverer, use artifact handlers instead
103 if ( lastAvceToken.endsWith( ".tar.gz" ) )
105 type = "distribution-tgz";
107 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
109 avceTokenList.addLast( lastAvceToken );
111 else if ( lastAvceToken.endsWith( "sources.jar" ) )
113 type = "java-source";
115 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
117 avceTokenList.addLast( lastAvceToken );
119 else if ( lastAvceToken.endsWith( ".zip" ) )
121 type = "distribution-zip";
123 lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
125 avceTokenList.addLast( lastAvceToken );
129 int extPos = lastAvceToken.lastIndexOf( '.' );
133 String ext = lastAvceToken.substring( extPos + 1 );
134 if ( type.equals( ext ) )
136 lastAvceToken = lastAvceToken.substring( 0, extPos );
138 avceTokenList.addLast( lastAvceToken );
142 addKickedOutPath( path );
150 addKickedOutPath( path );
156 // TODO: this is obscene - surely a better way?
157 String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
158 "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
159 "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
160 "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
161 "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
162 "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
163 "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "([AaBb][_.0-9]*)";
165 // let's discover the version, and whatever's leftover will be either
166 // a classifier, or part of the artifactId, depending on position.
167 // Since version is at the end, we have to move in from the back.
168 Collections.reverse( avceTokenList );
170 StringBuffer classifierBuffer = new StringBuffer();
171 StringBuffer versionBuffer = new StringBuffer();
173 boolean firstVersionTokenEncountered = false;
174 boolean firstToken = true;
176 int tokensIterated = 0;
177 for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
179 String token = (String) it.next();
181 boolean tokenIsVersionPart = token.matches( validVersionParts );
183 StringBuffer bufferToUpdate;
185 // NOTE: logic in code is reversed, since we're peeling off the back
186 // Any token after the last versionPart will be in the classifier.
187 // Any token UP TO first non-versionPart is part of the version.
188 if ( !tokenIsVersionPart )
190 if ( firstVersionTokenEncountered )
196 bufferToUpdate = classifierBuffer;
201 firstVersionTokenEncountered = true;
203 bufferToUpdate = versionBuffer;
212 bufferToUpdate.insert( 0, '-' );
215 bufferToUpdate.insert( 0, token );
220 getLogger().debug( "After parsing loop, state of buffers:\no Version Buffer: \'" + versionBuffer +
221 "\'\no Classifier Buffer: \'" + classifierBuffer + "\'\no Number of Tokens Iterated: " + tokensIterated );
223 // Now, restore the proper ordering so we can build the artifactId.
224 Collections.reverse( avceTokenList );
227 "Before repairing bad version and/or cleaning up used tokens, avce token list is:\n" + avceTokenList );
229 // if we didn't find a version, then punt. Use the last token
230 // as the version, and set the classifier empty.
231 if ( versionBuffer.length() < 1 )
233 if ( avceTokenList.size() > 1 )
235 int lastIdx = avceTokenList.size() - 1;
237 versionBuffer.append( avceTokenList.get( lastIdx ) );
238 avceTokenList.remove( lastIdx );
242 getLogger().debug( "Cannot parse version from artifact path: \'" + path + "\'." );
244 "artifact-version-classifier-extension remaining tokens is: \'" + avceTokenList + "\'" );
247 classifierBuffer.setLength( 0 );
251 getLogger().debug( "Removing " + tokensIterated + " tokens from avce token list." );
253 // if everything is kosher, then pop off all the classifier and
254 // version tokens, leaving the naked artifact id in the list.
255 avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
258 getLogger().debug( "Now, remainder of avce token list is:\n" + avceTokenList );
260 StringBuffer artifactIdBuffer = new StringBuffer();
263 for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
265 String token = (String) it.next();
273 artifactIdBuffer.append( '-' );
276 artifactIdBuffer.append( token );
279 String artifactId = artifactIdBuffer.toString();
281 int lastVersionCharIdx = versionBuffer.length() - 1;
282 if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
284 versionBuffer.setLength( lastVersionCharIdx );
287 String version = versionBuffer.toString();
289 if ( version.length() < 1 )
291 addKickedOutPath( path );
296 getLogger().debug( "Extracted artifact information from path:\n" + "groupId: \'" + groupId + "\'\n" +
297 "artifactId: \'" + artifactId + "\'\n" + "type: \'" + type + "\'\n" + "version: \'" + version + "\'\n" +
298 "classifier: \'" + classifierBuffer + "\'" );
300 Artifact result = null;
302 if ( classifierBuffer.length() > 0 )
304 getLogger().debug( "Creating artifact with classifier." );
306 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
307 classifierBuffer.toString() );
311 result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
314 result.setFile( new File( path ) );