diff options
author | Edwin L. Punzalan <epunzalan@apache.org> | 2006-01-31 09:15:13 +0000 |
---|---|---|
committer | Edwin L. Punzalan <epunzalan@apache.org> | 2006-01-31 09:15:13 +0000 |
commit | af47dd19e0f08152a72f3ce682fd123bb7a32ce2 (patch) | |
tree | 59ed2cdf273eded3fcd1b18c1ae2c13914b12e89 /maven-repository-utils | |
parent | c2713e0c4f52a333d9acd1b1fb1e37f7e279a783 (diff) | |
download | archiva-af47dd19e0f08152a72f3ce682fd123bb7a32ce2.tar.gz archiva-af47dd19e0f08152a72f3ce682fd123bb7a32ce2.zip |
PR: MRM-43
Refactored buildArtifact method from DefaultArtifactDiscoverer.java to a new class in maven-repository-utils for use maven-repository-proxy
git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@373773 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'maven-repository-utils')
-rw-r--r-- | maven-repository-utils/pom.xml | 4 | ||||
-rw-r--r-- | maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java | 188 |
2 files changed, 192 insertions, 0 deletions
diff --git a/maven-repository-utils/pom.xml b/maven-repository-utils/pom.xml index cd65d7719..5d3938087 100644 --- a/maven-repository-utils/pom.xml +++ b/maven-repository-utils/pom.xml @@ -29,5 +29,9 @@ <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-utils</artifactId> </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact</artifactId> + </dependency> </dependencies> </project> diff --git a/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java b/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java new file mode 100644 index 000000000..9de8321ae --- /dev/null +++ b/maven-repository-utils/src/main/java/org/apache/maven/repository/ArtifactUtils.java @@ -0,0 +1,188 @@ +package org.apache.maven.repository; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author Edwin Punzalan + */ +public class ArtifactUtils +{ + public static Artifact buildArtifact( File repositoryBase, String path, ArtifactRepository repository, + ArtifactFactory artifactFactory ) + { + List pathParts = new ArrayList(); + StringTokenizer st = new StringTokenizer( path, "/\\" ); + while ( st.hasMoreTokens() ) + { + pathParts.add( st.nextToken() ); + } + + Collections.reverse( pathParts ); + + Artifact finalResult = null; + if ( pathParts.size() >= 4 ) + { + // the actual artifact filename. + String filename = (String) pathParts.remove( 0 ); + + // the next one is the version. + String version = (String) pathParts.remove( 0 ); + + // the next one is the artifactId. + String artifactId = (String) pathParts.remove( 0 ); + + // the remaining are the groupId. + Collections.reverse( pathParts ); + String groupId = StringUtils.join( pathParts.iterator(), "." ); + + String remainingFilename = filename; + if ( !remainingFilename.startsWith( artifactId + "-" ) ) + { + return null; + } + else + { + remainingFilename = remainingFilename.substring( artifactId.length() + 1 ); + + String classifier = null; + + // TODO: use artifact handler, share with legacy discoverer + String type; + if ( remainingFilename.endsWith( ".tar.gz" ) ) + { + type = "distribution-tgz"; + remainingFilename = + remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() ); + } + else if ( remainingFilename.endsWith( ".zip" ) ) + { + type = "distribution-zip"; + remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() ); + } + else if ( remainingFilename.endsWith( "-sources.jar" ) ) + { + type = "java-source"; + classifier = "sources"; + remainingFilename = + remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() ); + } + else + { + int index = remainingFilename.lastIndexOf( "." ); + if ( index < 0 ) + { + return null; + } + else + { + type = remainingFilename.substring( index + 1 ); + remainingFilename = remainingFilename.substring( 0, index ); + } + } + + if ( type != null ) + { + Artifact result; + + if ( classifier == null ) + { + result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, + type ); + } + else + { + result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, + classifier ); + } + + if ( result.isSnapshot() ) + { + // version is XXX-SNAPSHOT, filename is XXX-yyyyMMdd.hhmmss-b + int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 ); + if ( classifierIndex >= 0 ) + { + classifier = remainingFilename.substring( classifierIndex + 1 ); + remainingFilename = remainingFilename.substring( 0, classifierIndex ); + result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, + remainingFilename, type, + classifier ); + } + else + { + result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename, + Artifact.SCOPE_RUNTIME, type ); + } + + // poor encapsulation requires we do this to populate base version + if ( !result.isSnapshot() ) + { + return null; + } + else if ( !result.getBaseVersion().equals( version ) ) + { + return null; + } + else + { + finalResult = result; + } + } + else if ( !remainingFilename.startsWith( version ) ) + { + return null; + } + else if ( !remainingFilename.equals( version ) ) + { + if ( remainingFilename.charAt( version.length() ) != '-' ) + { + return null; + } + else + { + classifier = remainingFilename.substring( version.length() + 1 ); + finalResult = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, + type, classifier ); + } + } + else + { + finalResult = result; + } + } + } + } + + if ( finalResult != null ) + { + finalResult.setRepository( repository ); + finalResult.setFile( new File( repositoryBase, path ) ); + } + + return finalResult; + } +} |