1 package org.apache.maven.repository.discovery;
4 * Copyright 2001-2005 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 //discard the actual artifact filename.
85 pathParts.remove( 0 );
87 // the next one is the version.
88 String version = (String) pathParts.get( 0 );
89 pathParts.remove( 0 );
91 // the next one is the artifactId.
92 String artifactId = (String) pathParts.get( 0 );
93 pathParts.remove( 0 );
95 // the remaining are the groupId.
96 Collections.reverse( pathParts );
97 String groupId = StringUtils.join( pathParts.iterator(), "." );
99 result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar" );
101 result.setFile( new File( path ) );