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;
21 import org.codehaus.plexus.util.StringUtils;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.StringTokenizer;
30 * Artifact discoverer for the new repository layout (Maven 2.0+).
33 * @author Brett Porter
35 public class DefaultArtifactDiscoverer
36 extends AbstractArtifactDiscoverer
38 private ArtifactFactory artifactFactory;
40 public List discoverArtifacts( File repositoryBase, String blacklistedPatterns, boolean convertSnapshots )
42 List artifacts = new ArrayList();
44 String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
46 for ( int i = 0; i < artifactPaths.length; i++ )
48 String path = artifactPaths[i];
50 Artifact artifact = buildArtifact( path );
52 if ( artifact != null )
54 if ( convertSnapshots || !artifact.isSnapshot() )
56 artifacts.add( artifact );
64 private Artifact buildArtifact( String path )
68 List pathParts = new ArrayList();
69 StringTokenizer st = new StringTokenizer( path, "/\\" );
70 while ( st.hasMoreTokens() )
72 pathParts.add( st.nextToken() );
75 Collections.reverse( pathParts );
77 if ( pathParts.size() < 4 )
79 addKickedOutPath( path );
84 // the actual artifact filename.
85 String filename = (String) pathParts.remove( 0 );
87 // the next one is the version.
88 String version = (String) pathParts.remove( 0 );
90 // the next one is the artifactId.
91 String artifactId = (String) pathParts.remove( 0 );
93 // the remaining are the groupId.
94 Collections.reverse( pathParts );
95 String groupId = StringUtils.join( pathParts.iterator(), "." );
97 String remainingFilename = filename;
98 if ( !remainingFilename.startsWith( artifactId + "-" ) )
100 addKickedOutPath( path );
105 remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
107 String classifier = null;
109 // TODO: use artifact handler, share with legacy discoverer
111 if ( remainingFilename.endsWith( ".tar.gz" ) )
113 type = "distribution-tgz";
114 remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - 7 );
116 else if ( remainingFilename.endsWith( ".zip" ) )
118 type = "distribution-zip";
119 remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - 4 );
121 else if ( remainingFilename.endsWith( "-sources.jar" ) )
123 type = "java-source";
124 classifier = "sources";
125 remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - 12 );
129 int index = remainingFilename.lastIndexOf( "." );
132 addKickedOutPath( path );
137 type = remainingFilename.substring( index + 1 );
138 remainingFilename = remainingFilename.substring( 0, index );
141 if ( classifier == null )
143 result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
147 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
150 if ( result.isSnapshot() )
152 // version is XXX-SNAPSHOT, filename is XXX-yyyyMMdd.hhmmss-b
153 int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
154 if ( classifierIndex >= 0 )
156 classifier = remainingFilename.substring( classifierIndex + 1 );
157 remainingFilename = remainingFilename.substring( 0, classifierIndex );
158 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, remainingFilename, type,
163 result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename, Artifact.SCOPE_RUNTIME,
167 // poor encapsulation requires we do this to populate base version
168 if ( !result.isSnapshot() )
170 addKickedOutPath( path );
174 if ( !result.getBaseVersion().equals( version ) )
176 addKickedOutPath( path );
181 else if ( !remainingFilename.startsWith( version ) )
183 addKickedOutPath( path );
187 else if ( !remainingFilename.equals( version ) )
189 if ( remainingFilename.charAt( version.length() ) != '-' )
191 addKickedOutPath( path );
195 classifier = remainingFilename.substring( version.length() + 1 );
196 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
199 result.setFile( new File( path ) );