[MRM-1360] move type population into Maven 2 plugin

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@921224 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2010-03-10 04:02:48 +00:00
parent 989f143a5f
commit 780176f7f5
23 changed files with 251 additions and 46 deletions

View File

@ -19,6 +19,9 @@ package org.apache.maven.archiva.repository.content;
* under the License.
*/
import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider;
import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
@ -40,21 +43,28 @@ public class ArtifactExtensionMapping
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();
static
{
typeToExtensionMap = new HashMap<String, String>();
typeToExtensionMap.put( "ejb-client", "jar" );
typeToExtensionMap.put( "ejb", "jar" );
typeToExtensionMap.put( "java-source", "jar" );
typeToExtensionMap.put( "javadoc", "jar" );
typeToExtensionMap.put( "test-jar", "jar" );
typeToExtensionMap.put( MAVEN_PLUGIN, "jar" );
typeToExtensionMap.put( MAVEN_ARCHETYPE, "jar" );
// TODO: move to maven 1 plugin
typeToExtensionMap.put( MAVEN_ONE_PLUGIN, "jar" );
typeToExtensionMap.put( "javadoc.jar", "jar" );
typeToExtensionMap.put( "uberjar", "jar" );
typeToExtensionMap.put( "distribution-tgz", "tar.gz" );
typeToExtensionMap.put( "distribution-zip", "zip" );
typeToExtensionMap.put( "java-source", "jar" );
typeToExtensionMap.put( "javadoc.jar", "jar" );
typeToExtensionMap.put( "javadoc", "jar" );
typeToExtensionMap.put( "aspect", "jar" );
typeToExtensionMap.put( "uberjar", "jar" );
typeToExtensionMap.put( MAVEN_PLUGIN, "jar" );
typeToExtensionMap.put( MAVEN_ONE_PLUGIN, "jar" );
typeToExtensionMap.put( MAVEN_ARCHETYPE, "jar" );
}
public static String getExtension( String type )
@ -62,7 +72,7 @@ public class ArtifactExtensionMapping
// Try specialized types first.
if ( typeToExtensionMap.containsKey( type ) )
{
return (String) typeToExtensionMap.get( type );
return typeToExtensionMap.get( type );
}
// Return type
@ -88,36 +98,37 @@ public class ArtifactExtensionMapping
public static String mapExtensionAndClassifierToType( String classifier, String extension,
String defaultExtension )
{
if ( "sources".equals( classifier ) )
String value = mapping.mapClassifierAndExtensionToType( classifier, extension );
if ( value == null )
{
return "java-source";
value = mapToMaven1Type( extension );
}
else if ( "javadoc".equals( classifier ) )
{
return "javadoc";
}
return mapExtensionToType( extension, defaultExtension );
return value != null ? value : defaultExtension;
}
public static String mapExtensionToType( String extension )
{
return mapExtensionToType( extension, extension );
String value = mapToMaven1Type( extension );
return value != null ? value : extension;
}
private static String mapExtensionToType( String extension, String defaultExtension )
private static String mapToMaven1Type( String extension )
{
// TODO: Maven 1 plugin
String value = null;
if ( "tar.gz".equals( extension ) )
{
return "distribution-tgz";
value = "distribution-tgz";
}
else if ( "tar.bz2".equals( extension ) )
{
return "distribution-bzip";
value = "distribution-bzip";
}
else if ( "zip".equals( extension ) )
{
return "distribution-zip";
value = "distribution-zip";
}
return defaultExtension;
return value;
}
}

View File

@ -5,9 +5,9 @@
<version>1.0-SNAPSHOT</version>
<versioning>
<snapshot>
<timestamp>20100308.230825</timestamp>
<buildNumber>1</buildNumber>
<timestamp>20100310.014828</timestamp>
<buildNumber>2</buildNumber>
</snapshot>
<lastUpdated>20100308230825</lastUpdated>
<lastUpdated>20100310014828</lastUpdated>
</versioning>
</metadata>

View File

@ -1 +1 @@
7210867ac16cd955db89cfca699e7c96
aad6835e3e7a4a9de5318ce9183eab54

View File

@ -1 +1 @@
271faeb2039d026ee6a3c937375b5ff7d4b1d605
73669371dc2fdae2fa9a129932ed0932d62d9e6b

View File

@ -0,0 +1,24 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.test</groupId>
<artifactId>test-artifact</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-artifact</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>test</id>
<url>file:${basedir}/repository</url>
</repository>
</distributionManagement>
</project>

View File

@ -4,9 +4,10 @@
<artifactId>test-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<versioning>
<release>1.0-SNAPSHOT</release>
<versions>
<version>1.0-SNAPSHOT</version>
</versions>
<lastUpdated>20100308230825</lastUpdated>
<lastUpdated>20100310014828</lastUpdated>
</versioning>
</metadata>

View File

@ -1 +1 @@
93afaac7340faadc7efd097952d79fa4
4c0a7072d66cfe2deebd3eb998332ccb

View File

@ -1 +1 @@
d59d588343d61fd7d838984a0daa4aaa2546bf9d
64c28127c4923ff352021bdfdad448d2d881ef3b

View File

@ -0,0 +1,25 @@
package org.apache.archiva.metadata.repository.storage.maven2;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
public interface ArtifactMappingProvider
{
String mapClassifierAndExtensionToType( String classifier, String ext );
}

View File

@ -0,0 +1,50 @@
package org.apache.archiva.metadata.repository.storage.maven2;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.util.HashMap;
import java.util.Map;
/**
* @plexus.component role="org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider" role-hint="default"
*/
public class DefaultArtifactMappingProvider
implements ArtifactMappingProvider
{
private final Map<String,String> classifierAndExtensionToTypeMap;
public DefaultArtifactMappingProvider()
{
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" );
classifierAndExtensionToTypeMap.put( "sources:jar", "java-source" );
classifierAndExtensionToTypeMap.put( "javadoc:jar", "javadoc" );
classifierAndExtensionToTypeMap.put( "tests:jar", "test-jar" );
}
public String mapClassifierAndExtensionToType( String classifier, String ext )
{
classifier = classifier != null ? classifier : "";
ext = ext != null ? ext : "";
return classifierAndExtensionToTypeMap.get( classifier + ":" + ext );
}
}

View File

@ -1,13 +1,5 @@
package org.apache.archiva.metadata.repository.storage.maven2;
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
import org.apache.maven.archiva.common.utils.VersionUtil;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@ -27,6 +19,15 @@ import java.util.regex.Pattern;
* under the License.
*/
import org.apache.archiva.metadata.model.ArtifactMetadata;
import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
import org.apache.maven.archiva.common.utils.VersionUtil;
import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @plexus.component role="org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator" role-hint="maven2"
*/
@ -39,6 +40,11 @@ public class Maven2RepositoryPathTranslator
private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
/**
* @plexus.requirement role="org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider"
*/
private List<ArtifactMappingProvider> artifactMappingProviders;
public File toFile( File basedir, String namespace, String projectId, String projectVersion, String filename )
{
return new File( basedir, toPath( namespace, projectId, projectVersion, filename ) );
@ -180,6 +186,8 @@ public class Maven2RepositoryPathTranslator
m.matches();
String timestamp = m.group( 1 );
String buildNumber = m.group( 2 );
facet.setTimestamp( timestamp );
facet.setBuildNumber( Integer.valueOf( buildNumber ) );
version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
}
catch ( IllegalStateException e )
@ -246,8 +254,25 @@ public class Maven2RepositoryPathTranslator
metadata.setVersion( version );
facet.setClassifier( classifier );
// TODO: migrate here from ArtifactExtensionMapping and make extensible
// facet.setType( );
// we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
// to select the correct order to apply multiple extensions mappings to a preferred type
// TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
// perhaps the plugins could register missing entries in configuration, then we just use configuration
// here?
String type = null;
for ( ArtifactMappingProvider mapping : artifactMappingProviders )
{
type = mapping.mapClassifierAndExtensionToType( classifier, ext );
if ( type != null )
{
break;
}
}
// use extension as default
facet.setType( type != null ? type : ext );
metadata.addFacet( facet );
return metadata;

View File

@ -133,7 +133,6 @@ public class Maven2RepositoryMetadataResolverTest
assertDependency( dependencies.get( 9 ), "easymock", "easymockclassextension", "1.2", "test" );
}
// TODO: test snapshot, rest of artifacts
public void testGetArtifactMetadata()
throws Exception
{
@ -156,8 +155,7 @@ public class Maven2RepositoryMetadataResolverTest
assertEquals( 0, facet.getBuildNumber() );
assertNull( facet.getTimestamp() );
assertEquals( "sources", facet.getClassifier() );
// TODO
// assertEquals( "java-source", facet.getType() );
assertEquals( "java-source", facet.getType() );
artifactMetadata = artifacts.get( 1 );
assertEquals( "plexus-spring-1.2.jar", artifactMetadata.getId() );
@ -165,8 +163,7 @@ public class Maven2RepositoryMetadataResolverTest
assertEquals( 0, facet.getBuildNumber() );
assertNull( facet.getTimestamp() );
assertNull( facet.getClassifier() );
// TODO
// assertEquals( "jar", facet.getType() );
assertEquals( "jar", facet.getType() );
artifactMetadata = artifacts.get( 2 );
assertEquals( "plexus-spring-1.2.pom", artifactMetadata.getId() );
@ -174,8 +171,72 @@ public class Maven2RepositoryMetadataResolverTest
assertEquals( 0, facet.getBuildNumber() );
assertNull( facet.getTimestamp() );
assertNull( facet.getClassifier() );
// TODO
// assertEquals( "pom", facet.getType() );
assertEquals( "pom", facet.getType() );
}
public void testGetArtifactMetadataSnapshots()
throws Exception
{
Collection<ArtifactMetadata> testArtifacts = resolver.getArtifacts( TEST_REPO_ID, "com.example.test",
"test-artifact", "1.0-SNAPSHOT" );
List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( testArtifacts );
Collections.sort( artifacts, new Comparator<ArtifactMetadata>()
{
public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
{
return o1.getId().compareTo( o2.getId() );
}
} );
assertEquals( 6, artifacts.size() );
ArtifactMetadata artifactMetadata = artifacts.get( 0 );
assertEquals( "test-artifact-1.0-20100308.230825-1.jar", artifactMetadata.getId() );
MavenArtifactFacet facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
assertEquals( 1, facet.getBuildNumber() );
assertEquals( "20100308.230825", facet.getTimestamp() );
assertNull( facet.getClassifier() );
assertEquals( "jar", facet.getType() );
artifactMetadata = artifacts.get( 1 );
assertEquals( "test-artifact-1.0-20100308.230825-1.pom", artifactMetadata.getId() );
facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
assertEquals( 1, facet.getBuildNumber() );
assertEquals( "20100308.230825", facet.getTimestamp() );
assertNull( facet.getClassifier() );
assertEquals( "pom", facet.getType() );
artifactMetadata = artifacts.get( 2 );
assertEquals( "test-artifact-1.0-20100310.014828-2-javadoc.jar", artifactMetadata.getId() );
facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
assertEquals( 2, facet.getBuildNumber() );
assertEquals( "20100310.014828", facet.getTimestamp() );
assertEquals( "javadoc", facet.getClassifier() );
assertEquals( "javadoc", facet.getType() );
artifactMetadata = artifacts.get( 3 );
assertEquals( "test-artifact-1.0-20100310.014828-2-sources.jar", artifactMetadata.getId() );
facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
assertEquals( 2, facet.getBuildNumber() );
assertEquals( "20100310.014828", facet.getTimestamp() );
assertEquals( "sources", facet.getClassifier() );
assertEquals( "java-source", facet.getType() );
artifactMetadata = artifacts.get( 4 );
assertEquals( "test-artifact-1.0-20100310.014828-2.jar", artifactMetadata.getId() );
facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
assertEquals( 2, facet.getBuildNumber() );
assertEquals( "20100310.014828", facet.getTimestamp() );
assertNull( facet.getClassifier() );
assertEquals( "jar", facet.getType() );
artifactMetadata = artifacts.get( 5 );
assertEquals( "test-artifact-1.0-20100310.014828-2.pom", artifactMetadata.getId() );
facet = (MavenArtifactFacet) artifactMetadata.getFacet( MavenArtifactFacet.FACET_ID );
assertEquals( 2, facet.getBuildNumber() );
assertEquals( "20100310.014828", facet.getTimestamp() );
assertNull( facet.getClassifier() );
assertEquals( "pom", facet.getType() );
}
private void assertDependency( Dependency dependency, String groupId, String artifactId, String version )