mirror of
https://github.com/apache/archiva.git
synced 2025-02-22 01:44:47 +00:00
[MRM-1282] Repository Path Translation consolidation
- DefaultPathParser now a shell around RepositoryPathTranslator, for refactoring away git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@921366 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2211cb809d
commit
f97e798940
@ -24,7 +24,6 @@
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* ArtifactExtensionMapping
|
||||
@ -41,8 +40,6 @@ public class ArtifactExtensionMapping
|
||||
|
||||
private static final Map<String, String> typeToExtensionMap;
|
||||
|
||||
private static final Pattern mavenPluginPattern = Pattern.compile( "^(maven-.*-plugin)|(.*-maven-plugin)$" );
|
||||
|
||||
// TODO: won't support extensions - need to refactor away this class
|
||||
private static final ArtifactMappingProvider mapping = new DefaultArtifactMappingProvider();
|
||||
|
||||
@ -79,17 +76,6 @@ public static String getExtension( String type )
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a given artifact Id conforms to the naming scheme for a maven plugin.
|
||||
*
|
||||
* @param artifactId the artifactId to test.
|
||||
* @return true if this artifactId conforms to the naming scheme for a maven plugin.
|
||||
*/
|
||||
public static boolean isMavenPlugin( String artifactId )
|
||||
{
|
||||
return mavenPluginPattern.matcher( artifactId ).matches();
|
||||
}
|
||||
|
||||
public static String mapExtensionAndClassifierToType( String classifier, String extension )
|
||||
{
|
||||
return mapExtensionAndClassifierToType( classifier, extension, extension );
|
||||
|
@ -24,8 +24,8 @@
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.maven.archiva.common.utils.VersionUtil;
|
||||
import org.apache.maven.archiva.model.ArtifactReference;
|
||||
import org.apache.maven.archiva.repository.layout.LayoutException;
|
||||
|
||||
@ -71,80 +71,13 @@ public ArtifactReference toArtifactReference( String path )
|
||||
artifact.setGroupId( metadata.getNamespace() );
|
||||
artifact.setArtifactId( metadata.getProject() );
|
||||
artifact.setVersion( metadata.getVersion() );
|
||||
|
||||
// TODO: use Maven facet instead
|
||||
String filename = metadata.getId();
|
||||
FilenameParser parser = new FilenameParser( filename );
|
||||
artifact.setArtifactId( parser.expect( artifact.getArtifactId() ) );
|
||||
if ( artifact.getArtifactId() == null )
|
||||
MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
|
||||
if ( facet != null )
|
||||
{
|
||||
throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
|
||||
+ "should start with artifactId as stated in path." );
|
||||
artifact.setClassifier( facet.getClassifier() );
|
||||
artifact.setType( facet.getType() );
|
||||
}
|
||||
String baseVersion = VersionUtil.getBaseVersion( metadata.getVersion() );
|
||||
artifact.setVersion( parser.expect( baseVersion ) );
|
||||
if ( artifact.getVersion() == null )
|
||||
{
|
||||
// We working with a snapshot?
|
||||
if ( VersionUtil.isSnapshot( baseVersion ) )
|
||||
{
|
||||
artifact.setVersion( parser.nextVersion() );
|
||||
if ( !VersionUtil.isUniqueSnapshot( artifact.getVersion() ) )
|
||||
{
|
||||
throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid,"
|
||||
+ "expected timestamp format in filename." );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
|
||||
+ "expected version as stated in path." );
|
||||
}
|
||||
}
|
||||
switch(parser.seperator())
|
||||
{
|
||||
case '-':
|
||||
// Definately a classifier.
|
||||
artifact.setClassifier( parser.remaining() );
|
||||
|
||||
// Set the type.
|
||||
artifact.setType( ArtifactExtensionMapping.mapExtensionAndClassifierToType( artifact.getClassifier(), parser.getExtension() ) );
|
||||
break;
|
||||
case '.':
|
||||
// We have an dual extension possibility.
|
||||
String extension = parser.remaining() + '.' + parser.getExtension();
|
||||
artifact.setType( extension );
|
||||
break;
|
||||
case 0:
|
||||
// End of the filename, only a simple extension left. - Set the type.
|
||||
String type = ArtifactExtensionMapping.mapExtensionToType( parser.getExtension() );
|
||||
if ( type == null )
|
||||
{
|
||||
throw new LayoutException( "Invalid artifact: no type was specified" );
|
||||
}
|
||||
artifact.setType( type );
|
||||
break;
|
||||
}
|
||||
if ( StringUtils.equals( "jar", artifact.getType() ) &&
|
||||
ArtifactExtensionMapping.isMavenPlugin( artifact.getArtifactId() ) )
|
||||
{
|
||||
artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
|
||||
}
|
||||
|
||||
// Sanity Checks.
|
||||
|
||||
// Do we have a snapshot version?
|
||||
if ( VersionUtil.isSnapshot( artifact.getVersion() ) )
|
||||
{
|
||||
// Rules are different for SNAPSHOTS
|
||||
if ( !VersionUtil.isGenericSnapshot( baseVersion ) )
|
||||
{
|
||||
String filenameBaseVersion = VersionUtil.getBaseVersion( artifact.getVersion() );
|
||||
throw new LayoutException( "Invalid snapshot artifact location, version directory should be "
|
||||
+ filenameBaseVersion );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return artifact;
|
||||
}
|
||||
|
||||
|
@ -19,39 +19,57 @@
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import org.apache.archiva.metadata.model.ArtifactMetadata;
|
||||
import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
|
||||
import org.apache.archiva.metadata.repository.storage.maven2.MavenArtifactFacet;
|
||||
import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* ArtifactExtensionMappingTest
|
||||
* ArtifactExtensionMappingTest
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
public class ArtifactExtensionMappingTest
|
||||
extends AbstractRepositoryLayerTestCase
|
||||
extends AbstractRepositoryLayerTestCase
|
||||
{
|
||||
private RepositoryPathTranslator pathTranslator = new Maven2RepositoryPathTranslator(
|
||||
Collections.<ArtifactMappingProvider>emptyList() );
|
||||
|
||||
public void testIsMavenPlugin()
|
||||
{
|
||||
assertMavenPlugin( "maven-test-plugin" );
|
||||
assertMavenPlugin( "maven-clean-plugin" );
|
||||
assertMavenPlugin( "cobertura-maven-plugin" );
|
||||
assertMavenPlugin( "maven-project-info-reports-plugin" );
|
||||
assertMavenPlugin( "silly-name-for-a-maven-plugin" );
|
||||
|
||||
assertNotMavenPlugin( "maven-plugin-api" );
|
||||
assertNotMavenPlugin( "foo-lib" );
|
||||
assertNotMavenPlugin( "another-maven-plugin-api" );
|
||||
assertNotMavenPlugin( "secret-maven-plugin-2" );
|
||||
assertMavenPlugin( "maven-test-plugin" );
|
||||
assertMavenPlugin( "maven-clean-plugin" );
|
||||
assertMavenPlugin( "cobertura-maven-plugin" );
|
||||
assertMavenPlugin( "maven-project-info-reports-plugin" );
|
||||
assertMavenPlugin( "silly-name-for-a-maven-plugin" );
|
||||
|
||||
assertNotMavenPlugin( "maven-plugin-api" );
|
||||
assertNotMavenPlugin( "foo-lib" );
|
||||
assertNotMavenPlugin( "another-maven-plugin-api" );
|
||||
assertNotMavenPlugin( "secret-maven-plugin-2" );
|
||||
}
|
||||
|
||||
|
||||
private void assertMavenPlugin( String artifactId )
|
||||
{
|
||||
assertTrue( "Should be detected as maven plugin: <" + artifactId + ">",
|
||||
ArtifactExtensionMapping.isMavenPlugin( artifactId ) );
|
||||
assertEquals( "Should be detected as maven plugin: <" + artifactId + ">", "maven-plugin", getTypeFromArtifactId(
|
||||
artifactId ) );
|
||||
}
|
||||
|
||||
|
||||
private String getTypeFromArtifactId( String artifactId )
|
||||
{
|
||||
ArtifactMetadata artifact = pathTranslator.getArtifactFromId( null, "groupId", artifactId, "1.0",
|
||||
artifactId + "-1.0.jar" );
|
||||
MavenArtifactFacet facet = (MavenArtifactFacet) artifact.getFacet( MavenArtifactFacet.FACET_ID );
|
||||
return facet.getType();
|
||||
}
|
||||
|
||||
private void assertNotMavenPlugin( String artifactId )
|
||||
{
|
||||
assertFalse( "Should NOT be detected as maven plugin: <" + artifactId + ">",
|
||||
ArtifactExtensionMapping.isMavenPlugin( artifactId ) );
|
||||
assertFalse( "Should NOT be detected as maven plugin: <" + artifactId + ">", "maven-plugin".equals(
|
||||
getTypeFromArtifactId( artifactId ) ) );
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ public class DefaultPathParserTest
|
||||
|
||||
public void testBadPathMissingType()
|
||||
{
|
||||
// TODO: should we allow this instead?
|
||||
assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
|
||||
}
|
||||
|
||||
|
@ -28,11 +28,11 @@
|
||||
public class DefaultArtifactMappingProvider
|
||||
implements ArtifactMappingProvider
|
||||
{
|
||||
private final Map<String,String> classifierAndExtensionToTypeMap;
|
||||
private final Map<String, String> classifierAndExtensionToTypeMap;
|
||||
|
||||
public DefaultArtifactMappingProvider()
|
||||
{
|
||||
classifierAndExtensionToTypeMap = new HashMap<String,String>();
|
||||
classifierAndExtensionToTypeMap = new HashMap<String, String>();
|
||||
|
||||
// Maven 2.2.1 supplied types (excluding defaults where extension == type and no classifier)
|
||||
classifierAndExtensionToTypeMap.put( "client:jar", "ejb-client" );
|
||||
@ -43,8 +43,14 @@ public DefaultArtifactMappingProvider()
|
||||
|
||||
public String mapClassifierAndExtensionToType( String classifier, String ext )
|
||||
{
|
||||
classifier = classifier != null ? classifier : "";
|
||||
ext = ext != null ? ext : "";
|
||||
if ( classifier == null )
|
||||
{
|
||||
classifier = "";
|
||||
}
|
||||
if ( ext == null )
|
||||
{
|
||||
ext = "";
|
||||
}
|
||||
return classifierAndExtensionToTypeMap.get( classifier + ":" + ext );
|
||||
}
|
||||
}
|
||||
|
@ -280,10 +280,35 @@ else if ( c == '.' )
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: this is cheating! We should check the POM metadata instead
|
||||
if ( type == null && "jar".equals( ext ) && isArtifactIdValidMavenPlugin( projectId ) )
|
||||
{
|
||||
type = "maven-plugin";
|
||||
}
|
||||
|
||||
// use extension as default
|
||||
facet.setType( type != null ? type : ext );
|
||||
if ( type == null )
|
||||
{
|
||||
type = ext;
|
||||
}
|
||||
|
||||
// TODO: should we allow this instead?
|
||||
if ( type == null )
|
||||
{
|
||||
throw new IllegalArgumentException(
|
||||
"Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
|
||||
}
|
||||
|
||||
facet.setType( type );
|
||||
metadata.addFacet( facet );
|
||||
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private static final Pattern MAVEN_PLUGIN_PATTERN = Pattern.compile( "^(maven-.*-plugin)|(.*-maven-plugin)$" );
|
||||
|
||||
public boolean isArtifactIdValidMavenPlugin( String artifactId )
|
||||
{
|
||||
return MAVEN_PLUGIN_PATTERN.matcher( artifactId ).matches();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user