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.common.utils.VersionUtil;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
25 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.stereotype.Service;
30 import javax.annotation.PostConstruct;
31 import javax.inject.Inject;
32 import java.nio.file.Path;
33 import java.util.List;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
40 @Service( "repositoryPathTranslator#maven2" )
41 public class Maven2RepositoryPathTranslator
42 implements RepositoryPathTranslator
45 private Logger log = LoggerFactory.getLogger( getClass() );
47 private static final char GROUP_SEPARATOR = '.';
49 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
52 private static final Pattern MAVEN_PLUGIN_PATTERN = Pattern.compile( "^(maven-.*-plugin)|(.*-maven-plugin)$" );
59 private List<ArtifactMappingProvider> artifactMappingProviders;
61 public Maven2RepositoryPathTranslator()
67 public void initialize()
69 //artifactMappingProviders = new ArrayList<ArtifactMappingProvider>(
70 // applicationContext.getBeansOfType( ArtifactMappingProvider.class ).values() );
75 public Maven2RepositoryPathTranslator( List<ArtifactMappingProvider> artifactMappingProviders )
77 this.artifactMappingProviders = artifactMappingProviders;
81 public Path toFile(Path basedir, String namespace, String projectId, String projectVersion, String filename )
83 return basedir.resolve( toPath( namespace, projectId, projectVersion, filename ) );
87 public Path toFile( Path basedir, String namespace, String projectId, String projectVersion )
89 return basedir.resolve( toPath( namespace, projectId, projectVersion ) );
93 public String toPath( String namespace, String projectId, String projectVersion, String filename )
95 StringBuilder path = new StringBuilder();
97 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
98 path.append( PATH_SEPARATOR );
99 path.append( filename );
101 return path.toString();
104 private void appendNamespaceToProjectVersion( StringBuilder path, String namespace, String projectId,
105 String projectVersion )
107 appendNamespaceAndProject( path, namespace, projectId );
108 path.append( projectVersion );
111 public String toPath( String namespace, String projectId, String projectVersion )
113 StringBuilder path = new StringBuilder();
115 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
117 return path.toString();
120 public String toPath( String namespace )
122 StringBuilder path = new StringBuilder();
124 appendNamespace( path, namespace );
126 return path.toString();
130 public String toPath( String namespace, String projectId )
132 StringBuilder path = new StringBuilder();
134 appendNamespaceAndProject( path, namespace, projectId );
136 return path.toString();
139 private void appendNamespaceAndProject( StringBuilder path, String namespace, String projectId )
141 appendNamespace( path, namespace );
142 path.append( projectId ).append( PATH_SEPARATOR );
145 private void appendNamespace( StringBuilder path, String namespace )
147 path.append( formatAsDirectory( namespace ) ).append( PATH_SEPARATOR );
151 public Path toFile( Path basedir, String namespace, String projectId )
153 return basedir.resolve( toPath( namespace, projectId ) );
157 public Path toFile( Path basedir, String namespace )
159 return basedir.resolve( toPath( namespace ) );
162 private String formatAsDirectory( String directory )
164 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
168 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
170 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
172 int len = parts.length;
175 throw new IllegalArgumentException(
176 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
179 String id = parts[--len];
180 String baseVersion = parts[--len];
181 String artifactId = parts[--len];
182 StringBuilder groupIdBuilder = new StringBuilder();
183 for ( int i = 0; i < len - 1; i++ )
185 groupIdBuilder.append( parts[i] );
186 groupIdBuilder.append( '.' );
188 groupIdBuilder.append( parts[len - 1] );
190 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
194 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
197 if ( !id.startsWith( projectId + "-" ) )
199 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
200 + "' doesn't start with artifact ID '" + projectId + "'" );
203 MavenArtifactFacet facet = new MavenArtifactFacet();
205 int index = projectId.length() + 1;
207 String idSubStrFromVersion = id.substring( index );
208 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
210 // non-snapshot versions, or non-timestamped snapshot versions
211 version = projectVersion;
213 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
215 // timestamped snapshots
218 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
219 if ( mainVersionLength == 0 )
221 throw new IllegalArgumentException(
222 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
225 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
227 String timestamp = m.group( 1 );
228 String buildNumber = m.group( 2 );
229 facet.setTimestamp( timestamp );
230 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
231 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
233 catch ( IllegalStateException e )
235 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
236 + "' doesn't contain a timestamped version matching snapshot '"
237 + projectVersion + "'", e);
243 throw new IllegalArgumentException(
244 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
245 + projectVersion + "'" );
250 index += version.length();
251 if ( index == id.length() )
253 // no classifier or extension
259 char c = id.charAt( index );
262 // classifier up until '.'
263 int extIndex = id.indexOf( '.', index );
266 classifier = id.substring( index + 1, extIndex );
267 ext = id.substring( extIndex + 1 );
271 classifier = id.substring( index + 1 );
277 // rest is the extension
279 ext = id.substring( index + 1 );
283 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
284 + "' expected classifier or extension but got '"
285 + id.substring( index ) + "'" );
289 ArtifactMetadata metadata = new ArtifactMetadata();
290 metadata.setId( id );
291 metadata.setNamespace( namespace );
292 metadata.setProject( projectId );
293 metadata.setRepositoryId( repoId );
294 metadata.setProjectVersion( projectVersion );
295 metadata.setVersion( version );
297 facet.setClassifier( classifier );
299 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
300 // to select the correct order to apply multiple extensions mappings to a preferred type
301 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
302 // perhaps the plugins could register missing entries in configuration, then we just use configuration
306 for ( ArtifactMappingProvider mapping : artifactMappingProviders )
308 type = mapping.mapClassifierAndExtensionToType( classifier, ext );
315 // TODO: this is cheating! We should check the POM metadata instead
316 if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
318 type = "maven-plugin";
321 // use extension as default
327 // TODO: should we allow this instead?
330 throw new IllegalArgumentException(
331 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
334 facet.setType( type );
335 metadata.addFacet( facet );
341 public boolean isArtifactIdValidMavenPlugin( String artifactId )
343 return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();