Index: src/main/java/org/apache/maven/archiver/MavenArchiver.java =================================================================== --- src/main/java/org/apache/maven/archiver/MavenArchiver.java (revision 1232770) +++ src/main/java/org/apache/maven/archiver/MavenArchiver.java (working copy) @@ -19,8 +19,19 @@ * under the License. */ +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.jar.JarArchiver; @@ -36,42 +47,34 @@ import org.codehaus.plexus.interpolation.ValueSource; import org.codehaus.plexus.util.StringUtils; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; - /** * @author Emmanuel Venisse * @version $Revision$ $Date$ */ public class MavenArchiver { - - public static final String SIMPLE_LAYOUT = "${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}"; - public static final String REPOSITORY_LAYOUT = "${artifact.groupIdPath}/${artifact.artifactId}/" + - "${artifact.baseVersion}/${artifact.artifactId}-" + - "${artifact.version}${dashClassifier?}.${artifact.extension}"; - - public static final String SIMPLE_LAYOUT_NONUNIQUE = "${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + public static final String SIMPLE_LAYOUT = + "${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}"; - public static final String REPOSITORY_LAYOUT_NONUNIQUE = "${artifact.groupIdPath}/${artifact.artifactId}/" + - "${artifact.baseVersion}/${artifact.artifactId}-" + - "${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; - + public static final String REPOSITORY_LAYOUT = "${artifact.groupIdPath}/${artifact.artifactId}/" + + "${artifact.baseVersion}/${artifact.artifactId}-" + + "${artifact.version}${dashClassifier?}.${artifact.extension}"; + + public static final String SIMPLE_LAYOUT_NONUNIQUE = + "${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + + public static final String REPOSITORY_LAYOUT_NONUNIQUE = "${artifact.groupIdPath}/${artifact.artifactId}/" + + "${artifact.baseVersion}/${artifact.artifactId}-" + + "${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + private static final List ARTIFACT_EXPRESSION_PREFIXES; - + static { List artifactExpressionPrefixes = new ArrayList(); artifactExpressionPrefixes.add( "artifact." ); - + ARTIFACT_EXPRESSION_PREFIXES = artifactExpressionPrefixes; } @@ -81,15 +84,27 @@ /** * Return a pre-configured manifest - * + * * @todo Add user attributes list and user groups list + * @deprecated */ public Manifest getManifest( MavenProject project, MavenArchiveConfiguration config ) throws ManifestException, DependencyResolutionRequiredException { + return getManifest( null, project, config ); + } + + /** + * Return a pre-configured manifest + * + * @todo Add user attributes list and user groups list + */ + public Manifest getManifest( MavenSession session, MavenProject project, MavenArchiveConfiguration config ) + throws ManifestException, DependencyResolutionRequiredException + { boolean hasManifestEntries = !config.isManifestEntriesEmpty(); Map entries = hasManifestEntries ? config.getManifestEntries() : Collections.EMPTY_MAP; - Manifest manifest = getManifest( project, config.getManifest(), entries ); + Manifest manifest = getManifest( session, project, config.getManifest(), entries ); // any custom manifest entries in the archive configuration manifest? if ( hasManifestEntries ) @@ -103,7 +118,7 @@ if ( key.equals( "Class-Path" ) && attr != null ) { // Merge the user-supplied Class-Path value with the programmatically - // generated Class-Path. Note that the user-supplied value goes first + // generated Class-Path. Note that the user-supplied value goes first // so that resources there will override any in the standard Class-Path. attr.setValue( value + " " + attr.getValue() ); } @@ -146,13 +161,25 @@ /** * Return a pre-configured manifest - * + * * @todo Add user attributes list and user groups list + * @deprecated */ public Manifest getManifest( MavenProject project, ManifestConfiguration config ) throws ManifestException, DependencyResolutionRequiredException { - return getManifest( project, config, Collections.EMPTY_MAP ); + return getManifest( null, project, config, Collections.EMPTY_MAP ); + } + + /** + * Return a pre-configured manifest + * + * @todo Add user attributes list and user groups list + */ + public Manifest getManifest( MavenSession session, MavenProject project, ManifestConfiguration config ) + throws ManifestException, DependencyResolutionRequiredException + { + return getManifest( session, project, config, Collections.EMPTY_MAP ); } private void addManifestAttribute( Manifest manifest, Map map, String key, String value ) @@ -160,7 +187,7 @@ { if ( map.containsKey( key ) ) { - return; // The map value will be added later + return; // The map value will be added later } addManifestAttribute( manifest, key, value ); } @@ -175,33 +202,43 @@ } else { - // if the value is empty we have create an entry with an empty string + // if the value is empty we have create an entry with an empty string // to prevent null print in the manifest file Manifest.Attribute attr = new Manifest.Attribute( key, "" ); manifest.addConfiguredAttribute( attr ); } } + /** + * @deprecated + */ protected Manifest getManifest( MavenProject project, ManifestConfiguration config, Map entries ) throws ManifestException, DependencyResolutionRequiredException { + return getManifest( null, project, config, entries ); + } + + protected Manifest getManifest( MavenSession session, MavenProject project, ManifestConfiguration config, + Map entries ) + throws ManifestException, DependencyResolutionRequiredException + { // TODO: Should we replace "map" with a copy? Note, that we modify it! // Added basic entries Manifest m = new Manifest(); - addManifestAttribute( m, entries, "Created-By", "Apache Maven" ); + addCreatedByEntry( session, m, entries ); addCustomEntries( m, entries, config ); if ( config.isAddClasspath() ) { StringBuffer classpath = new StringBuffer(); - + List artifacts = project.getRuntimeClasspathElements(); String classpathPrefix = config.getClasspathPrefix(); String layoutType = config.getClasspathLayoutType(); String layout = config.getCustomClasspathLayout(); - + Interpolator interpolator = new StringSearchInterpolator(); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) @@ -210,14 +247,15 @@ if ( f.getAbsoluteFile().isFile() ) { 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. + + // 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() ); @@ -226,8 +264,12 @@ { List valueSources = new ArrayList(); valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES, artifact, true ) ); - valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES, artifact == null ? null : artifact.getArtifactHandler(), true ) ); - + valueSources.add( new PrefixedObjectValueSource( + ARTIFACT_EXPRESSION_PREFIXES, + artifact == null ? null + : artifact.getArtifactHandler(), + true ) ); + Properties extraExpressions = new Properties(); if ( artifact != null ) { @@ -237,7 +279,7 @@ { extraExpressions.setProperty( "baseVersion", artifact.getVersion() ); } - + extraExpressions.setProperty( "groupIdPath", artifact.getGroupId().replace( '.', '/' ) ); if ( StringUtils.isNotEmpty( artifact.getClassifier() ) ) { @@ -250,16 +292,18 @@ extraExpressions.setProperty( "dashClassifier?", "" ); } } - valueSources.add( new PrefixedPropertiesValueSource( ARTIFACT_EXPRESSION_PREFIXES, extraExpressions, true ) ); - + valueSources.add( new PrefixedPropertiesValueSource( ARTIFACT_EXPRESSION_PREFIXES, + extraExpressions, true ) ); + for ( Iterator it = valueSources.iterator(); it.hasNext(); ) { ValueSource vs = (ValueSource) it.next(); interpolator.addValueSource( vs ); } - - RecursionInterceptor recursionInterceptor = new PrefixAwareRecursionInterceptor( ARTIFACT_EXPRESSION_PREFIXES ); - + + RecursionInterceptor recursionInterceptor = + new PrefixAwareRecursionInterceptor( ARTIFACT_EXPRESSION_PREFIXES ); + try { if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_SIMPLE.equals( layoutType ) ) @@ -270,7 +314,8 @@ } else { - classpath.append( interpolator.interpolate( SIMPLE_LAYOUT_NONUNIQUE, recursionInterceptor ) ); + classpath.append( interpolator.interpolate( SIMPLE_LAYOUT_NONUNIQUE, + recursionInterceptor ) ); } } else if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY.equals( layoutType ) ) @@ -283,7 +328,8 @@ } else { - classpath.append( interpolator.interpolate( REPOSITORY_LAYOUT_NONUNIQUE, recursionInterceptor ) ); + classpath.append( interpolator.interpolate( REPOSITORY_LAYOUT_NONUNIQUE, + recursionInterceptor ) ); } } else if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM.equals( layoutType ) ) @@ -294,7 +340,7 @@ ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM + " layout type was declared, but custom layout expression was not specified. Check your element." ); } - + classpath.append( interpolator.interpolate( layout, recursionInterceptor ) ); } else @@ -308,7 +354,7 @@ ManifestException error = new ManifestException( "Error interpolating artifact path for classpath entry: " + e.getMessage() ); - + error.initCause( e ); throw error; } @@ -415,23 +461,34 @@ return m; } + private void addCreatedByEntry( MavenSession session, Manifest m, Map entries ) + throws ManifestException + { + System.out.println("Session: " + session); + String createdBy = "Apache Maven"; + if ( session != null ) + { // can be null due to API backwards compatibility + String mavenVersion = session.getExecutionProperties().getProperty( "maven.version" ); + if ( mavenVersion != null ) + { + createdBy += " " + mavenVersion; + } + } + System.out.println("Created by: " + createdBy); + addManifestAttribute( m, entries, "Created-By", createdBy ); + } + private void addCustomEntries( Manifest m, Map entries, ManifestConfiguration config ) throws ManifestException { addManifestAttribute( m, entries, "Built-By", System.getProperty( "user.name" ) ); addManifestAttribute( m, entries, "Build-Jdk", System.getProperty( "java.version" ) ); -/* TODO: rethink this, it wasn't working - Artifact projectArtifact = project.getArtifact(); - - if ( projectArtifact.isSnapshot() ) - { - Manifest.Attribute buildNumberAttr = new Manifest.Attribute( "Build-Number", "" + - project.getSnapshotDeploymentBuildNumber() ); - m.addConfiguredAttribute( buildNumberAttr ); - } - -*/ + /* + * TODO: rethink this, it wasn't working Artifact projectArtifact = project.getArtifact(); if ( + * projectArtifact.isSnapshot() ) { Manifest.Attribute buildNumberAttr = new Manifest.Attribute( "Build-Number", + * "" + project.getSnapshotDeploymentBuildNumber() ); m.addConfiguredAttribute( buildNumberAttr ); } + */ if ( config.getPackageName() != null ) { addManifestAttribute( m, entries, "Package", config.getPackageName() ); @@ -453,9 +510,19 @@ archiveFile = outputFile; } + /** + * @deprecated + */ public void createArchive( MavenProject project, MavenArchiveConfiguration archiveConfiguration ) throws ArchiverException, ManifestException, IOException, DependencyResolutionRequiredException { + createArchive( null, project, archiveConfiguration ); + } + + public void createArchive( MavenSession session, MavenProject project, + MavenArchiveConfiguration archiveConfiguration ) + throws ArchiverException, ManifestException, IOException, DependencyResolutionRequiredException + { // we have to clone the project instance so we can write out the pom with the deployment version, // without impacting the main project instance... // TODO use clone() in Maven 2.0.9+ @@ -510,7 +577,7 @@ archiver.setManifest( manifestFile ); } - Manifest manifest = getManifest( workingProject, archiveConfiguration ); + Manifest manifest = getManifest( session, workingProject, archiveConfiguration ); // Configure the jar archiver.addConfiguredManifest( manifest ); @@ -533,17 +600,17 @@ } archiver.setForced( forced ); - if ( !archiveConfiguration.isForced() && archiver.isSupportingForced() ) + if ( !archiveConfiguration.isForced() && archiver.isSupportingForced() ) { // TODO Should issue a warning here, but how do we get a logger? - // TODO getLog().warn( "Forced build is disabled, but disabling the forced mode isn't supported by the archiver." ); + // TODO getLog().warn( + // "Forced build is disabled, but disabling the forced mode isn't supported by the archiver." ); } // create archive archiver.createArchive(); } - - + private Artifact findArtifactWithFile( Set artifacts, File file ) { for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); ) Index: src/test/java/org/apache/maven/archiver/MavenArchiverTest.java =================================================================== --- src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (revision 1232770) +++ src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (working copy) @@ -368,7 +368,13 @@ } } - public void testManifestEntries() + public void testDeprecatedManifestCreationAPI() + throws Exception + { + //TODO + } + + public void testManifestEntries_deprecatedAPI() throws Exception { JarFile jar = null; @@ -596,8 +602,7 @@ Manifest manifest = archiver.getManifest( project, config ); String[] classPathEntries = - StringUtils.split( - new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), + StringUtils.split( new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), " " ); assertEquals( "org/apache/dummy/dummy1/1.0/dummy1-1.0.jar", classPathEntries[0] ); assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] ); @@ -651,8 +656,7 @@ Manifest manifest = archiver.getManifest( project, config ); String[] classPathEntries = - StringUtils.split( - new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), + StringUtils.split( new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), " " ); assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] ); assertEquals( "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", classPathEntries[1] ); @@ -700,16 +704,14 @@ config.getManifest().setMainClass( "org.apache.maven.Foo" ); config.getManifest().setAddClasspath( true ); config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM ); - config.getManifest().setCustomClasspathLayout( - "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.version}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" ); + config.getManifest().setCustomClasspathLayout( "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.version}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" ); archiver.createArchive( project, config ); assertTrue( jarFile.exists() ); jar = new JarFile( jarFile ); Manifest manifest = archiver.getManifest( project, config ); String[] classPathEntries = - StringUtils.split( - new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), + StringUtils.split( new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), " " ); assertEquals( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", classPathEntries[0] ); assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] ); @@ -757,25 +759,25 @@ config.getManifest().setMainClass( "org.apache.maven.Foo" ); config.getManifest().setAddClasspath( true ); config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM ); - config.getManifest().setCustomClasspathLayout( - "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" ); + config.getManifest().setCustomClasspathLayout( "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}" ); archiver.createArchive( project, config ); assertTrue( jarFile.exists() ); jar = new JarFile( jarFile ); Manifest manifest = archiver.getManifest( project, config ); String[] classPathEntries = - StringUtils.split( - new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), + StringUtils.split( new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), " " ); - assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] ); + assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", + classPathEntries[0] ); assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] ); assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] ); String classPath = jar.getManifest().getMainAttributes().getValue( Attributes.Name.CLASS_PATH ); assertNotNull( classPath ); classPathEntries = StringUtils.split( classPath, " " ); - assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", classPathEntries[0] ); + assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", + classPathEntries[0] ); assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] ); assertEquals( "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar", classPathEntries[2] ); @@ -814,16 +816,14 @@ config.getManifest().setMainClass( "org.apache.maven.Foo" ); config.getManifest().setAddClasspath( true ); config.getManifest().setClasspathLayoutType( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM ); - config.getManifest().setCustomClasspathLayout( - "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}" ); + config.getManifest().setCustomClasspathLayout( "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/TEST-${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}" ); archiver.createArchive( project, config ); assertTrue( jarFile.exists() ); jar = new JarFile( jarFile ); Manifest manifest = archiver.getManifest( project, config ); String[] classPathEntries = - StringUtils.split( - new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), + StringUtils.split( new String( manifest.getMainSection().getAttributeValue( "Class-Path" ).getBytes() ), " " ); assertEquals( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar", classPathEntries[0] ); assertEquals( "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", classPathEntries[1] ); Index: pom.xml =================================================================== --- pom.xml (revision 1232770) +++ pom.xml (working copy) @@ -75,6 +75,11 @@ + org.apache.maven + maven-core + ${mavenVersion} + + org.codehaus.plexus plexus-archiver 2.0.2