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.repository.ArtifactRepository;
21 import org.apache.maven.model.Model;
22 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
23 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
33 * Base class for artifact discoverers.
36 * @author Brett Porter
38 public abstract class AbstractArtifactDiscoverer
39 extends AbstractDiscoverer
42 * Standard patterns to exclude from discovery as they are not artifacts.
44 private static final String[] STANDARD_DISCOVERY_EXCLUDES = {"bin/**", "reports/**", ".maven/**", "**/*.md5",
45 "**/*.MD5", "**/*.sha1", "**/*.SHA1", "**/*snapshot-version", "*/website/**", "*/licenses/**", "*/licences/**",
46 "**/.htaccess", "**/*.html", "**/*.asc", "**/*.txt", "**/*.xml", "**/README*", "**/CHANGELOG*", "**/KEYS*"};
48 private static final String POM = ".pom";
51 * Scan the repository for artifact paths.
53 private String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns )
55 return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES );
58 protected abstract Artifact buildArtifactFromPath( String path, ArtifactRepository repository );
60 public List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots )
62 if ( !"file".equals( repository.getProtocol() ) )
64 throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
67 File repositoryBase = new File( repository.getBasedir() );
69 List artifacts = new ArrayList();
71 String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
73 for ( int i = 0; i < artifactPaths.length; i++ )
75 String path = artifactPaths[i];
77 Artifact artifact = buildArtifactFromPath( path, repository );
78 if ( artifact != null )
80 if ( includeSnapshots || !artifact.isSnapshot() )
82 artifacts.add( artifact );
87 addKickedOutPath( path );
94 public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns,
95 boolean includeSnapshots )
97 List artifacts = new ArrayList();
99 File repositoryBase = new File( repository.getBasedir() );
101 String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns );
103 for ( int i = 0; i < artifactPaths.length; i++ )
105 String path = artifactPaths[i];
107 if ( path.toLowerCase().endsWith( POM ) )
109 Artifact pomArtifact = buildArtifactFromPath( path, repository );
110 if ( pomArtifact != null )
112 pomArtifact.setFile( new File( repositoryBase, path ) );
115 MavenXpp3Reader mavenReader = new MavenXpp3Reader();
116 String filename = repositoryBase.getAbsolutePath() + "/" + path;
119 Model model = mavenReader.read( new FileReader( filename ) );
120 if ( pomArtifact != null && "pom".equals( model.getPackaging() ) )
122 if ( includeSnapshots || !pomArtifact.isSnapshot() )
124 artifacts.add( model );
128 catch ( FileNotFoundException e )
130 // this should never happen
131 getLogger().error( "Error finding file during POM discovery: " + filename, e );
133 catch ( IOException e )
135 getLogger().error( "Error reading file during POM discovery: " + filename + ": " + e );
137 catch ( XmlPullParserException e )
140 "Parse error reading file during POM discovery: " + filename + ": " + e.getMessage() );