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
37 implements ArtifactDiscoverer
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 );
53 if ( artifact != null )
55 if ( includeSnapshots || !artifact.isSnapshot() )
57 artifacts.add( artifact );
65 private Artifact buildArtifact( String path )
67 List pathParts = new ArrayList();
68 StringTokenizer st = new StringTokenizer( path, "/\\" );
69 while ( st.hasMoreTokens() )
71 pathParts.add( st.nextToken() );
74 Collections.reverse( pathParts );
76 Artifact finalResult = null;
77 if ( pathParts.size() < 4 )
79 addKickedOutPath( path );
83 // the actual artifact filename.
84 String filename = (String) pathParts.remove( 0 );
86 // the next one is the version.
87 String version = (String) pathParts.remove( 0 );
89 // the next one is the artifactId.
90 String artifactId = (String) pathParts.remove( 0 );
92 // the remaining are the groupId.
93 Collections.reverse( pathParts );
94 String groupId = StringUtils.join( pathParts.iterator(), "." );
96 String remainingFilename = filename;
97 if ( !remainingFilename.startsWith( artifactId + "-" ) )
99 addKickedOutPath( path );
103 remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
105 String classifier = null;
107 // TODO: use artifact handler, share with legacy discoverer
109 if ( remainingFilename.endsWith( ".tar.gz" ) )
111 type = "distribution-tgz";
113 remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() );
115 else if ( remainingFilename.endsWith( ".zip" ) )
117 type = "distribution-zip";
118 remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() );
120 else if ( remainingFilename.endsWith( "-sources.jar" ) )
122 type = "java-source";
123 classifier = "sources";
125 remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() );
129 int index = remainingFilename.lastIndexOf( "." );
132 addKickedOutPath( path );
136 type = remainingFilename.substring( index + 1 );
137 remainingFilename = remainingFilename.substring( 0, index );
145 if ( classifier == null )
147 result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME,
152 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
156 if ( result.isSnapshot() )
158 // version is XXX-SNAPSHOT, filename is XXX-yyyyMMdd.hhmmss-b
159 int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
160 if ( classifierIndex >= 0 )
162 classifier = remainingFilename.substring( classifierIndex + 1 );
163 remainingFilename = remainingFilename.substring( 0, classifierIndex );
164 result = artifactFactory.createArtifactWithClassifier( groupId, artifactId,
165 remainingFilename, type,
170 result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename,
171 Artifact.SCOPE_RUNTIME, type );
174 // poor encapsulation requires we do this to populate base version
175 if ( !result.isSnapshot() )
177 addKickedOutPath( path );
179 else if ( !result.getBaseVersion().equals( version ) )
181 addKickedOutPath( path );
185 finalResult = result;
188 else if ( !remainingFilename.startsWith( version ) )
190 addKickedOutPath( path );
192 else if ( !remainingFilename.equals( version ) )
194 if ( remainingFilename.charAt( version.length() ) != '-' )
196 addKickedOutPath( path );
200 classifier = remainingFilename.substring( version.length() + 1 );
201 finalResult = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version,
209 if ( finalResult != null )
211 finalResult.setFile( new File( path ) );