From 146e022327e7344948cc95751560212a350cf0fa Mon Sep 17 00:00:00 2001 From: John Dennis Casey Date: Mon, 22 Aug 2005 19:00:51 +0000 Subject: [PATCH] Resolving: MNG-251 o Added transformation manager o snapshot timestamp/buildnumber is now managed from the transformation rather than the metadata o maven-archiver now clones the MavenProject and resolves snapshot versions for introduction into manifest and exported pom. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@239219 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/archiver/MavenArchiver.java | 37 +++++++--- .../deployer/DefaultArtifactDeployer.java | 14 ++-- .../installer/DefaultArtifactInstaller.java | 12 +--- .../metadata/SnapshotArtifactMetadata.java | 30 +++------ .../resolver/DefaultArtifactResolver.java | 23 +++---- .../DefaultArtifactTransformationManager.java | 63 +++++++++++++++++ .../transform/SnapshotTransformation.java | 67 ++++++++++++++++++- .../resources/META-INF/plexus/components.xml | 31 +++++++-- .../transform/ArtifactTransformation.java | 1 + .../ArtifactTransformationManager.java | 62 +++++++++++++++++ .../project/DefaultMavenProjectBuilder.java | 10 +++ .../apache/maven/project/MavenProject.java | 34 ++++++++++ .../resources/META-INF/plexus/components.xml | 3 + 13 files changed, 318 insertions(+), 69 deletions(-) create mode 100644 maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/DefaultArtifactTransformationManager.java create mode 100644 maven-artifact/src/main/java/org/apache/maven/artifact/transform/ArtifactTransformationManager.java diff --git a/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java b/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java index c4d69de22c..770ac8bf7a 100644 --- a/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java +++ b/maven-archiver/src/main/java/org/apache/maven/archiver/MavenArchiver.java @@ -42,7 +42,7 @@ import java.util.Set; public class MavenArchiver { private JarArchiver archiver = new JarArchiver(); - + private File archiveFile; /** @@ -59,6 +59,14 @@ public class MavenArchiver m.addConfiguredAttribute( buildAttr ); Manifest.Attribute createdAttr = new Manifest.Attribute( "Created-By", "Apache Maven" ); m.addConfiguredAttribute( createdAttr ); + + Artifact projectArtifact = project.getArtifact(); + + if ( projectArtifact.isSnapshot() ) + { + Manifest.Attribute buildNumberAttr = new Manifest.Attribute( "Build-Number", "" + project.getSnapshotDeploymentBuildNumber() ); + m.addConfiguredAttribute( buildNumberAttr ); + } if ( config.getPackageName() != null ) { @@ -214,12 +222,21 @@ public class MavenArchiver // top-level POM elements so that applications that wish to access // POM information without the use of maven tools can do so. // ---------------------------------------------------------------------- + + // we have to clone the project instance so we can write out the pom with the deployment version, + // without impacting the main project instance... + MavenProject workingProject = new MavenProject( project ); + + if ( workingProject.getArtifact().isSnapshot() ) + { + workingProject.setVersion( workingProject.getSnapshotDeploymentVersion() ); + } + + String groupId = workingProject.getGroupId(); - String groupId = project.getGroupId(); + String artifactId = workingProject.getArtifactId(); - String artifactId = project.getArtifactId(); - - File exportReadyPom = writeExportReadyPom( project ); + File exportReadyPom = writeExportReadyPom( workingProject ); archiver.addFile( exportReadyPom, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml" ); @@ -229,13 +246,13 @@ public class MavenArchiver Properties p = new Properties(); - p.setProperty( "groupId", project.getGroupId() ); + p.setProperty( "groupId", workingProject.getGroupId() ); - p.setProperty( "artifactId", project.getArtifactId() ); + p.setProperty( "artifactId", workingProject.getArtifactId() ); - p.setProperty( "version", project.getVersion() ); + p.setProperty( "version", workingProject.getVersion() ); - File pomPropertiesFile = new File( project.getFile().getParentFile(), "pom.properties" ); + File pomPropertiesFile = new File( workingProject.getFile().getParentFile(), "pom.properties" ); OutputStream os = new FileOutputStream( pomPropertiesFile ); @@ -256,7 +273,7 @@ public class MavenArchiver archiver.setManifest( manifestFile ); } - Manifest manifest = getManifest( project, archiveConfiguration.getManifest() ); + Manifest manifest = getManifest( workingProject, archiveConfiguration.getManifest() ); // Configure the jar archiver.addConfiguredManifest( manifest ); diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java index 126bc1c887..588de9b3a6 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/deployer/DefaultArtifactDeployer.java @@ -21,7 +21,7 @@ import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.transform.ArtifactTransformation; +import org.apache.maven.artifact.transform.ArtifactTransformationManager; import org.apache.maven.wagon.TransferFailedException; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.FileUtils; @@ -29,15 +29,14 @@ import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.io.IOException; import java.util.Iterator; -import java.util.List; public class DefaultArtifactDeployer extends AbstractLogEnabled implements ArtifactDeployer { private WagonManager wagonManager; - - private List artifactTransformations; + + private ArtifactTransformationManager transformationManager; /** @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly. */ public void deploy( String basedir, String finalName, Artifact artifact, ArtifactRepository deploymentRepository, @@ -55,12 +54,7 @@ public class DefaultArtifactDeployer { try { - // TODO: better to have a transform manager, or reuse the handler manager again so we don't have these requirements duplicated all over? - for ( Iterator i = artifactTransformations.iterator(); i.hasNext(); ) - { - ArtifactTransformation transform = (ArtifactTransformation) i.next(); - transform.transformForDeployment( artifact, deploymentRepository ); - } + transformationManager.transformForDeployment( artifact, localRepository ); // Copy the original file to the new one if it was transformed File artifactFile = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) ); diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/installer/DefaultArtifactInstaller.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/installer/DefaultArtifactInstaller.java index 3f721cbb4b..1f49061043 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/installer/DefaultArtifactInstaller.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/installer/DefaultArtifactInstaller.java @@ -20,20 +20,19 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.transform.ArtifactTransformation; +import org.apache.maven.artifact.transform.ArtifactTransformationManager; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.FileUtils; import java.io.File; import java.io.IOException; import java.util.Iterator; -import java.util.List; public class DefaultArtifactInstaller extends AbstractLogEnabled implements ArtifactInstaller { - private List artifactTransformations; + private ArtifactTransformationManager transformationManager; /** @deprecated we want to use the artifact method only, and ensure artifact.file is set correctly. */ public void install( String basedir, String finalName, Artifact artifact, ArtifactRepository localRepository ) @@ -50,12 +49,7 @@ public class DefaultArtifactInstaller { try { - // TODO: better to have a transform manager, or reuse the handler manager again so we don't have these requirements duplicated all over? - for ( Iterator i = artifactTransformations.iterator(); i.hasNext(); ) - { - ArtifactTransformation transform = (ArtifactTransformation) i.next(); - transform.transformForInstall( artifact, localRepository ); - } + transformationManager.transformForInstall( artifact, localRepository ); String localPath = localRepository.pathOf( artifact ); diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/metadata/SnapshotArtifactMetadata.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/metadata/SnapshotArtifactMetadata.java index 7189e19a19..02552356ab 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/metadata/SnapshotArtifactMetadata.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/metadata/SnapshotArtifactMetadata.java @@ -43,9 +43,6 @@ public class SnapshotArtifactMetadata private static final String UTC_TIMESTAMP_PATTERN = "yyyyMMdd.HHmmss"; - // TODO: very quick and nasty hack to get the same timestamp across a build - not embedder friendly - private static String sessionTimestamp = null; - public SnapshotArtifactMetadata( Artifact artifact ) { super( artifact, artifact.getArtifactId() + "-" + artifact.getBaseVersion() + "." + SNAPSHOT_VERSION_FILE ); @@ -83,11 +80,22 @@ public class SnapshotArtifactMetadata buildNumber = 0; } } + + public void setVersion( String timestamp, int buildNumber ) + { + this.timestamp = timestamp; + this.buildNumber = buildNumber; + } public String getTimestamp() { return timestamp; } + + public int getBuildNumber() + { + return buildNumber; + } public static DateFormat getUtcDateFormatter() { @@ -96,22 +104,6 @@ public class SnapshotArtifactMetadata return utcDateFormatter; } - public void update() - { - this.buildNumber++; - timestamp = getSessionTimestamp(); - } - - private static String getSessionTimestamp() - { - if ( sessionTimestamp == null ) - { - sessionTimestamp = getUtcDateFormatter().format( new Date() ); - } - return sessionTimestamp; - } - - public int compareTo( Object o ) { SnapshotArtifactMetadata metadata = (SnapshotArtifactMetadata) o; diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index f0f0ff5bd8..37e370558d 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -24,7 +24,7 @@ import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.resolver.filter.ArtifactFilter; -import org.apache.maven.artifact.transform.ArtifactTransformation; +import org.apache.maven.artifact.transform.ArtifactTransformationManager; import org.apache.maven.wagon.ResourceDoesNotExistException; import org.apache.maven.wagon.TransferFailedException; import org.codehaus.plexus.logging.AbstractLogEnabled; @@ -46,8 +46,8 @@ public class DefaultArtifactResolver // ---------------------------------------------------------------------- private WagonManager wagonManager; - - private List artifactTransformations; + + private ArtifactTransformationManager transformationManager; protected ArtifactFactory artifactFactory; @@ -86,18 +86,13 @@ public class DefaultArtifactResolver artifact.setFile( new File( localRepository.getBasedir(), localPath ) ); - // TODO: better to have a transform manager, or reuse the handler manager again so we don't have these requirements duplicated all over? - for ( Iterator i = artifactTransformations.iterator(); i.hasNext(); ) + try { - ArtifactTransformation transform = (ArtifactTransformation) i.next(); - try - { - transform.transformForResolve( artifact, remoteRepositories, localRepository ); - } - catch ( ArtifactMetadataRetrievalException e ) - { - throw new ArtifactResolutionException( e.getMessage(), artifact, remoteRepositories, e ); - } + transformationManager.transformForResolve( artifact, remoteRepositories, localRepository ); + } + catch ( ArtifactMetadataRetrievalException e ) + { + throw new ArtifactResolutionException( e.getMessage(), artifact, remoteRepositories, e ); } File destination = artifact.getFile(); diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/DefaultArtifactTransformationManager.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/DefaultArtifactTransformationManager.java new file mode 100644 index 0000000000..46f54242f5 --- /dev/null +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/DefaultArtifactTransformationManager.java @@ -0,0 +1,63 @@ +package org.apache.maven.artifact.transform; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; +import org.apache.maven.artifact.repository.ArtifactRepository; + +import java.util.Iterator; +import java.util.List; + +public class DefaultArtifactTransformationManager + implements ArtifactTransformationManager +{ + + private List artifactTransformations; + + private SnapshotTransformation snapshotTransformation; + + public void transformForResolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) + throws ArtifactMetadataRetrievalException + { + for ( Iterator i = artifactTransformations.iterator(); i.hasNext(); ) + { + ArtifactTransformation transform = (ArtifactTransformation) i.next(); + transform.transformForResolve( artifact, remoteRepositories, localRepository ); + } + } + + public void transformForInstall( Artifact artifact, ArtifactRepository localRepository ) + throws ArtifactMetadataRetrievalException + { + for ( Iterator i = artifactTransformations.iterator(); i.hasNext(); ) + { + ArtifactTransformation transform = (ArtifactTransformation) i.next(); + transform.transformForInstall( artifact, localRepository ); + } + } + + public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository ) + throws ArtifactMetadataRetrievalException + { + for ( Iterator i = artifactTransformations.iterator(); i.hasNext(); ) + { + ArtifactTransformation transform = (ArtifactTransformation) i.next(); + transform.transformForDeployment( artifact, remoteRepository ); + } + } + + public String getSnapshotDeploymentTimestamp() + { + return snapshotTransformation.getDeploymentTimestamp(); + } + + public int getSnapshotDeploymentBuildNumber( Artifact artifact ) + { + return snapshotTransformation.getDeploymentBuildNumber( artifact); + } + + public String getSnapshotDeploymentVersion( Artifact snapshotArtifact ) + { + return snapshotTransformation.getDeploymentVersion( snapshotArtifact ); + } + +} diff --git a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java index f678203d4a..56bb2aea9d 100644 --- a/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java +++ b/maven-artifact-manager/src/main/java/org/apache/maven/artifact/transform/SnapshotTransformation.java @@ -17,6 +17,7 @@ package org.apache.maven.artifact.transform; */ import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.metadata.AbstractVersionArtifactMetadata; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.SnapshotArtifactMetadata; @@ -24,7 +25,10 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; import org.apache.maven.wagon.ResourceDoesNotExistException; +import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author Brett Porter @@ -35,6 +39,12 @@ import java.util.List; public class SnapshotTransformation extends AbstractVersionTransformation { + private String deploymentTimestamp; + + private int deploymentBuildNumber = 1; + + private Map buildNumbers = new HashMap(); + public void transformForResolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ArtifactMetadataRetrievalException { @@ -66,6 +76,8 @@ public class SnapshotTransformation { metadata = (SnapshotArtifactMetadata) retrieveFromRemoteRepository( artifact, remoteRepository, null, ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE ); + + updateDeploymentBuildNumber( artifact, metadata.getTimestamp(), metadata.getBuildNumber() ); } catch ( ResourceDoesNotExistException e ) { @@ -77,7 +89,7 @@ public class SnapshotTransformation metadata = (SnapshotArtifactMetadata) createMetadata( artifact ); } - metadata.update(); + metadata.setVersion( getDeploymentTimestamp(), deploymentBuildNumber ); artifact.setResolvedVersion( metadata.constructVersion() ); @@ -85,9 +97,62 @@ public class SnapshotTransformation } } + private void updateDeploymentBuildNumber( Artifact artifact, String timestamp, int buildNumberFromMetadata ) + { + // we only have to handle bumping the build number if we're on the same timestamp, somehow...miraculously + if ( deploymentTimestamp.equals( timestamp ) ) + { + String artifactKey = ArtifactUtils.versionlessKey( artifact ); + + Integer buildNum = (Integer) buildNumbers.get( artifactKey ); + + if ( buildNum == null || buildNum.intValue() <= buildNumberFromMetadata ) + { + buildNum = new Integer( buildNumberFromMetadata + 1 ); + + buildNumbers.put( artifactKey, buildNum ); + } + } + } + + public String getDeploymentTimestamp() + { + if ( deploymentTimestamp == null ) + { + deploymentTimestamp = SnapshotArtifactMetadata.getUtcDateFormatter().format( new Date() ); + } + return deploymentTimestamp; + } + + public int getDeploymentBuildNumber( Artifact artifact ) + { + String artifactKey = ArtifactUtils.versionlessKey( artifact ); + + Integer buildNum = (Integer) buildNumbers.get( artifactKey ); + + if ( buildNum == null ) + { + buildNum = new Integer( 1 ); + buildNumbers.put( artifactKey, buildNum ); + } + + return buildNum.intValue(); + } + protected AbstractVersionArtifactMetadata createMetadata( Artifact artifact ) { return new SnapshotArtifactMetadata( artifact ); } + public String getDeploymentVersion( Artifact artifact ) + { + int buildnum = getDeploymentBuildNumber( artifact ); + + SnapshotArtifactMetadata metadata = (SnapshotArtifactMetadata) createMetadata( artifact ); + + metadata.setVersion( getDeploymentTimestamp(), buildnum ); + + return metadata.constructVersion(); + } + } \ No newline at end of file diff --git a/maven-artifact-manager/src/main/resources/META-INF/plexus/components.xml b/maven-artifact-manager/src/main/resources/META-INF/plexus/components.xml index a00680c3a8..ce8fde2735 100644 --- a/maven-artifact-manager/src/main/resources/META-INF/plexus/components.xml +++ b/maven-artifact-manager/src/main/resources/META-INF/plexus/components.xml @@ -47,6 +47,28 @@ + + + + org.apache.maven.artifact.transform.ArtifactTransformationManager + org.apache.maven.artifact.transform.DefaultArtifactTransformationManager + + + org.apache.maven.artifact.transform.ArtifactTransformation + artifactTransformations + + + org.apache.maven.artifact.transform.ArtifactTransformation + snapshot + snapshotTransformation + + + +