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
73 .getPath(), metadataPaths[i] );
75 if ( metadata != null )
77 metadataFiles.add( metadata );
81 addKickedOutPath( metadataPaths[i] );
89 * Create RepositoryMetadata object.
91 * @param repo The path to the repository.
92 * @param metadataPath The path to the metadata file.
93 * @return the metadata
95 private RepositoryMetadata buildMetadata( String repo, String metadataPath )
97 RepositoryMetadata metadata = null;
101 URL url = new File( repo + "/" + metadataPath ).toURL();
102 InputStream is = url.openStream();
103 Reader reader = new InputStreamReader( is );
104 MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
106 Metadata m = metadataReader.read( reader );
107 String metaGroupId = m.getGroupId();
108 String metaArtifactId = m.getArtifactId();
109 String metaVersion = m.getVersion();
111 // check if the groupId, artifactId and version is in the
113 // parse the path, in reverse order
114 List pathParts = new ArrayList();
115 StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
116 while ( st.hasMoreTokens() )
118 pathParts.add( st.nextToken() );
121 Collections.reverse( pathParts );
122 // remove the metadata file
123 pathParts.remove( 0 );
124 Iterator it = pathParts.iterator();
125 String tmpDir = (String) it.next();
127 ArtifactHandler handler = new DefaultArtifactHandler( "jar" );
128 VersionRange version = VersionRange.createFromVersion( metaVersion );
130 new DefaultArtifact( metaGroupId, metaArtifactId, version, "compile", "jar", "", handler );
133 if ( tmpDir.equals( metaVersion ) )
135 metadata = new SnapshotArtifactRepositoryMetadata( artifact );
137 else if ( tmpDir.equals( metaArtifactId ) )
140 metadata = new ArtifactRepositoryMetadata( artifact );
145 String groupDir = "";
147 for ( it = pathParts.iterator(); it.hasNext(); )
151 groupDir = (String) it.next();
155 groupDir = (String) it.next() + "." + groupDir;
161 if ( metaGroupId.equals( groupDir ) )
163 metadata = new GroupRepositoryMetadata( metaGroupId );
168 catch ( FileNotFoundException fe )
170 // TODO: log ignored metadata
172 catch ( XmlPullParserException xe )
174 // TODO: log ignored metadata
176 catch ( IOException ie )
178 // TODO: log ignored metadata