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;
27 import java.util.List;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
32 * @plexus.component role="org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator" role-hint="maven2"
34 public class Maven2RepositoryPathTranslator
35 implements RepositoryPathTranslator
37 private static final char PATH_SEPARATOR = '/';
39 private static final char GROUP_SEPARATOR = '.';
41 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
44 * @plexus.requirement role="org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider"
46 private List<ArtifactMappingProvider> artifactMappingProviders;
48 public Maven2RepositoryPathTranslator()
52 public Maven2RepositoryPathTranslator( List<ArtifactMappingProvider> artifactMappingProviders )
54 this.artifactMappingProviders = artifactMappingProviders;
57 public File toFile( File basedir, String namespace, String projectId, String projectVersion, String filename )
59 return new File( basedir, toPath( namespace, projectId, projectVersion, filename ) );
62 public File toFile( File basedir, String namespace, String projectId, String projectVersion )
64 return new File( basedir, toPath( namespace, projectId, projectVersion ) );
67 public String toPath( String namespace, String projectId, String projectVersion, String filename )
69 StringBuilder path = new StringBuilder();
71 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
72 path.append( PATH_SEPARATOR );
73 path.append( filename );
75 return path.toString();
78 private void appendNamespaceToProjectVersion( StringBuilder path, String namespace, String projectId,
79 String projectVersion )
81 appendNamespaceAndProject( path, namespace, projectId );
82 path.append( projectVersion );
85 public String toPath( String namespace, String projectId, String projectVersion )
87 StringBuilder path = new StringBuilder();
89 appendNamespaceToProjectVersion( path, namespace, projectId, projectVersion );
91 return path.toString();
94 public String toPath( String namespace )
96 StringBuilder path = new StringBuilder();
98 appendNamespace( path, namespace );
100 return path.toString();
103 public String toPath( String namespace, String projectId )
105 StringBuilder path = new StringBuilder();
107 appendNamespaceAndProject( path, namespace, projectId );
109 return path.toString();
112 private void appendNamespaceAndProject( StringBuilder path, String namespace, String projectId )
114 appendNamespace( path, namespace );
115 path.append( projectId ).append( PATH_SEPARATOR );
118 private void appendNamespace( StringBuilder path, String namespace )
120 path.append( formatAsDirectory( namespace ) ).append( PATH_SEPARATOR );
123 public File toFile( File basedir, String namespace, String projectId )
125 return new File( basedir, toPath( namespace, projectId ) );
128 public File toFile( File basedir, String namespace )
130 return new File( basedir, toPath( namespace ) );
133 private String formatAsDirectory( String directory )
135 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
138 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
140 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
142 int len = parts.length;
145 throw new IllegalArgumentException(
146 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
149 String id = parts[--len];
150 String baseVersion = parts[--len];
151 String artifactId = parts[--len];
152 StringBuilder groupIdBuilder = new StringBuilder();
153 for ( int i = 0; i < len - 1; i++ )
155 groupIdBuilder.append( parts[i] );
156 groupIdBuilder.append( '.' );
158 groupIdBuilder.append( parts[len - 1] );
160 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
163 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
166 if ( !id.startsWith( projectId + "-" ) )
168 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id +
169 "' doesn't start with artifact ID '" + projectId + "'" );
172 MavenArtifactFacet facet = new MavenArtifactFacet();
174 int index = projectId.length() + 1;
176 String idSubStrFromVersion = id.substring( index );
177 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
179 // non-snapshot versions, or non-timestamped snapshot versions
180 version = projectVersion;
182 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
184 // timestamped snapshots
187 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
188 if ( mainVersionLength == 0 )
190 throw new IllegalArgumentException(
191 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
194 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
196 String timestamp = m.group( 1 );
197 String buildNumber = m.group( 2 );
198 facet.setTimestamp( timestamp );
199 facet.setBuildNumber( Integer.valueOf( buildNumber ) );
200 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
202 catch ( IllegalStateException e )
204 throw new IllegalArgumentException(
205 "Not a valid artifact path in a Maven 2 repository, filename '" + id +
206 "' doesn't contain a timestamped version matching snapshot '" + projectVersion + "'" );
212 throw new IllegalArgumentException(
213 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '" +
214 projectVersion + "'" );
219 index += version.length();
220 if ( index == id.length() )
222 // no classifier or extension
228 char c = id.charAt( index );
231 // classifier up until last '.'
232 int extIndex = id.lastIndexOf( '.' );
233 if ( extIndex > index )
235 classifier = id.substring( index + 1, extIndex );
236 ext = id.substring( extIndex + 1 );
240 classifier = id.substring( index + 1 );
246 // rest is the extension
248 ext = id.substring( index + 1 );
252 throw new IllegalArgumentException(
253 "Not a valid artifact path in a Maven 2 repository, filename '" + id +
254 "' expected classifier or extension but got '" + id.substring( index ) + "'" );
258 ArtifactMetadata metadata = new ArtifactMetadata();
259 metadata.setId( id );
260 metadata.setNamespace( namespace );
261 metadata.setProject( projectId );
262 metadata.setRepositoryId( repoId );
263 metadata.setVersion( version );
265 facet.setClassifier( classifier );
267 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
268 // to select the correct order to apply multiple extensions mappings to a preferred type
269 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
270 // perhaps the plugins could register missing entries in configuration, then we just use configuration
274 for ( ArtifactMappingProvider mapping : artifactMappingProviders )
276 type = mapping.mapClassifierAndExtensionToType( classifier, ext );
283 // TODO: this is cheating! We should check the POM metadata instead
284 if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
286 type = "maven-plugin";
289 // use extension as default
295 // TODO: should we allow this instead?
298 throw new IllegalArgumentException(
299 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
302 facet.setType( type );
303 metadata.addFacet( facet );
308 private static final Pattern MAVEN_PLUGIN_PATTERN = Pattern.compile( "^(maven-.*-plugin)|(.*-maven-plugin)$" );
310 public boolean isArtifactIdValidMavenPlugin( String artifactId )
312 return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();