private static final String INVALID_ARTIFACT_PATH = "Invalid path to Artifact: ";
protected static ArtifactReference toArtifactReference( String path )
throws LayoutException
{
ArtifactReference artifact = new ArtifactReference();
String normalizedPath = StringUtils.replace( path, "\\", "/" );
String pathParts[] = StringUtils.split( normalizedPath, '/' );
/* Always 3 parts. (Never more or less)
*
* path = "commons-lang/jars/commons-lang-2.1.jar"
* path[0] = "commons-lang"; // The Group ID
* path[1] = "jars"; // The Directory Type
* path[2] = "commons-lang-2.1.jar"; // The Filename.
*/
if ( pathParts.length != 3 )
{
// Illegal Path Parts Length.
throw new LayoutException( INVALID_ARTIFACT_PATH
+ "legacy paths should only have 3 parts [groupId]/[type]s/[artifactId]-[version].[type], found "
+ pathParts.length + " instead." );
}
// The Group ID.
artifact.setGroupId( pathParts[0] );
// The Expected Type.
String expectedType = pathParts[1];
// Sanity Check: expectedType should end in "s".
if ( !expectedType.endsWith( "s" ) )
{
throw new LayoutException( INVALID_ARTIFACT_PATH
+ "legacy paths should have an expected type ending in [s] in the second part of the path." );
}
// The Filename.
String filename = pathParts[2];
FilenameParser parser = new FilenameParser( filename );
artifact.setArtifactId( parser.nextNonVersion() );
// Sanity Check: does it have an artifact id?
if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
{
// Special Case: The filename might start with a version id (like "test-arch-1.0.jar").
int idx = filename.indexOf( '-' );
if ( idx > 0 )
{
parser.reset();
// Take the first section regardless of content.
String artifactId = parser.next();
// Is there anything more that is considered not a version id?
String moreArtifactId = parser.nextNonVersion();
if ( StringUtils.isNotBlank( moreArtifactId ) )
{
artifact.setArtifactId( artifactId + "-" + moreArtifactId );
}
else
{
artifact.setArtifactId( artifactId );
}
}
// Sanity Check: still no artifact id?
if ( StringUtils.isEmpty( artifact.getArtifactId() ) )
{
throw new LayoutException( INVALID_ARTIFACT_PATH + "no artifact id present." );
}
}
artifact.setVersion( parser.remaining() );
// Sanity Check: does it have a version?
if ( StringUtils.isEmpty( artifact.getVersion() ) )
{
// Special Case: use last section of artifactId as version.
String artifactId = artifact.getArtifactId();
int idx = artifactId.lastIndexOf( '-' );
if ( idx > 0 )
{
artifact.setVersion( artifactId.substring( idx + 1 ) );
artifact.setArtifactId( artifactId.substring( 0, idx ) );
}
else
{
throw new LayoutException( INVALID_ARTIFACT_PATH + "no version found." );
}
}
// Set Type
artifact.setType( ArtifactExtensionMapping.guessTypeFromFilename( filename ) );
// Sanity Check: does it have an extension?
if ( StringUtils.isEmpty( artifact.getType() ) )
{
throw new LayoutException( INVALID_ARTIFACT_PATH + "no extension found." );
}
// Special Case with Maven Plugins
if ( StringUtils.equals( "jar", artifact.getType() ) && StringUtils.equals( "plugins", expectedType ) )
{
artifact.setType( ArtifactExtensionMapping.MAVEN_PLUGIN );
}
else
{
// Sanity Check: does extension match pathType on path?
String trimPathType = expectedType.substring( 0, expectedType.length() - 1 );
String expectedExtension = ArtifactExtensionMapping.getExtension( trimPathType );
String actualExtension = parser.getExtension();
if ( !expectedExtension.equals( actualExtension ) )
{
throw new LayoutException( INVALID_ARTIFACT_PATH + "mismatch on extension [" + actualExtension
+ "] and layout specified type [" + expectedType + "] (which maps to extension: ["
+ expectedExtension + "]) on path [" + path + "]" );
}
}
String classifier = ArtifactClassifierMapping.getClassifier( artifact.getType() );
if ( classifier != null )
{
String version = artifact.getVersion();
if ( ! version.endsWith( "-" + classifier ) )
{
throw new LayoutException( INVALID_ARTIFACT_PATH + expectedType + " artifacts must use the classifier " + classifier );
}
version = version.substring( 0, version.length() - classifier.length() - 1 );
artifact.setVersion( version );
artifact.setClassifier( classifier );
}
return artifact;
}