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.DefaultArtifact;
21 import org.apache.maven.artifact.handler.ArtifactHandler;
22 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
23 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.Metadata;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
29 import org.apache.maven.artifact.versioning.VersionRange;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.io.Reader;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.List;
43 import java.util.StringTokenizer;
46 * This class gets all the paths that contain the metadata files.
48 public class DefaultMetadataDiscoverer
49 extends AbstractArtifactDiscoverer
50 implements MetadataDiscoverer
53 * Standard patterns to include in discovery of metadata files.
55 private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml",
56 "**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"};
59 * Search the repository for metadata files.
61 * @param repositoryBase
62 * @param blacklistedPatterns
64 public List discoverMetadata( File repositoryBase, String blacklistedPatterns )
66 List metadataFiles = new ArrayList();
67 String[] metadataPaths =
68 scanForArtifactPaths( repositoryBase, blacklistedPatterns, STANDARD_DISCOVERY_INCLUDES, null );
70 for ( int i = 0; i < metadataPaths.length; i++ )
72 RepositoryMetadata metadata = buildMetadata( repositoryBase.getPath(), metadataPaths[i] );
74 if ( metadata != null )
76 metadataFiles.add( metadata );
80 addKickedOutPath( metadataPaths[i] );
88 * Create RepositoryMetadata object.
90 * @param repo The path to the repository.
91 * @param metadataPath The path to the metadata file.
92 * @return the metadata
94 private RepositoryMetadata buildMetadata( String repo, String metadataPath )
96 RepositoryMetadata metadata = null;
100 URL url = new File( repo + "/" + metadataPath ).toURL();
101 InputStream is = url.openStream();
102 Reader reader = new InputStreamReader( is );
103 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
105 Metadata m = metadataReader.read( reader );
106 String metaGroupId = m.getGroupId();
107 String metaArtifactId = m.getArtifactId();
108 String metaVersion = m.getVersion();
110 // check if the groupId, artifactId and version is in the
112 // parse the path, in reverse order
113 List pathParts = new ArrayList();
114 StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
115 while ( st.hasMoreTokens() )
117 pathParts.add( st.nextToken() );
120 Collections.reverse( pathParts );
121 // remove the metadata file
122 pathParts.remove( 0 );
123 Iterator it = pathParts.iterator();
124 String tmpDir = (String) it.next();
126 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
127 VersionRange version = VersionRange.createFromVersion( metaVersion );
129 new DefaultArtifact( metaGroupId, metaArtifactId, version, "compile", "jar", "", handler );
132 if ( tmpDir.equals( metaVersion ) )
134 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
136 else if ( tmpDir.equals( metaArtifactId ) )
139 metadata = new ArtifactRepositoryMetadata( artifact );
144 String groupDir = "";
146 for ( it = pathParts.iterator(); it.hasNext(); )
150 groupDir = (String) it.next();
154 groupDir = (String) it.next() + "." + groupDir;
160 if ( metaGroupId.equals( groupDir ) )
162 metadata = new GroupRepositoryMetadata( metaGroupId );
167 catch ( FileNotFoundException fe )
169 // TODO: log ignored metadata
171 catch ( XmlPullParserException xe )
173 // TODO: log ignored metadata
175 catch ( IOException ie )
177 // TODO: log ignored metadata