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.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
27 import org.apache.archiva.metadata.model.ProjectMetadata;
28 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
29 import org.apache.archiva.metadata.model.ProjectVersionReference;
30 import org.apache.archiva.metadata.repository.MetadataResolver;
31 import org.apache.archiva.metadata.repository.MetadataResolverException;
32 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
33 import org.apache.maven.archiva.common.utils.VersionUtil;
34 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
35 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
36 import org.apache.maven.archiva.xml.XMLException;
37 import org.apache.maven.model.CiManagement;
38 import org.apache.maven.model.Dependency;
39 import org.apache.maven.model.IssueManagement;
40 import org.apache.maven.model.License;
41 import org.apache.maven.model.MailingList;
42 import org.apache.maven.model.Model;
43 import org.apache.maven.model.Organization;
44 import org.apache.maven.model.Scm;
45 import org.apache.maven.model.building.DefaultModelBuildingRequest;
46 import org.apache.maven.model.building.ModelBuilder;
47 import org.apache.maven.model.building.ModelBuildingException;
48 import org.apache.maven.model.building.ModelBuildingRequest;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataResolver" role-hint="maven2"
55 public class Maven2RepositoryMetadataResolver
56 implements MetadataResolver
61 private ModelBuilder builder;
66 private ArchivaConfiguration archivaConfiguration;
69 * @plexus.requirement role-hint="maven2"
71 private RepositoryPathTranslator pathTranslator;
73 private final static Logger log = LoggerFactory.getLogger( Maven2RepositoryMetadataResolver.class );
75 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
77 throw new UnsupportedOperationException();
80 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
81 String projectVersion )
82 throws MetadataResolverException
84 ManagedRepositoryConfiguration repositoryConfiguration =
85 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
87 String artifactVersion = projectVersion;
89 File basedir = new File( repositoryConfiguration.getLocation() );
90 if ( VersionUtil.isSnapshot( projectVersion ) )
93 pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" );
96 MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile );
98 // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
99 MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion();
100 if ( snapshotVersion != null )
103 artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
105 artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
108 catch ( XMLException e )
110 // unable to parse metadata - log it, and continue with the version as the original SNAPSHOT version
111 log.warn( "Invalid metadata: " + metadataFile + " - " + e.getMessage() );
115 File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion,
116 projectId + "-" + artifactVersion + ".pom" );
118 if ( !file.exists() )
120 // metadata could not be resolved
124 ModelBuildingRequest req = new DefaultModelBuildingRequest();
125 req.setProcessPlugins( false );
126 req.setPomFile( file );
127 req.setModelResolver( new RepositoryModelResolver( basedir, pathTranslator ) );
128 req.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
133 model = builder.build( req ).getEffectiveModel();
135 catch ( ModelBuildingException e )
137 throw new MetadataResolverException( "Unable to build Maven POM to derive metadata from: " + e.getMessage(),
141 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
142 metadata.setCiManagement( convertCiManagement( model.getCiManagement() ) );
143 metadata.setDescription( model.getDescription() );
144 metadata.setId( projectVersion );
145 metadata.setIssueManagement( convertIssueManagement( model.getIssueManagement() ) );
146 metadata.setLicenses( convertLicenses( model.getLicenses() ) );
147 metadata.setMailingLists( convertMailingLists( model.getMailingLists() ) );
148 metadata.setDependencies( convertDependencies( model.getDependencies() ) );
149 metadata.setName( model.getName() );
150 metadata.setOrganization( convertOrganization( model.getOrganization() ) );
151 metadata.setScm( convertScm( model.getScm() ) );
152 metadata.setUrl( model.getUrl() );
154 MavenProjectFacet facet = new MavenProjectFacet();
155 facet.setGroupId( model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId() );
156 facet.setArtifactId( model.getArtifactId() );
157 facet.setPackaging( model.getPackaging() );
158 if ( model.getParent() != null )
160 MavenProjectParent parent = new MavenProjectParent();
161 parent.setGroupId( model.getParent().getGroupId() );
162 parent.setArtifactId( model.getParent().getArtifactId() );
163 parent.setVersion( model.getParent().getVersion() );
164 facet.setParent( parent );
166 metadata.addFacet( facet );
171 private List<org.apache.archiva.metadata.model.Dependency> convertDependencies( List<Dependency> dependencies )
173 List<org.apache.archiva.metadata.model.Dependency> l =
174 new ArrayList<org.apache.archiva.metadata.model.Dependency>();
175 for ( Dependency dependency : dependencies )
177 org.apache.archiva.metadata.model.Dependency newDependency =
178 new org.apache.archiva.metadata.model.Dependency();
179 newDependency.setArtifactId( dependency.getArtifactId() );
180 newDependency.setClassifier( dependency.getClassifier() );
181 newDependency.setGroupId( dependency.getGroupId() );
182 newDependency.setOptional( dependency.isOptional() );
183 newDependency.setScope( dependency.getScope() );
184 newDependency.setSystemPath( dependency.getSystemPath() );
185 newDependency.setType( dependency.getType() );
186 newDependency.setVersion( dependency.getVersion() );
187 l.add( newDependency );
192 private org.apache.archiva.metadata.model.Scm convertScm( Scm scm )
194 org.apache.archiva.metadata.model.Scm newScm = null;
197 newScm = new org.apache.archiva.metadata.model.Scm();
198 newScm.setConnection( scm.getConnection() );
199 newScm.setDeveloperConnection( scm.getDeveloperConnection() );
200 newScm.setUrl( scm.getUrl() );
205 private org.apache.archiva.metadata.model.Organization convertOrganization( Organization organization )
207 org.apache.archiva.metadata.model.Organization org = null;
208 if ( organization != null )
210 org = new org.apache.archiva.metadata.model.Organization();
211 org.setName( organization.getName() );
212 org.setUrl( organization.getUrl() );
217 private List<org.apache.archiva.metadata.model.License> convertLicenses( List<License> licenses )
219 List<org.apache.archiva.metadata.model.License> l = new ArrayList<org.apache.archiva.metadata.model.License>();
220 for ( License license : licenses )
222 org.apache.archiva.metadata.model.License newLicense = new org.apache.archiva.metadata.model.License();
223 newLicense.setName( license.getName() );
224 newLicense.setUrl( license.getUrl() );
230 private List<org.apache.archiva.metadata.model.MailingList> convertMailingLists( List<MailingList> mailingLists )
232 List<org.apache.archiva.metadata.model.MailingList> l =
233 new ArrayList<org.apache.archiva.metadata.model.MailingList>();
234 for ( MailingList mailingList : mailingLists )
236 org.apache.archiva.metadata.model.MailingList newMailingList =
237 new org.apache.archiva.metadata.model.MailingList();
238 newMailingList.setName( mailingList.getName() );
239 newMailingList.setMainArchiveUrl( mailingList.getArchive() );
240 newMailingList.setPostAddress( mailingList.getPost() );
241 newMailingList.setSubscribeAddress( mailingList.getSubscribe() );
242 newMailingList.setUnsubscribeAddress( mailingList.getUnsubscribe() );
243 newMailingList.setOtherArchives( mailingList.getOtherArchives() );
244 l.add( newMailingList );
249 private org.apache.archiva.metadata.model.IssueManagement convertIssueManagement( IssueManagement issueManagement )
251 org.apache.archiva.metadata.model.IssueManagement im = null;
252 if ( issueManagement != null )
254 im = new org.apache.archiva.metadata.model.IssueManagement();
255 im.setSystem( issueManagement.getSystem() );
256 im.setUrl( issueManagement.getUrl() );
261 private org.apache.archiva.metadata.model.CiManagement convertCiManagement( CiManagement ciManagement )
263 org.apache.archiva.metadata.model.CiManagement ci = null;
264 if ( ciManagement != null )
266 ci = new org.apache.archiva.metadata.model.CiManagement();
267 ci.setSystem( ciManagement.getSystem() );
268 ci.setUrl( ciManagement.getUrl() );
273 // TODO: evidence that storage and resolver != repository API - split the interface up
274 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
275 String projectVersion )
277 throw new UnsupportedOperationException();
280 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
281 String projectVersion )
283 throw new UnsupportedOperationException();
286 public Collection<String> getRootNamespaces( String repoId )
288 throw new UnsupportedOperationException();
291 public List<String> getNamespaces( String repoId, String namespace )
293 throw new UnsupportedOperationException();
296 public Collection<String> getProjects( String repoId, String namespace )
298 throw new UnsupportedOperationException();
301 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
303 throw new UnsupportedOperationException();