List<String> artifacts = project.getRuntimeClasspathElements();
String classpathPrefix = config.getClasspathPrefix();
String layoutType = config.getClasspathLayoutType();
String layout = config.getCustomClasspathLayout();
Interpolator interpolator = new StringSearchInterpolator();
for ( String artifactFile : artifacts )
{
File f = new File( artifactFile );
if ( f.getAbsoluteFile().isFile() )
{
@SuppressWarnings( "unchecked" )
Artifact artifact = findArtifactWithFile( project.getArtifacts(), f );
if ( classpath.length() > 0 )
{
classpath.append( " " );
}
classpath.append( classpathPrefix );
// NOTE: If the artifact or layout type (from config) is null, give up and use the file name by itself.
if ( artifact == null || layoutType == null )
{
classpath.append( f.getName() );
}
else
{
List<ValueSource> valueSources = new ArrayList<ValueSource>();
valueSources.add(
new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES, artifact, true ) );
valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES, artifact == null
? null
: artifact.getArtifactHandler(), true ) );
Properties extraExpressions = new Properties();
if ( artifact != null )
{
// FIXME: This query method SHOULD NOT affect the internal
// state of the artifact version, but it does.
if ( !artifact.isSnapshot() )
{
extraExpressions.setProperty( "baseVersion", artifact.getVersion() );
}
extraExpressions.setProperty( "groupIdPath", artifact.getGroupId().replace( '.', '/' ) );
if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
{
extraExpressions.setProperty( "dashClassifier", "-" + artifact.getClassifier() );
extraExpressions.setProperty( "dashClassifier?", "-" + artifact.getClassifier() );
}
else
{
extraExpressions.setProperty( "dashClassifier", "" );
extraExpressions.setProperty( "dashClassifier?", "" );
}
}
valueSources.add(
new PrefixedPropertiesValueSource( ARTIFACT_EXPRESSION_PREFIXES, extraExpressions, true ) );
for ( ValueSource vs : valueSources )
{
interpolator.addValueSource( vs );
}
RecursionInterceptor recursionInterceptor =
new PrefixAwareRecursionInterceptor( ARTIFACT_EXPRESSION_PREFIXES );
try
{
if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_SIMPLE.equals( layoutType ) )
{
if ( config.isUseUniqueVersions() )
{
classpath.append( interpolator.interpolate( SIMPLE_LAYOUT, recursionInterceptor ) );
}
else
{
classpath.append(
interpolator.interpolate( SIMPLE_LAYOUT_NONUNIQUE, recursionInterceptor ) );
}
}
else if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY.equals( layoutType ) )
{
// we use layout /$groupId[0]/../${groupId[n]/$artifactId/$version/{fileName}
// here we must find the Artifact in the project Artifacts to generate the maven layout
if ( config.isUseUniqueVersions() )
{
classpath.append(
interpolator.interpolate( REPOSITORY_LAYOUT, recursionInterceptor ) );
}
else
{
classpath.append(
interpolator.interpolate( REPOSITORY_LAYOUT_NONUNIQUE, recursionInterceptor ) );
}
}
else if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM.equals( layoutType ) )
{
if ( layout == null )
{
throw new ManifestException( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM
+ " layout type was declared, but custom layout expression was not specified. Check your <archive><manifest><customLayout/> element." );
}
classpath.append( interpolator.interpolate( layout, recursionInterceptor ) );
}
else
{
throw new ManifestException( "Unknown classpath layout type: '" + layoutType
+ "'. Check your <archive><manifest><layoutType/> element." );
}
}
catch ( InterpolationException e )
{
ManifestException error = new ManifestException(
"Error interpolating artifact path for classpath entry: " + e.getMessage() );
error.initCause( e );
throw error;
}
finally
{
for ( ValueSource vs : valueSources )
{
interpolator.removeValuesSource( vs );
}
}
}
}
}