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.repository.MetadataResolver;
30 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
31 import org.apache.maven.archiva.common.utils.VersionUtil;
32 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.xml.XMLException;
35 import org.apache.maven.model.CiManagement;
36 import org.apache.maven.model.IssueManagement;
37 import org.apache.maven.model.License;
38 import org.apache.maven.model.Model;
39 import org.apache.maven.model.Organization;
40 import org.apache.maven.model.Scm;
41 import org.apache.maven.model.building.DefaultModelBuildingRequest;
42 import org.apache.maven.model.building.ModelBuilder;
43 import org.apache.maven.model.building.ModelBuildingException;
44 import org.apache.maven.model.building.ModelBuildingRequest;
47 * @plexus.component role="org.apache.archiva.metadata.repository.MetadataResolver" role-hint="maven2"
49 public class Maven2RepositoryMetadataResolver
50 implements MetadataResolver
55 private ModelBuilder builder;
60 private ArchivaConfiguration archivaConfiguration;
63 * @plexus.requirement role-hint="maven2"
65 private RepositoryPathTranslator pathTranslator;
67 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
69 throw new UnsupportedOperationException();
72 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
73 String projectVersion )
75 ManagedRepositoryConfiguration repositoryConfiguration =
76 archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
78 String artifactVersion = projectVersion;
80 File basedir = new File( repositoryConfiguration.getLocation() );
81 if ( VersionUtil.isSnapshot( projectVersion ) )
83 // TODO: need much error handling here for incorrect metadata
86 MavenRepositoryMetadata metadata = MavenRepositoryMetadataReader.read(
87 pathTranslator.toFile( basedir, namespace, projectId, projectVersion, "maven-metadata.xml" ) );
90 artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
91 MavenRepositoryMetadata.Snapshot snapshotVersion = metadata.getSnapshotVersion();
93 artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
95 catch ( XMLException e )
98 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
102 File file = pathTranslator.toFile( basedir, namespace, projectId, projectVersion,
103 projectId + "-" + artifactVersion + ".pom" );
105 ModelBuildingRequest req = new DefaultModelBuildingRequest();
106 req.setProcessPlugins( false );
107 req.setPomFile( file );
108 req.setModelResolver( new RepositoryModelResolver( basedir, pathTranslator ) );
113 model = builder.build( req ).getEffectiveModel();
115 catch ( ModelBuildingException e )
118 throw new RuntimeException( e );
121 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
122 metadata.setCiManagement( convertCiManagement( model.getCiManagement() ) );
123 metadata.setDescription( model.getDescription() );
124 metadata.setId( projectVersion );
125 metadata.setIssueManagement( convertIssueManagement( model.getIssueManagement() ) );
126 metadata.setLicenses( convertLicenses( model.getLicenses() ) );
127 metadata.setName( model.getName() );
128 metadata.setOrganization( convertOrganization( model.getOrganization() ) );
129 metadata.setScm( convertScm( model.getScm() ) );
130 metadata.setUrl( model.getUrl() );
132 MavenProjectFacet facet = new MavenProjectFacet();
133 facet.setGroupId( model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId() );
134 facet.setArtifactId( model.getArtifactId() );
135 facet.setPackaging( model.getPackaging() );
136 if ( model.getParent() != null )
138 MavenProjectParent parent = new MavenProjectParent();
139 parent.setGroupId( model.getParent().getGroupId() );
140 parent.setArtifactId( model.getParent().getArtifactId() );
141 parent.setVersion( model.getParent().getVersion() );
142 facet.setParent( parent );
144 metadata.addFacet( facet );
149 private org.apache.archiva.metadata.model.Scm convertScm( Scm scm )
151 org.apache.archiva.metadata.model.Scm newScm = null;
154 newScm = new org.apache.archiva.metadata.model.Scm();
155 newScm.setConnection( scm.getConnection() );
156 newScm.setDeveloperConnection( scm.getDeveloperConnection() );
157 newScm.setUrl( scm.getUrl() );
162 private org.apache.archiva.metadata.model.Organization convertOrganization( Organization organization )
164 org.apache.archiva.metadata.model.Organization org = null;
165 if ( organization != null )
167 org = new org.apache.archiva.metadata.model.Organization();
168 org.setName( organization.getName() );
169 org.setUrl( organization.getUrl() );
174 private List<org.apache.archiva.metadata.model.License> convertLicenses( List<License> licenses )
176 List<org.apache.archiva.metadata.model.License> l = new ArrayList<org.apache.archiva.metadata.model.License>();
177 for ( License license : licenses )
179 org.apache.archiva.metadata.model.License newLicense = new org.apache.archiva.metadata.model.License();
180 newLicense.setName( license.getName() );
181 newLicense.setUrl( license.getUrl() );
187 private org.apache.archiva.metadata.model.IssueManagement convertIssueManagement( IssueManagement issueManagement )
189 org.apache.archiva.metadata.model.IssueManagement im = null;
190 if ( issueManagement != null )
192 im = new org.apache.archiva.metadata.model.IssueManagement();
193 im.setSystem( issueManagement.getSystem() );
194 im.setUrl( issueManagement.getUrl() );
199 private org.apache.archiva.metadata.model.CiManagement convertCiManagement( CiManagement ciManagement )
201 org.apache.archiva.metadata.model.CiManagement ci = null;
202 if ( ciManagement != null )
204 ci = new org.apache.archiva.metadata.model.CiManagement();
205 ci.setSystem( ciManagement.getSystem() );
206 ci.setUrl( ciManagement.getUrl() );
211 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
212 String projectVersion )
214 throw new UnsupportedOperationException();