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.apache.archiva.repository.storage.StorageAsset;
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.nio.file.Path;
34 import java.util.List;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
41 @Service( "repositoryPathTranslator#maven2" )
42 public class Maven2RepositoryPathTranslator
43 implements RepositoryPathTranslator
46 private Logger log = LoggerFactory.getLogger( getClass() );
48 private static final char GROUP_SEPARATOR = '.';
50 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
53 private static final Pattern MAVEN_PLUGIN_PATTERN = Pattern.compile( "^(maven-.*-plugin)|(.*-maven-plugin)$" );
60 private List<ArtifactMappingProvider> artifactMappingProviders;
62 public Maven2RepositoryPathTranslator()
68 public void initialize()
70 //artifactMappingProviders = new ArrayList<ArtifactMappingProvider>(
71 // applicationContext.getBeansOfType( ArtifactMappingProvider.class ).values() );
76 public Maven2RepositoryPathTranslator( List<ArtifactMappingProvider> artifactMappingProviders )
78 this.artifactMappingProviders = artifactMappingProviders;
82 public StorageAsset toFile(StorageAsset basedir, String namespace, String projectId, String projectVersion, String filename )
84 return basedir.resolve( toPath( namespace, projectId, projectVersion, filename ) );
88 public StorageAsset toFile( StorageAsset basedir, String namespace, String projectId, String projectVersion )
90 return basedir.resolve( toPath( namespace, projectId, projectVersion ) );
94 public String toPath( String namespace, String projectId, String projectVersion, String filename )
96 StringBuilder path = new StringBuilder();
98 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
99 path.append( PATH_SEPARATOR );
100 path.append( filename );
102 return path.toString();
105 private void appendNamespaceToProjectVersion( StringBuilder path, String namespace, String projectId,
106 String projectVersion )
108 appendNamespaceAndProject( path, namespace, projectId );
109 path.append( projectVersion );
112 public String toPath( String namespace, String projectId, String projectVersion )
114 StringBuilder path = new StringBuilder();
116 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
118 return path.toString();
121 public String toPath( String namespace )
123 StringBuilder path = new StringBuilder();
125 appendNamespace( path, namespace );
127 return path.toString();
131 public String toPath( String namespace, String projectId )
133 StringBuilder path = new StringBuilder();
135 appendNamespaceAndProject( path, namespace, projectId );
137 return path.toString();
140 private void appendNamespaceAndProject( StringBuilder path, String namespace, String projectId )
142 appendNamespace( path, namespace );
143 path.append( projectId ).append( PATH_SEPARATOR );
146 private void appendNamespace( StringBuilder path, String namespace )
148 path.append( formatAsDirectory( namespace ) ).append( PATH_SEPARATOR );
152 public StorageAsset toFile( StorageAsset basedir, String namespace, String projectId )
154 return basedir.resolve( toPath( namespace, projectId ) );
158 public StorageAsset toFile( StorageAsset basedir, String namespace )
160 return basedir.resolve( toPath( namespace ) );
163 private String formatAsDirectory( String directory )
165 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
169 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
171 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
173 int len = parts.length;
176 throw new IllegalArgumentException(
177 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
180 String id = parts[--len];
181 String baseVersion = parts[--len];
182 String artifactId = parts[--len];
183 StringBuilder groupIdBuilder = new StringBuilder();
184 for ( int i = 0; i < len - 1; i++ )
186 groupIdBuilder.append( parts[i] );
187 groupIdBuilder.append( '.' );
189 groupIdBuilder.append( parts[len - 1] );
191 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
195 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
198 if ( !id.startsWith( projectId + "-" ) )
200 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
201 + "' doesn't start with artifact ID '" + projectId + "'" );
204 MavenArtifactFacet facet = new MavenArtifactFacet();
206 int index = projectId.length() + 1;
208 String idSubStrFromVersion = id.substring( index );
209 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
211 // non-snapshot versions, or non-timestamped snapshot versions
212 version = projectVersion;
214 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
216 // timestamped snapshots
219 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
220 if ( mainVersionLength == 0 )
222 throw new IllegalArgumentException(
223 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
226 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
228 String timestamp = m.group( 1 );
229 String buildNumber = m.group( 2 );
230 facet.setTimestamp( timestamp );
231 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
232 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
234 catch ( IllegalStateException e )
236 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
237 + "' doesn't contain a timestamped version matching snapshot '"
238 + projectVersion + "'", e);
244 throw new IllegalArgumentException(
245 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
246 + projectVersion + "'" );
251 index += version.length();
252 if ( index == id.length() )
254 // no classifier or extension
260 char c = id.charAt( index );
263 // classifier up until '.'
264 int extIndex = id.indexOf( '.', index );
267 classifier = id.substring( index + 1, extIndex );
268 ext = id.substring( extIndex + 1 );
272 classifier = id.substring( index + 1 );
278 // rest is the extension
280 ext = id.substring( index + 1 );
284 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
285 + "' expected classifier or extension but got '"
286 + id.substring( index ) + "'" );
290 ArtifactMetadata metadata = new ArtifactMetadata();
291 metadata.setId( id );
292 metadata.setNamespace( namespace );
293 metadata.setProject( projectId );
294 metadata.setRepositoryId( repoId );
295 metadata.setProjectVersion( projectVersion );
296 metadata.setVersion( version );
298 facet.setClassifier( classifier );
300 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
301 // to select the correct order to apply multiple extensions mappings to a preferred type
302 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
303 // perhaps the plugins could register missing entries in configuration, then we just use configuration
307 for ( ArtifactMappingProvider mapping : artifactMappingProviders )
309 type = mapping.mapClassifierAndExtensionToType( classifier, ext );
316 // TODO: this is cheating! We should check the POM metadata instead
317 if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
319 type = "maven-plugin";
322 // use extension as default
328 // TODO: should we allow this instead?
331 throw new IllegalArgumentException(
332 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
335 facet.setType( type );
336 metadata.addFacet( facet );
342 public boolean isArtifactIdValidMavenPlugin( String artifactId )
344 return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();