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.metadata.ArtifactRepositoryMetadata;
21 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
22 import org.apache.maven.artifact.repository.metadata.Metadata;
23 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.io.Reader;
33 import java.net.MalformedURLException;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.StringTokenizer;
42 * This class gets all the paths that contain the metadata files.
44 * @plexus.component role="org.apache.maven.repository.discovery.MetadataDiscoverer" role-hint="default"
46 public class DefaultMetadataDiscoverer
47 extends AbstractDiscoverer
48 implements MetadataDiscoverer
51 * Standard patterns to include in discovery of metadata files.
53 private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml",
54 "**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"};
57 * Search the repository for metadata files.
59 * @param repositoryBase
60 * @param blacklistedPatterns
62 public List discoverMetadata( File repositoryBase, String blacklistedPatterns )
64 List metadataFiles = new ArrayList();
65 String[] metadataPaths =
66 scanForArtifactPaths( repositoryBase, blacklistedPatterns, STANDARD_DISCOVERY_INCLUDES, null );
68 for ( int i = 0; i < metadataPaths.length; i++ )
70 RepositoryMetadata metadata = buildMetadata( repositoryBase.getPath(), metadataPaths[i] );
72 if ( metadata != null )
74 metadataFiles.add( metadata );
78 addKickedOutPath( metadataPaths[i] );
86 * Create RepositoryMetadata object.
88 * @param repo The path to the repository.
89 * @param metadataPath The path to the metadata file.
90 * @return the metadata
92 private RepositoryMetadata buildMetadata( String repo, String metadataPath )
95 String repoPath = repo + "/" + metadataPath;
98 URL url = new File( repoPath ).toURL();
99 InputStream is = url.openStream();
100 Reader reader = new InputStreamReader( is );
101 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
103 m = metadataReader.read( reader );
105 catch ( XmlPullParserException e )
107 getLogger().error( "Error parsing metadata file '" + repoPath + "': " + e.getMessage(), e );
109 catch ( MalformedURLException e )
112 getLogger().error( "Error constructing metadata file '" + repoPath + "': " + e.getMessage(), e );
114 catch ( IOException e )
116 getLogger().error( "Error reading metadata file '" + repoPath + "': " + e.getMessage(), e );
119 RepositoryMetadata repositoryMetadata = null;
122 repositoryMetadata = buildMetadata( m, metadataPath );
124 return repositoryMetadata;
127 private RepositoryMetadata buildMetadata( Metadata m, String metadataPath )
129 String metaGroupId = m.getGroupId();
130 String metaArtifactId = m.getArtifactId();
131 String metaVersion = m.getVersion();
133 // check if the groupId, artifactId and version is in the
135 // parse the path, in reverse order
136 List pathParts = new ArrayList();
137 StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
138 while ( st.hasMoreTokens() )
140 pathParts.add( st.nextToken() );
143 Collections.reverse( pathParts );
144 // remove the metadata file
145 pathParts.remove( 0 );
146 Iterator it = pathParts.iterator();
147 String tmpDir = (String) it.next();
149 //ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
150 //if( metaVersion != null && !metaVersion.equals( "" ) )
152 // VersionRange version = VersionRange.createFromVersion( metaVersion );
155 Artifact artifact = null;
156 if ( metaVersion != null && !"".equals( metaVersion ) )
158 artifact = artifactFactory.createBuildArtifact( metaGroupId, metaArtifactId, metaVersion, "jar" );
162 RepositoryMetadata metadata = null;
163 if ( tmpDir != null && tmpDir.equals( metaVersion ) )
165 if ( artifact != null )
167 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
170 else if ( tmpDir != null && tmpDir.equals( metaArtifactId ) )
173 if ( artifact != null )
175 metadata = new ArtifactRepositoryMetadata( artifact );
180 String groupDir = "";
182 for ( it = pathParts.iterator(); it.hasNext(); )
184 String path = (String) it.next();
191 groupDir = path + "." + groupDir;
197 if ( metaGroupId != null && metaGroupId.equals( groupDir ) )
199 metadata = new GroupRepositoryMetadata( metaGroupId );