]> source.dussan.org Git - archiva.git/blob
771128ff464898613a1c7319f51f382033183a78
[archiva.git] /
1 package org.apache.archiva.metadata.repository.storage.maven2;
2
3 /*
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
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26
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;
51
52 /**
53  * @plexus.component role="org.apache.archiva.metadata.repository.MetadataResolver" role-hint="maven2"
54  */
55 public class Maven2RepositoryMetadataResolver
56     implements MetadataResolver
57 {
58     /**
59      * @plexus.requirement
60      */
61     private ModelBuilder builder;
62
63     /**
64      * @plexus.requirement
65      */
66     private ArchivaConfiguration archivaConfiguration;
67
68     /**
69      * @plexus.requirement role-hint="maven2"
70      */
71     private RepositoryPathTranslator pathTranslator;
72
73     private final static Logger log = LoggerFactory.getLogger( Maven2RepositoryMetadataResolver.class );
74
75     public ProjectMetadata getProject( String repoId, String namespace, String projectId )
76     {
77         throw new UnsupportedOperationException();
78     }
79
80     public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
81                                                      String projectVersion )
82         throws MetadataResolverException
83     {
84         ManagedRepositoryConfiguration repositoryConfiguration =
85             archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
86
87         String artifactVersion = projectVersion;
88
89         File basedir = new File( repositoryConfiguration.getLocation() );
90         if ( VersionUtil.isSnapshot( projectVersion ) )
91         {
92             File metadataFile =
93                 pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" );
94             try
95             {
96                 MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read( metadataFile );
97
98                 // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
99                 MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion();
100                 if ( snapshotVersion != null )
101                 {
102                     artifactVersion =
103                         artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
104                     artifactVersion =
105                         artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
106                 }
107             }
108             catch ( XMLException e )
109             {
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() );
112             }
113         }
114
115         File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion,
116                                            projectId + "-" + artifactVersion + ".pom" );
117
118         if ( !file.exists() )
119         {
120             // metadata could not be resolved
121             return null;
122         }
123
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 );
129
130         Model model;
131         try
132         {
133             model = builder.build( req ).getEffectiveModel();
134         }
135         catch ( ModelBuildingException e )
136         {
137             throw new MetadataResolverException( "Unable to build Maven POM to derive metadata from: " + e.getMessage(),
138                                                  e );
139         }
140
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() );
153
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 )
159         {
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 );
165         }
166         metadata.addFacet( facet );
167
168         return metadata;
169     }
170
171     private List<org.apache.archiva.metadata.model.Dependency> convertDependencies( List<Dependency> dependencies )
172     {
173         List<org.apache.archiva.metadata.model.Dependency> l =
174             new ArrayList<org.apache.archiva.metadata.model.Dependency>();
175         for ( Dependency dependency : dependencies )
176         {
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 );
188         }
189         return l;
190     }
191
192     private org.apache.archiva.metadata.model.Scm convertScm( Scm scm )
193     {
194         org.apache.archiva.metadata.model.Scm newScm = null;
195         if ( scm != null )
196         {
197             newScm = new org.apache.archiva.metadata.model.Scm();
198             newScm.setConnection( scm.getConnection() );
199             newScm.setDeveloperConnection( scm.getDeveloperConnection() );
200             newScm.setUrl( scm.getUrl() );
201         }
202         return newScm;
203     }
204
205     private org.apache.archiva.metadata.model.Organization convertOrganization( Organization organization )
206     {
207         org.apache.archiva.metadata.model.Organization org = null;
208         if ( organization != null )
209         {
210             org = new org.apache.archiva.metadata.model.Organization();
211             org.setName( organization.getName() );
212             org.setUrl( organization.getUrl() );
213         }
214         return org;
215     }
216
217     private List<org.apache.archiva.metadata.model.License> convertLicenses( List<License> licenses )
218     {
219         List<org.apache.archiva.metadata.model.License> l = new ArrayList<org.apache.archiva.metadata.model.License>();
220         for ( License license : licenses )
221         {
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() );
225             l.add( newLicense );
226         }
227         return l;
228     }
229
230     private List<org.apache.archiva.metadata.model.MailingList> convertMailingLists( List<MailingList> mailingLists )
231     {
232         List<org.apache.archiva.metadata.model.MailingList> l =
233             new ArrayList<org.apache.archiva.metadata.model.MailingList>();
234         for ( MailingList mailingList : mailingLists )
235         {
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 );
245         }
246         return l;
247     }
248
249     private org.apache.archiva.metadata.model.IssueManagement convertIssueManagement( IssueManagement issueManagement )
250     {
251         org.apache.archiva.metadata.model.IssueManagement im = null;
252         if ( issueManagement != null )
253         {
254             im = new org.apache.archiva.metadata.model.IssueManagement();
255             im.setSystem( issueManagement.getSystem() );
256             im.setUrl( issueManagement.getUrl() );
257         }
258         return im;
259     }
260
261     private org.apache.archiva.metadata.model.CiManagement convertCiManagement( CiManagement ciManagement )
262     {
263         org.apache.archiva.metadata.model.CiManagement ci = null;
264         if ( ciManagement != null )
265         {
266             ci = new org.apache.archiva.metadata.model.CiManagement();
267             ci.setSystem( ciManagement.getSystem() );
268             ci.setUrl( ciManagement.getUrl() );
269         }
270         return ci;
271     }
272
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 )
276     {
277         throw new UnsupportedOperationException();
278     }
279
280     public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
281                                                                      String projectVersion )
282     {
283         throw new UnsupportedOperationException();
284     }
285
286     public Collection<String> getRootNamespaces( String repoId )
287     {
288         throw new UnsupportedOperationException();
289     }
290
291     public List<String> getNamespaces( String repoId, String namespace )
292     {
293         throw new UnsupportedOperationException();
294     }
295
296     public Collection<String> getProjects( String repoId, String namespace )
297     {
298         throw new UnsupportedOperationException();
299     }
300
301     public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
302     {
303         throw new UnsupportedOperationException();
304     }
305 }