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
This commit is contained in:
John Dennis Casey 2005-08-22 19:00:51 +00:00
parent b88302c209
commit 146e022327
13 changed files with 318 additions and 69 deletions

View File

@ -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 );

View File

@ -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 ) );

View File

@ -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 );

View File

@ -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;

View File

@ -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();

View File

@ -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 );
}
}

View File

@ -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 <a href="mailto:brett@apache.org">Brett Porter</a>
@ -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();
}
}

View File

@ -47,6 +47,28 @@
</requirement>
</requirements>
</component>
<!--
|
| ArtifactTransformationManager
|
-->
<component>
<role>org.apache.maven.artifact.transform.ArtifactTransformationManager</role>
<implementation>org.apache.maven.artifact.transform.DefaultArtifactTransformationManager</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.transform.ArtifactTransformation</role>
<field-name>artifactTransformations</field-name>
</requirement>
<requirement>
<role>org.apache.maven.artifact.transform.ArtifactTransformation</role>
<role-hint>snapshot</role-hint>
<field-name>snapshotTransformation</field-name>
</requirement>
</requirements>
</component>
<!--
|
| Resolver
@ -60,8 +82,7 @@
<role>org.apache.maven.artifact.manager.WagonManager</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.transform.ArtifactTransformation</role>
<field-name>artifactTransformations</field-name>
<role>org.apache.maven.artifact.transform.ArtifactTransformationManager</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
@ -82,8 +103,7 @@
<implementation>org.apache.maven.artifact.installer.DefaultArtifactInstaller</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.transform.ArtifactTransformation</role>
<field-name>artifactTransformations</field-name>
<role>org.apache.maven.artifact.transform.ArtifactTransformationManager</role>
</requirement>
</requirements>
</component>
@ -101,8 +121,7 @@
<role>org.apache.maven.artifact.manager.WagonManager</role>
</requirement>
<requirement>
<role>org.apache.maven.artifact.transform.ArtifactTransformation</role>
<field-name>artifactTransformations</field-name>
<role>org.apache.maven.artifact.transform.ArtifactTransformationManager</role>
</requirement>
</requirements>
</component>

View File

@ -61,4 +61,5 @@ public interface ArtifactTransformation
*/
void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository )
throws ArtifactMetadataRetrievalException;
}

View File

@ -0,0 +1,62 @@
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.List;
/**
* Manages multiple ArtifactTransformation instances and applies them in succession.
*/
public interface ArtifactTransformationManager
{
String ROLE = ArtifactTransformationManager.class.getName();
/**
* Take in a artifact and return the transformed artifact for locating in the remote repository. If no
* transformation has occured the original artifact is returned.
*
* @param artifact Artifact to be transformed.
* @param remoteRepositories the repositories to check
* @param localRepository the local repository
*/
void transformForResolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository )
throws ArtifactMetadataRetrievalException;
/**
* Take in a artifact and return the transformed artifact for locating in the local repository. If no
* transformation has occured the original artifact is returned.
*
* @param artifact Artifact to be transformed.
* @param localRepository the local repository it will be stored in
*/
void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
throws ArtifactMetadataRetrievalException;
/**
* Take in a artifact and return the transformed artifact for distributing toa remote repository. If no
* transformation has occured the original artifact is returned.
*
* @param artifact Artifact to be transformed.
* @param remoteRepository the repository to deploy to
*/
void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository )
throws ArtifactMetadataRetrievalException;
/**
* Return the timestamp which will be used to deploy artifacts from this build.
*/
String getSnapshotDeploymentTimestamp();
/**
* Return the buildnumber which will be used to deploy artifacts from this build.
*/
int getSnapshotDeploymentBuildNumber( Artifact snapshotArtifact );
/**
* Return the artifact-version which will be used to deploy artifacts from this build.
*/
String getSnapshotDeploymentVersion( Artifact snapshotArtifact );
}

View File

@ -26,6 +26,7 @@ import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.transform.ArtifactTransformationManager;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.model.Build;
@ -120,6 +121,8 @@ public class DefaultMavenProjectBuilder
private ArtifactRepositoryFactory artifactRepositoryFactory;
private ArtifactTransformationManager transformationManager;
private final Map modelCache = new HashMap();
public static final String MAVEN_MODEL_VERSION = "4.0.0";
@ -576,6 +579,13 @@ public class DefaultMavenProjectBuilder
Artifact projectArtifact = artifactFactory.createBuildArtifact( project.getGroupId(), project.getArtifactId(),
project.getVersion(), project.getPackaging() );
project.setArtifact( projectArtifact );
if ( projectArtifact.isSnapshot() )
{
project.setSnapshotDeploymentVersion( transformationManager.getSnapshotDeploymentVersion( projectArtifact ) );
project.setSnapshotDeploymentBuildNumber( transformationManager.getSnapshotDeploymentBuildNumber( projectArtifact ) );
}
project.setPluginArtifactRepositories( ProjectUtils.buildArtifactRepositories( model.getPluginRepositories(),
artifactRepositoryFactory,

View File

@ -137,6 +137,10 @@ public class MavenProject
private Build buildOverlay;
private String snapshotDeploymentVersion;
private int snapshotDeploymentBuildNumber = -1;
public MavenProject( Model model )
{
this.model = model;
@ -182,6 +186,9 @@ public class MavenProject
{
this.originalModel = ModelUtils.cloneModel( project.originalModel );
}
this.snapshotDeploymentVersion = project.snapshotDeploymentVersion;
this.snapshotDeploymentBuildNumber = project.snapshotDeploymentBuildNumber;
// TODO: need to clone this too?
this.artifact = project.artifact;
@ -1341,5 +1348,32 @@ public class MavenProject
public void attachArtifact( String type, String classifier, File file )
{
}
public void setSnapshotDeploymentVersion( String deploymentVersion )
{
this.snapshotDeploymentVersion = deploymentVersion;
}
public String getSnapshotDeploymentVersion()
{
if ( snapshotDeploymentVersion == null )
{
return getVersion();
}
else
{
return snapshotDeploymentVersion;
}
}
public void setSnapshotDeploymentBuildNumber( int deploymentBuildNumber )
{
this.snapshotDeploymentBuildNumber = deploymentBuildNumber;
}
public int getSnapshotDeploymentBuildNumber()
{
return snapshotDeploymentBuildNumber;
}
}

View File

@ -50,6 +50,9 @@
<role>org.apache.maven.project.MavenProjectBuilder</role>
<implementation>org.apache.maven.project.DefaultMavenProjectBuilder</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.transform.ArtifactTransformationManager</role>
</requirement>
<requirement>
<role>org.apache.maven.profiles.MavenProfilesBuilder</role>
</requirement>