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
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
24 import org.apache.maven.archiva.common.utils.VersionUtil;
25 import org.springframework.context.ApplicationContext;
26 import org.springframework.stereotype.Service;
28 import javax.annotation.PostConstruct;
29 import javax.inject.Inject;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
37 * plexus.component role="org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator" role-hint="maven2"
39 @Service( "repositoryPathTranslator#maven2" )
40 public class Maven2RepositoryPathTranslator
41 implements RepositoryPathTranslator
43 private static final char PATH_SEPARATOR = '/';
45 private static final char GROUP_SEPARATOR = '.';
47 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
50 //private ApplicationContext applicationContext;
53 * plexus.requirement role="org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider"
57 private List<ArtifactMappingProvider> artifactMappingProviders;
59 public Maven2RepositoryPathTranslator()
65 public void initialize()
68 //artifactMappingProviders = new ArrayList<ArtifactMappingProvider>(
69 // applicationContext.getBeansOfType( ArtifactMappingProvider.class ).values() );
74 public Maven2RepositoryPathTranslator( List<ArtifactMappingProvider> artifactMappingProviders )
76 this.artifactMappingProviders = artifactMappingProviders;
79 public File toFile( File basedir, String namespace, String projectId, String projectVersion, String filename )
81 return new File( basedir, toPath( namespace, projectId, projectVersion, filename ) );
84 public File toFile( File basedir, String namespace, String projectId, String projectVersion )
86 return new File( basedir, toPath( namespace, projectId, projectVersion ) );
89 public String toPath( String namespace, String projectId, String projectVersion, String filename )
91 StringBuilder path = new StringBuilder();
93 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
94 path.append( PATH_SEPARATOR );
95 path.append( filename );
97 return path.toString();
100 private void appendNamespaceToProjectVersion( StringBuilder path, String namespace, String projectId,
101 String projectVersion )
103 appendNamespaceAndProject( path, namespace, projectId );
104 path.append( projectVersion );
107 public String toPath( String namespace, String projectId, String projectVersion )
109 StringBuilder path = new StringBuilder();
111 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
113 return path.toString();
116 public String toPath( String namespace )
118 StringBuilder path = new StringBuilder();
120 appendNamespace( path, namespace );
122 return path.toString();
125 public String toPath( String namespace, String projectId )
127 StringBuilder path = new StringBuilder();
129 appendNamespaceAndProject( path, namespace, projectId );
131 return path.toString();
134 private void appendNamespaceAndProject( StringBuilder path, String namespace, String projectId )
136 appendNamespace( path, namespace );
137 path.append( projectId ).append( PATH_SEPARATOR );
140 private void appendNamespace( StringBuilder path, String namespace )
142 path.append( formatAsDirectory( namespace ) ).append( PATH_SEPARATOR );
145 public File toFile( File basedir, String namespace, String projectId )
147 return new File( basedir, toPath( namespace, projectId ) );
150 public File toFile( File basedir, String namespace )
152 return new File( basedir, toPath( namespace ) );
155 private String formatAsDirectory( String directory )
157 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
160 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
162 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
164 int len = parts.length;
167 throw new IllegalArgumentException(
168 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
171 String id = parts[--len];
172 String baseVersion = parts[--len];
173 String artifactId = parts[--len];
174 StringBuilder groupIdBuilder = new StringBuilder();
175 for ( int i = 0; i < len - 1; i++ )
177 groupIdBuilder.append( parts[i] );
178 groupIdBuilder.append( '.' );
180 groupIdBuilder.append( parts[len - 1] );
182 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
185 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
188 if ( !id.startsWith( projectId + "-" ) )
190 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
191 + "' doesn't start with artifact ID '" + projectId + "'" );
194 MavenArtifactFacet facet = new MavenArtifactFacet();
196 int index = projectId.length() + 1;
198 String idSubStrFromVersion = id.substring( index );
199 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
201 // non-snapshot versions, or non-timestamped snapshot versions
202 version = projectVersion;
204 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
206 // timestamped snapshots
209 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
210 if ( mainVersionLength == 0 )
212 throw new IllegalArgumentException(
213 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
216 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
218 String timestamp = m.group( 1 );
219 String buildNumber = m.group( 2 );
220 facet.setTimestamp( timestamp );
221 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
222 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
224 catch ( IllegalStateException e )
226 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
227 + "' doesn't contain a timestamped version matching snapshot '"
228 + projectVersion + "'" );
234 throw new IllegalArgumentException(
235 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
236 + projectVersion + "'" );
241 index += version.length();
242 if ( index == id.length() )
244 // no classifier or extension
250 char c = id.charAt( index );
253 // classifier up until '.'
254 int extIndex = id.indexOf( '.', index );
257 classifier = id.substring( index + 1, extIndex );
258 ext = id.substring( extIndex + 1 );
262 classifier = id.substring( index + 1 );
268 // rest is the extension
270 ext = id.substring( index + 1 );
274 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
275 + "' expected classifier or extension but got '"
276 + id.substring( index ) + "'" );
280 ArtifactMetadata metadata = new ArtifactMetadata();
281 metadata.setId( id );
282 metadata.setNamespace( namespace );
283 metadata.setProject( projectId );
284 metadata.setRepositoryId( repoId );
285 metadata.setProjectVersion( projectVersion );
286 metadata.setVersion( version );
288 facet.setClassifier( classifier );
290 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
291 // to select the correct order to apply multiple extensions mappings to a preferred type
292 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
293 // perhaps the plugins could register missing entries in configuration, then we just use configuration
297 for ( ArtifactMappingProvider mapping : artifactMappingProviders )
299 type = mapping.mapClassifierAndExtensionToType( classifier, ext );
306 // TODO: this is cheating! We should check the POM metadata instead
307 if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
309 type = "maven-plugin";
312 // use extension as default
318 // TODO: should we allow this instead?
321 throw new IllegalArgumentException(
322 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
325 facet.setType( type );
326 metadata.addFacet( facet );
331 private static final Pattern MAVEN_PLUGIN_PATTERN = Pattern.compile( "^(maven-.*-plugin)|(.*-maven-plugin)$" );
333 public boolean isArtifactIdValidMavenPlugin( String artifactId )
335 return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();