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.model.maven2.MavenArtifactFacet;
24 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
25 import org.apache.archiva.common.utils.VersionUtil;
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;
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;
80 public File toFile( File basedir, String namespace, String projectId, String projectVersion, String filename )
82 return new File( basedir, toPath( namespace, projectId, projectVersion, filename ) );
85 public File toFile( File basedir, String namespace, String projectId, String projectVersion )
87 return new File( basedir, toPath( namespace, projectId, projectVersion ) );
90 public String toPath( String namespace, String projectId, String projectVersion, String filename )
92 StringBuilder path = new StringBuilder();
94 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
95 path.append( PATH_SEPARATOR );
96 path.append( filename );
98 return path.toString();
101 private void appendNamespaceToProjectVersion( StringBuilder path, String namespace, String projectId,
102 String projectVersion )
104 appendNamespaceAndProject( path, namespace, projectId );
105 path.append( projectVersion );
108 public String toPath( String namespace, String projectId, String projectVersion )
110 StringBuilder path = new StringBuilder();
112 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
114 return path.toString();
117 public String toPath( String namespace )
119 StringBuilder path = new StringBuilder();
121 appendNamespace( path, namespace );
123 return path.toString();
126 public String toPath( String namespace, String projectId )
128 StringBuilder path = new StringBuilder();
130 appendNamespaceAndProject( path, namespace, projectId );
132 return path.toString();
135 private void appendNamespaceAndProject( StringBuilder path, String namespace, String projectId )
137 appendNamespace( path, namespace );
138 path.append( projectId ).append( PATH_SEPARATOR );
141 private void appendNamespace( StringBuilder path, String namespace )
143 path.append( formatAsDirectory( namespace ) ).append( PATH_SEPARATOR );
146 public File toFile( File basedir, String namespace, String projectId )
148 return new File( basedir, toPath( namespace, projectId ) );
151 public File toFile( File basedir, String namespace )
153 return new File( basedir, toPath( namespace ) );
156 private String formatAsDirectory( String directory )
158 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
161 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
163 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
165 int len = parts.length;
168 throw new IllegalArgumentException(
169 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
172 String id = parts[--len];
173 String baseVersion = parts[--len];
174 String artifactId = parts[--len];
175 StringBuilder groupIdBuilder = new StringBuilder();
176 for ( int i = 0; i < len - 1; i++ )
178 groupIdBuilder.append( parts[i] );
179 groupIdBuilder.append( '.' );
181 groupIdBuilder.append( parts[len - 1] );
183 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
186 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
189 if ( !id.startsWith( projectId + "-" ) )
191 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
192 + "' doesn't start with artifact ID '" + projectId + "'" );
195 MavenArtifactFacet facet = new MavenArtifactFacet();
197 int index = projectId.length() + 1;
199 String idSubStrFromVersion = id.substring( index );
200 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
202 // non-snapshot versions, or non-timestamped snapshot versions
203 version = projectVersion;
205 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
207 // timestamped snapshots
210 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
211 if ( mainVersionLength == 0 )
213 throw new IllegalArgumentException(
214 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
217 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
219 String timestamp = m.group( 1 );
220 String buildNumber = m.group( 2 );
221 facet.setTimestamp( timestamp );
222 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
223 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
225 catch ( IllegalStateException e )
227 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
228 + "' doesn't contain a timestamped version matching snapshot '"
229 + projectVersion + "'" );
235 throw new IllegalArgumentException(
236 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
237 + projectVersion + "'" );
242 index += version.length();
243 if ( index == id.length() )
245 // no classifier or extension
251 char c = id.charAt( index );
254 // classifier up until '.'
255 int extIndex = id.indexOf( '.', index );
258 classifier = id.substring( index + 1, extIndex );
259 ext = id.substring( extIndex + 1 );
263 classifier = id.substring( index + 1 );
269 // rest is the extension
271 ext = id.substring( index + 1 );
275 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
276 + "' expected classifier or extension but got '"
277 + id.substring( index ) + "'" );
281 ArtifactMetadata metadata = new ArtifactMetadata();
282 metadata.setId( id );
283 metadata.setNamespace( namespace );
284 metadata.setProject( projectId );
285 metadata.setRepositoryId( repoId );
286 metadata.setProjectVersion( projectVersion );
287 metadata.setVersion( version );
289 facet.setClassifier( classifier );
291 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
292 // to select the correct order to apply multiple extensions mappings to a preferred type
293 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
294 // perhaps the plugins could register missing entries in configuration, then we just use configuration
298 for ( ArtifactMappingProvider mapping : artifactMappingProviders )
300 type = mapping.mapClassifierAndExtensionToType( classifier, ext );
307 // TODO: this is cheating! We should check the POM metadata instead
308 if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
310 type = "maven-plugin";
313 // use extension as default
319 // TODO: should we allow this instead?
322 throw new IllegalArgumentException(
323 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
326 facet.setType( type );
327 metadata.addFacet( facet );
333 public boolean isArtifactIdValidMavenPlugin( String artifactId )
335 return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();