import java.util.HashMap;
import java.util.Map;
-import java.util.regex.Pattern;
/**
* 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();
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 );
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;
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 )
- {
- throw new LayoutException( INVALID_ARTIFACT_PATH + "filename format is invalid, "
- + "should start with artifactId as stated in path." );
- }
- String baseVersion = VersionUtil.getBaseVersion( metadata.getVersion() );
- artifact.setVersion( parser.expect( baseVersion ) );
- if ( artifact.getVersion() == null )
+ MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
+ if ( facet != 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." );
- }
+ artifact.setClassifier( facet.getClassifier() );
+ artifact.setType( facet.getType() );
}
- 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;
}
* 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 ) ) );
}
}
public void testBadPathMissingType()
{
+ // TODO: should we allow this instead?
assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
}
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" );
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 );
}
}
}
}
+ // 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();
+ }
}