1 package org.apache.archiva.metadata.repository.storage.maven2;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Date;
30 import java.util.List;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import org.apache.archiva.checksum.ChecksumAlgorithm;
35 import org.apache.archiva.checksum.ChecksummedFile;
36 import org.apache.archiva.metadata.model.ArtifactMetadata;
37 import org.apache.archiva.metadata.model.ProjectMetadata;
38 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
39 import org.apache.archiva.metadata.model.ProjectVersionReference;
40 import org.apache.archiva.metadata.repository.MetadataRepository;
41 import org.apache.archiva.metadata.repository.filter.AllFilter;
42 import org.apache.archiva.metadata.repository.filter.Filter;
43 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
44 import org.apache.archiva.metadata.repository.storage.StorageMetadataResolver;
45 import org.apache.archiva.reports.RepositoryProblemFacet;
46 import org.apache.maven.archiva.common.utils.VersionUtil;
47 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
48 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
49 import org.apache.maven.archiva.xml.XMLException;
50 import org.apache.maven.model.CiManagement;
51 import org.apache.maven.model.Dependency;
52 import org.apache.maven.model.IssueManagement;
53 import org.apache.maven.model.License;
54 import org.apache.maven.model.MailingList;
55 import org.apache.maven.model.Model;
56 import org.apache.maven.model.Organization;
57 import org.apache.maven.model.Scm;
58 import org.apache.maven.model.building.DefaultModelBuildingRequest;
59 import org.apache.maven.model.building.ModelBuilder;
60 import org.apache.maven.model.building.ModelBuildingException;
61 import org.apache.maven.model.building.ModelBuildingRequest;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * @plexus.component role="org.apache.archiva.metadata.repository.storage.StorageMetadataResolver" role-hint="maven2"
68 public class Maven2RepositoryMetadataResolver
69 implements StorageMetadataResolver
74 private ModelBuilder builder;
79 private ArchivaConfiguration archivaConfiguration;
82 * @plexus.requirement role-hint="maven2"
84 private RepositoryPathTranslator pathTranslator;
89 private MetadataRepository metadataRepository;
91 private final static Logger log = LoggerFactory.getLogger( Maven2RepositoryMetadataResolver.class );
93 private static final String METADATA_FILENAME = "maven-metadata.xml";
95 private static final Filter<String> ALL = new AllFilter<String>();
97 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
99 // TODO: could natively implement the "shared model" concept from the browse action to avoid needing it there?
103 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
104 String projectVersion )
106 ManagedRepositoryConfiguration repositoryConfiguration =
107 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
109 String artifactVersion = projectVersion;
111 File basedir = new File( repositoryConfiguration.getLocation() );
112 if ( VersionUtil.isSnapshot( projectVersion ) )
115 pathTranslator.toFile( basedir, namespace, projectId, projectVersion, METADATA_FILENAME );
118 MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile );
120 // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
121 MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion();
122 if ( snapshotVersion != null )
125 artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
127 artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
130 catch ( XMLException e )
132 // unable to parse metadata - log it, and continue with the version as the original SNAPSHOT version
133 log.warn( "Invalid metadata: " + metadataFile + " - " + e.getMessage() );
137 String id = projectId + "-" + artifactVersion + ".pom";
138 File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion, id );
140 if ( !file.exists() )
142 // TODO: an event mechanism would remove coupling to the problem reporting plugin
143 addProblemReport( repoId, namespace, projectId, projectVersion, "missing-pom",
144 "The artifact's POM file '" + file + "' was missing" );
146 // metadata could not be resolved
150 ModelBuildingRequest req = new DefaultModelBuildingRequest();
151 req.setProcessPlugins( false );
152 req.setPomFile( file );
153 req.setModelResolver( new RepositoryModelResolver( basedir, pathTranslator ) );
154 req.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
159 model = builder.build( req ).getEffectiveModel();
161 catch ( ModelBuildingException e )
163 addProblemReport( repoId, namespace, projectId, projectVersion, "invalid-pom",
164 "The artifact's POM file '" + file + "' was invalid: " + e.getMessage() );
166 // metadata could not be resolved
170 // Check if the POM is in the correct location
171 boolean correctGroupId = namespace.equals( model.getGroupId() );
172 boolean correctArtifactId = projectId.equals( model.getArtifactId() );
173 boolean correctVersion = projectVersion.equals( model.getVersion() );
174 if ( !correctGroupId || !correctArtifactId || !correctVersion )
176 StringBuilder message = new StringBuilder( "Incorrect POM coordinates in '" + file + "':" );
177 if ( !correctGroupId )
179 message.append( "\nIncorrect group ID: " ).append( model.getGroupId() );
181 if ( !correctArtifactId )
183 message.append( "\nIncorrect artifact ID: " ).append( model.getArtifactId() );
185 if ( !correctVersion )
187 message.append( "\nIncorrect version: " ).append( model.getVersion() );
190 addProblemReport( repoId, namespace, projectId, projectVersion, "mislocated-pom", message.toString() );
192 // metadata could not be resolved
196 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
197 metadata.setCiManagement( convertCiManagement( model.getCiManagement() ) );
198 metadata.setDescription( model.getDescription() );
199 metadata.setId( projectVersion );
200 metadata.setIssueManagement( convertIssueManagement( model.getIssueManagement() ) );
201 metadata.setLicenses( convertLicenses( model.getLicenses() ) );
202 metadata.setMailingLists( convertMailingLists( model.getMailingLists() ) );
203 metadata.setDependencies( convertDependencies( model.getDependencies() ) );
204 metadata.setName( model.getName() );
205 metadata.setOrganization( convertOrganization( model.getOrganization() ) );
206 metadata.setScm( convertScm( model.getScm() ) );
207 metadata.setUrl( model.getUrl() );
209 MavenProjectFacet facet = new MavenProjectFacet();
210 facet.setGroupId( model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId() );
211 facet.setArtifactId( model.getArtifactId() );
212 facet.setPackaging( model.getPackaging() );
213 if ( model.getParent() != null )
215 MavenProjectParent parent = new MavenProjectParent();
216 parent.setGroupId( model.getParent().getGroupId() );
217 parent.setArtifactId( model.getParent().getArtifactId() );
218 parent.setVersion( model.getParent().getVersion() );
219 facet.setParent( parent );
221 metadata.addFacet( facet );
226 private void addProblemReport( String repoId, String namespace, String projectId, String projectVersion,
227 String problemId, String message )
229 // TODO: an event mechanism would remove coupling to the problem reporting plugin
230 RepositoryProblemFacet problem = new RepositoryProblemFacet();
231 problem.setProblem( problemId );
232 problem.setMessage( message );
233 problem.setProject( projectId );
234 problem.setNamespace( namespace );
235 problem.setRepositoryId( repoId );
236 problem.setVersion( projectVersion );
238 metadataRepository.addMetadataFacet( repoId, problem );
241 private List<org.apache.archiva.metadata.model.Dependency> convertDependencies( List<Dependency> dependencies )
243 List<org.apache.archiva.metadata.model.Dependency> l =
244 new ArrayList<org.apache.archiva.metadata.model.Dependency>();
245 for ( Dependency dependency : dependencies )
247 org.apache.archiva.metadata.model.Dependency newDependency =
248 new org.apache.archiva.metadata.model.Dependency();
249 newDependency.setArtifactId( dependency.getArtifactId() );
250 newDependency.setClassifier( dependency.getClassifier() );
251 newDependency.setGroupId( dependency.getGroupId() );
252 newDependency.setOptional( dependency.isOptional() );
253 newDependency.setScope( dependency.getScope() );
254 newDependency.setSystemPath( dependency.getSystemPath() );
255 newDependency.setType( dependency.getType() );
256 newDependency.setVersion( dependency.getVersion() );
257 l.add( newDependency );
262 private org.apache.archiva.metadata.model.Scm convertScm( Scm scm )
264 org.apache.archiva.metadata.model.Scm newScm = null;
267 newScm = new org.apache.archiva.metadata.model.Scm();
268 newScm.setConnection( scm.getConnection() );
269 newScm.setDeveloperConnection( scm.getDeveloperConnection() );
270 newScm.setUrl( scm.getUrl() );
275 private org.apache.archiva.metadata.model.Organization convertOrganization( Organization organization )
277 org.apache.archiva.metadata.model.Organization org = null;
278 if ( organization != null )
280 org = new org.apache.archiva.metadata.model.Organization();
281 org.setName( organization.getName() );
282 org.setUrl( organization.getUrl() );
287 private List<org.apache.archiva.metadata.model.License> convertLicenses( List<License> licenses )
289 List<org.apache.archiva.metadata.model.License> l = new ArrayList<org.apache.archiva.metadata.model.License>();
290 for ( License license : licenses )
292 org.apache.archiva.metadata.model.License newLicense = new org.apache.archiva.metadata.model.License();
293 newLicense.setName( license.getName() );
294 newLicense.setUrl( license.getUrl() );
300 private List<org.apache.archiva.metadata.model.MailingList> convertMailingLists( List<MailingList> mailingLists )
302 List<org.apache.archiva.metadata.model.MailingList> l =
303 new ArrayList<org.apache.archiva.metadata.model.MailingList>();
304 for ( MailingList mailingList : mailingLists )
306 org.apache.archiva.metadata.model.MailingList newMailingList =
307 new org.apache.archiva.metadata.model.MailingList();
308 newMailingList.setName( mailingList.getName() );
309 newMailingList.setMainArchiveUrl( mailingList.getArchive() );
310 newMailingList.setPostAddress( mailingList.getPost() );
311 newMailingList.setSubscribeAddress( mailingList.getSubscribe() );
312 newMailingList.setUnsubscribeAddress( mailingList.getUnsubscribe() );
313 newMailingList.setOtherArchives( mailingList.getOtherArchives() );
314 l.add( newMailingList );
319 private org.apache.archiva.metadata.model.IssueManagement convertIssueManagement( IssueManagement issueManagement )
321 org.apache.archiva.metadata.model.IssueManagement im = null;
322 if ( issueManagement != null )
324 im = new org.apache.archiva.metadata.model.IssueManagement();
325 im.setSystem( issueManagement.getSystem() );
326 im.setUrl( issueManagement.getUrl() );
331 private org.apache.archiva.metadata.model.CiManagement convertCiManagement( CiManagement ciManagement )
333 org.apache.archiva.metadata.model.CiManagement ci = null;
334 if ( ciManagement != null )
336 ci = new org.apache.archiva.metadata.model.CiManagement();
337 ci.setSystem( ciManagement.getSystem() );
338 ci.setUrl( ciManagement.getUrl() );
343 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
344 String projectVersion )
346 // TODO: useful, but not implemented yet, not called from DefaultMetadataResolver
347 throw new UnsupportedOperationException();
350 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
351 String projectVersion )
353 // Can't be determined on a Maven 2 repository
354 throw new UnsupportedOperationException();
357 public Collection<String> getRootNamespaces( String repoId )
359 return getRootNamespaces( repoId, ALL );
362 public Collection<String> getRootNamespaces( String repoId, Filter<String> filter )
364 File dir = getRepositoryBasedir( repoId );
366 String[] files = dir.list( new DirectoryFilter( filter ) );
367 return files != null ? Arrays.asList( files ) : Collections.<String>emptyList();
370 private File getRepositoryBasedir( String repoId )
372 ManagedRepositoryConfiguration repositoryConfiguration =
373 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
375 return new File( repositoryConfiguration.getLocation() );
378 public Collection<String> getNamespaces( String repoId, String namespace )
380 return getNamespaces( repoId, namespace, ALL );
383 public Collection<String> getNamespaces( String repoId, String namespace, Filter<String> filter )
385 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace );
387 // scan all the directories which are potential namespaces. Any directories known to be projects are excluded
388 Collection<String> namespaces = new ArrayList<String>();
389 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
392 for ( File file : files )
394 if ( !isProject( file, filter ) )
396 namespaces.add( file.getName() );
403 public Collection<String> getProjects( String repoId, String namespace )
405 return getProjects( repoId, namespace, ALL );
408 public Collection<String> getProjects( String repoId, String namespace, Filter<String> filter )
410 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace );
412 // scan all directories in the namespace, and only include those that are known to be projects
413 Collection<String> projects = new ArrayList<String>();
414 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
417 for ( File file : files )
419 if ( isProject( file, filter ) )
421 projects.add( file.getName() );
428 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
430 return getProjectVersions( repoId, namespace, projectId, ALL );
433 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
434 String projectVersion )
436 return getArtifacts( repoId, namespace, projectId, projectVersion, ALL );
439 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId,
440 Filter<String> filter )
442 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace, projectId );
444 // all directories in a project directory can be considered a version
445 String[] files = dir.list( new DirectoryFilter( filter ) );
446 return files != null ? Arrays.asList( files ) : Collections.<String>emptyList();
449 public Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
450 String projectVersion, Filter<String> filter )
452 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace, projectId, projectVersion );
454 // all files that are not metadata and not a checksum / signature are considered artifacts
455 File[] files = dir.listFiles( new ArtifactDirectoryFilter( filter ) );
457 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
460 for ( File file : files )
462 ArtifactMetadata metadata = new ArtifactMetadata();
463 metadata.setId( file.getName() );
464 metadata.setProject( projectId );
465 metadata.setNamespace( namespace );
466 metadata.setRepositoryId( repoId );
467 metadata.setWhenGathered( new Date() );
468 metadata.setFileLastModified( file.lastModified() );
469 ChecksummedFile checksummedFile = new ChecksummedFile( file );
472 metadata.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
474 catch ( IOException e )
476 log.error( "Unable to checksum file " + file + ": " + e.getMessage() );
480 metadata.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
482 catch ( IOException e )
484 log.error( "Unable to checksum file " + file + ": " + e.getMessage() );
486 metadata.setSize( file.length() );
488 // TODO: very crude, migrate the functionality from the repository-layer here
489 if ( VersionUtil.isGenericSnapshot( projectVersion ) )
492 projectVersion.substring( 0, projectVersion.length() - 8 ); // 8 is length of "SNAPSHOT"
493 System.out.println( file.getName() + " " + mainVersion );
494 Matcher m = Pattern.compile( projectId + "-" + mainVersion + "([0-9]{8}.[0-9]{6}-[0-9]+).*" ).matcher(
497 String version = mainVersion + m.group( 1 );
499 metadata.setVersion( version );
503 metadata.setVersion( projectVersion );
505 artifacts.add( metadata );
511 private boolean isProject( File dir, Filter<String> filter )
513 // scan directories for a valid project version subdirectory, meaning this must be a project directory
514 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
517 for ( File file : files )
519 if ( isProjectVersion( file ) )
526 // if a metadata file is present, check if this is the "artifactId" directory, marking it as a project
527 MavenRepositoryMetadata metadata = readMetadata( dir );
528 if ( metadata != null && dir.getName().equals( metadata.getArtifactId() ) )
536 private boolean isProjectVersion( File dir )
538 final String artifactId = dir.getParentFile().getName();
539 final String projectVersion = dir.getName();
541 // check if there is a POM artifact file to ensure it is a version directory
543 if ( VersionUtil.isSnapshot( projectVersion ) )
545 files = dir.listFiles( new FilenameFilter()
547 public boolean accept( File dir, String name )
549 if ( name.startsWith( artifactId + "-" ) && name.endsWith( ".pom" ) )
551 String v = name.substring( artifactId.length() + 1, name.length() - 4 );
552 v = VersionUtil.getBaseVersion( v );
553 if ( v.equals( projectVersion ) )
564 final String pomFile = artifactId + "-" + projectVersion + ".pom";
565 files = dir.listFiles( new FilenameFilter()
567 public boolean accept( File dir, String name )
569 return pomFile.equals( name );
573 if ( files != null && files.length > 0 )
578 // if a metadata file is present, check if this is the "version" directory, marking it as a project version
579 MavenRepositoryMetadata metadata = readMetadata( dir );
580 if ( metadata != null && projectVersion.equals( metadata.getVersion() ) )
588 private MavenRepositoryMetadata readMetadata( File directory )
590 MavenRepositoryMetadata metadata = null;
591 File metadataFile = new File( directory, METADATA_FILENAME );
592 if ( metadataFile.exists() )
596 metadata = MavenRepositoryMetadataReader.read( metadataFile );
598 catch ( XMLException e )
600 // ignore missing or invalid metadata
606 public void setConfiguration( ArchivaConfiguration configuration )
608 this.archivaConfiguration = configuration;
611 private static class DirectoryFilter
612 implements FilenameFilter
614 private final Filter<String> filter;
616 public DirectoryFilter( Filter<String> filter )
618 this.filter = filter;
621 public boolean accept( File dir, String name )
623 if ( !filter.accept( name ) )
627 else if ( name.startsWith( "." ) )
631 else if ( !new File( dir, name ).isDirectory() )
639 private class ArtifactDirectoryFilter
640 implements FilenameFilter
642 private final Filter<String> filter;
644 public ArtifactDirectoryFilter( Filter<String> filter )
646 this.filter = filter;
649 public boolean accept( File dir, String name )
651 // TODO compare to logic in maven-repository-layer
652 if ( !filter.accept( name ) )
656 else if ( name.startsWith( "." ) )
660 else if ( name.endsWith( ".md5" ) || name.endsWith( ".sha1" ) || name.endsWith( ".asc" ) )
664 else if ( name.equals( METADATA_FILENAME ) )
668 else if ( new File( dir, name ).isDirectory() )