1 package org.apache.archiva.repository.maven.metadata.storage;
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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.VersionUtil;
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
24 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
25 import org.apache.archiva.repository.storage.StorageAsset;
26 import org.apache.commons.lang3.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Service;
31 import javax.annotation.PostConstruct;
32 import javax.inject.Inject;
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 StorageAsset toFile(StorageAsset basedir, String namespace, String projectId, String projectVersion, String filename )
83 return basedir.resolve( toPath( namespace, projectId, projectVersion, filename ) );
87 public StorageAsset toFile( StorageAsset 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 if (StringUtils.isNotEmpty( projectId ))
144 path.append( projectId ).append( PATH_SEPARATOR );
148 private void appendNamespace( StringBuilder path, String namespace )
150 if ( StringUtils.isNotEmpty( namespace ) ) {
151 path.append( formatAsDirectory( namespace ) ).append( PATH_SEPARATOR );
156 public StorageAsset toFile( StorageAsset basedir, String namespace, String projectId )
158 return basedir.resolve( toPath( namespace, projectId ) );
162 public StorageAsset toFile( StorageAsset basedir, String namespace )
164 return basedir.resolve( toPath( namespace ) );
167 private String formatAsDirectory( String directory )
169 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
173 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
175 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
177 int len = parts.length;
180 throw new IllegalArgumentException(
181 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
184 String id = parts[--len];
185 String baseVersion = parts[--len];
186 String artifactId = parts[--len];
187 StringBuilder groupIdBuilder = new StringBuilder();
188 for ( int i = 0; i < len - 1; i++ )
190 groupIdBuilder.append( parts[i] );
191 groupIdBuilder.append( '.' );
193 groupIdBuilder.append( parts[len - 1] );
195 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
199 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
202 if ( !id.startsWith( projectId + "-" ) )
204 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
205 + "' doesn't start with artifact ID '" + projectId + "'" );
208 MavenArtifactFacet facet = new MavenArtifactFacet();
210 int index = projectId.length() + 1;
212 String idSubStrFromVersion = id.substring( index );
213 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
215 // non-snapshot versions, or non-timestamped snapshot versions
216 version = projectVersion;
218 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
220 // timestamped snapshots
223 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
224 if ( mainVersionLength == 0 )
226 throw new IllegalArgumentException(
227 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
230 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
232 String timestamp = m.group( 1 );
233 String buildNumber = m.group( 2 );
234 facet.setTimestamp( timestamp );
235 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
236 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
238 catch ( IllegalStateException e )
240 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
241 + "' doesn't contain a timestamped version matching snapshot '"
242 + projectVersion + "'", e);
248 throw new IllegalArgumentException(
249 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
250 + projectVersion + "'" );
255 index += version.length();
256 if ( index == id.length() )
258 // no classifier or extension
264 char c = id.charAt( index );
267 // classifier up until '.'
268 int extIndex = id.indexOf( '.', index );
271 classifier = id.substring( index + 1, extIndex );
272 ext = id.substring( extIndex + 1 );
276 classifier = id.substring( index + 1 );
282 // rest is the extension
284 ext = id.substring( index + 1 );
288 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
289 + "' expected classifier or extension but got '"
290 + id.substring( index ) + "'" );
294 ArtifactMetadata metadata = new ArtifactMetadata();
295 metadata.setId( id );
296 metadata.setNamespace( namespace );
297 metadata.setProject( projectId );
298 metadata.setRepositoryId( repoId );
299 metadata.setProjectVersion( projectVersion );
300 metadata.setVersion( version );
302 facet.setClassifier( classifier );
304 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
305 // to select the correct order to apply multiple extensions mappings to a preferred type
306 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
307 // perhaps the plugins could register missing entries in configuration, then we just use configuration
311 for ( ArtifactMappingProvider mapping : artifactMappingProviders )
313 type = mapping.mapClassifierAndExtensionToType( classifier, ext );
320 // TODO: this is cheating! We should check the POM metadata instead
321 if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
323 type = "maven-plugin";
326 // use extension as default
332 // TODO: should we allow this instead?
335 throw new IllegalArgumentException(
336 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
339 facet.setType( type );
340 metadata.addFacet( facet );
346 public boolean isArtifactIdValidMavenPlugin( String artifactId )
348 return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();