Start of release plugin

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@170406 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Venisse 2005-05-16 16:27:51 +00:00
parent d5e88e8c9e
commit 8b6052bbc6
7 changed files with 798 additions and 0 deletions

View File

@ -67,6 +67,11 @@ public class DefaultArtifactResolver
public void resolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository )
throws ArtifactResolutionException
{
if ( artifact == null )
{
return;
}
// ----------------------------------------------------------------------
// Check for the existence of the artifact in the specified local
// ArtifactRepository. If it is present then simply return as the

View File

@ -832,6 +832,19 @@ public class DefaultPluginManager
// TODO: should I get the modified artifacts back into the project?
artifactResolver.resolve( artifact, context.getRemoteRepositories(), context.getLocalRepository() );
}
Set pluginArtifacts = new HashSet();
for ( Iterator it = context.getProject().getPluginArtifacts().iterator(); it.hasNext(); )
{
Artifact artifact = (Artifact) it.next();
artifactResolver.resolve( artifact, context.getRemoteRepositories(), context.getLocalRepository() );
getLogger().info( "=======>" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getBaseVersion() );
pluginArtifacts.add( artifact );
}
context.getProject().setPluginArtifacts( pluginArtifacts );
artifactResolver.resolve( context.getProject().getParentArtifact(), context.getRemoteRepositories(), context.getLocalRepository() );
}
}

View File

@ -0,0 +1,45 @@
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>maven-plugin-parent</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>maven-release-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Maven Release plugin</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-api</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-cvs</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-svn</artifactId>
<version>1.0-alpha-1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-provider-api</artifactId>
<version>1.0-alpha-3</version>
</dependency>
<dependency>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-file</artifactId>
<version>1.0-alpha-3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,411 @@
package org.apache.maven.plugin.release;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.transform.ReleaseArtifactTransformation;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.scm.ScmBean;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.manager.ScmManager;
import org.codehaus.plexus.PlexusConstants;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
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.Set;
/**
* @goal release
* @description Release plugin
* @requiresDependencyResolution test
*
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
* @version $Id: DoxiaMojo.java 169372 2005-05-09 22:47:34Z evenisse $
*/
public class ReleaseMojo
extends AbstractMojo
implements Contextualizable
{
/**
* @parameter expression="${basedir}"
* @required
* @readonly
*/
private String basedir;
/**
* @parameter expression="${project.build.directory}/checkout"
* @required
*/
private String workingDirectory;
/**
* @parameter expression="${project.scm.developerConnection}"
* @required
*/
private String urlScm;
/**
* @parameter expression="${username}"
*/
private String username;
/**
* @parameter expression="${password}"
*/
private String password;
/**
* @parameter expression="${tagBase}"
*/
private String tagBase = "../tags";
/**
* @parameter expression="${tag}"
*/
private String tag;
/**
* @parameter expression="${project.artifacts}"
* @readonly
*/
private Set dependencies;
/**
* @parameter expression="${project.pluginArtifacts}"
* @readonly
*/
private Set plugins;
/**
* @parameter expression="${localRepository}"
* @required
* @readonly
*/
private ArtifactRepository localRepository;
/**
* @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
* @required
* @readonly
*/
private ArtifactFactory artifactFactory;
/**
* @parameter expression="${project}"
* @required
* @readonly
*/
private MavenProject project;
private PlexusContainer container;
private ScmManager scmManager;
private static final String SNAPSHOT = "-SNAPSHOT";
public void execute()
throws MojoExecutionException
{
try
{
initScmManager();
}
catch ( Exception e )
{
throw new MojoExecutionException( "Can't initialize ReleaseMojo.", e );
}
try
{
prepareRelease();
performRelease();
}
finally
{
releaseScmManager();
}
}
private void prepareRelease()
throws MojoExecutionException
{
//checkStatus();
checkDependencies();
transformPom();
//commit();
//tag();
}
private void performRelease()
throws MojoExecutionException
{
//checkout();
}
private boolean isSnapshot( String version )
{
return version.endsWith( SNAPSHOT );
}
private ScmBean getScm()
{
ScmBean scm = new ScmBean();
scm.setScmManager( scmManager );
scm.setUrl( urlScm );
scm.setTag( tag );
scm.setTagBase( tagBase );
scm.setUsername( username );
scm.setPassword( password );
scm.setWorkingDirectory( workingDirectory );
return scm;
}
private void checkDependencies()
throws MojoExecutionException
{
MavenProject currentProject = project;
while ( currentProject.hasParent() )
{
Artifact parentArtifact = currentProject.getParentArtifact();
if ( isSnapshot( parentArtifact.getVersion() ) )
{
// throw new MojoExecutionException( "Can't release project due to non released parent." );
}
currentProject = project.getParent();
}
List snapshotDependencies = new ArrayList();
for ( Iterator i = dependencies.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getVersion() ) )
{
snapshotDependencies.add( artifact );
}
}
for ( Iterator i = plugins.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getVersion() ) )
{
snapshotDependencies.add( artifact );
}
}
if ( !snapshotDependencies.isEmpty() )
{
Collections.sort( snapshotDependencies );
StringBuffer message = new StringBuffer();
for ( Iterator i = snapshotDependencies.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
message.append( " " );
message.append( artifact.getGroupId() );
message.append( ":" );
message.append( artifact.getArtifactId() );
message.append( ":" );
message.append( artifact.getVersion() );
message.append( "\n" );
}
// throw new MojoExecutionException( "Can't release project due to non released dependencies :\n" +
// message.toString() );
}
}
private void checkStatus()
throws MojoExecutionException
{
List changedFiles;
try
{
ScmBean scm = getScm();
scm.setWorkingDirectory( basedir );
changedFiles = scm.getStatus();
}
catch ( ScmException e )
{
throw new MojoExecutionException( "An error is occurred in the status process.", e );
}
finally
{
releaseScmManager();
}
if ( !changedFiles.isEmpty() )
{
StringBuffer message = new StringBuffer();
for ( Iterator i = changedFiles.iterator(); i.hasNext(); )
{
ScmFile file = (ScmFile) i.next();
message.append( file.toString() );
message.append( "\n" );
}
throw new MojoExecutionException( "You have some uncommitted files : \n" + message.toString() );
}
}
private void transformPom()
throws MojoExecutionException
{
Model model = project.getModel();
if ( !isSnapshot( model.getVersion() ) )
{
throw new MojoExecutionException( "This project isn't a snapshot (" + model.getVersion() + ")." );
}
//Rewrite project version
model.setVersion( model.getVersion().substring( 0, model.getVersion().length() - SNAPSHOT.length() ) );
//Rewrite parent version
if ( project.hasParent() )
{
if ( isSnapshot( project.getParentArtifact().getBaseVersion() ) )
{
model.getParent().setVersion( project.getParentArtifact().getVersion() );
}
}
//Rewrite dependencies version
for ( Iterator i = dependencies.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getBaseVersion() ) )
{
for ( Iterator j = model.getDependencies().iterator(); j.hasNext(); )
{
Dependency dependency = (Dependency) j.next();
if ( artifact.getGroupId().equals( dependency.getGroupId() ) &&
artifact.getArtifactId().equals( dependency.getArtifactId() ) &&
artifact.getBaseVersion().equals( dependency.getVersion() ) &&
artifact.getType().equals( dependency.getType() ) )
{
dependency.setVersion( artifact.getVersion() );
}
}
}
}
//Rewrite plugins version
//TODO Resolve version
for ( Iterator i = plugins.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getBaseVersion() ) )
{
for ( Iterator j = model.getBuild().getPlugins().iterator(); j.hasNext(); )
{
Plugin plugin = (Plugin) j.next();
if ( artifact.getGroupId().equals( plugin.getGroupId() ) &&
artifact.getArtifactId().equals( plugin.getArtifactId() ) )
{
plugin.setGroupId( artifact.getGroupId() );
plugin.setVersion( artifact.getVersion() );
}
}
}
}
MavenXpp3Writer modelWriter = new MavenXpp3Writer();
try
{
//TODO: Write in pom file
java.io.StringWriter writer = new java.io.StringWriter();
modelWriter.write( writer, model );
getLog().info( writer.toString() );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Can't update pom.", e );
}
}
private void tag()
throws MojoExecutionException
{
try
{
getScm().tag();
}
catch ( Exception e )
{
throw new MojoExecutionException( "An error is occurred in the tag process.", e );
}
}
private void checkout()
throws MojoExecutionException
{
try
{
getScm().checkout();
}
catch ( Exception e )
{
throw new MojoExecutionException( "An error is occurred in the checkout process.", e );
}
}
private void initScmManager()
throws Exception
{
scmManager = (ScmManager) container.lookup( ScmManager.ROLE );
}
private void releaseScmManager()
{
try
{
container.release( scmManager );
}
catch ( Exception e )
{
getLog().warn( "Error releasing component - ignoring", e );
}
}
public void contextualize( Context context )
throws ContextException
{
container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
}
}

View File

@ -0,0 +1,255 @@
package org.apache.maven.plugin.scm;
/* ====================================================================
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFile;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.command.status.StatusScmResult;
import org.apache.maven.scm.command.tag.TagScmResult;
import org.apache.maven.scm.command.update.UpdateScmResult;
import org.apache.maven.scm.manager.ScmManager;
import org.apache.maven.scm.manager.NoSuchScmProviderException;
import org.apache.maven.scm.repository.ScmRepository;
import org.apache.maven.scm.repository.ScmRepositoryException;
import org.codehaus.plexus.embed.Embedder;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* A bean for using the Maven SCM API.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
* @version $Id: DoxiaMojo.java 169372 2005-05-09 22:47:34Z evenisse $
*/
public class ScmBean
{
private String username;
private String password;
private String url;
private String tag;
private String workingDirectory;
// note - this should not have a setter
private File checkoutDirectory;
private String tagBase;
private ScmManager scmManager;
private ScmManager getScmManager()
throws ScmException
{
if ( scmManager == null )
{
throw new ScmException( "scmManager isn't define." );
}
return scmManager;
}
private ScmRepository getScmRepository()
throws ScmException
{
ScmRepository repository;
try
{
repository = getScmManager().makeScmRepository( url );
if ( repository.getProvider().equals( "svn" ) )
{
SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
if ( username != null && username.length() > 0 )
{
svnRepo.setUser( username );
}
if ( password != null && password.length() > 0 )
{
svnRepo.setPassword( password );
}
if ( tagBase != null && tagBase.length() > 0 )
{
svnRepo.setTagBase( tagBase );
}
}
}
catch( Exception e )
{
throw new ScmException( "Can't load the scm provider.", e );
}
return repository;
}
private void checkResult( ScmResult result )
throws ScmException
{
if ( !result.isSuccess() )
{
// TODO: improve error handling
System.err.println( "Provider message:" );
System.err.println( result.getProviderMessage() );
System.err.println( "Command output:" );
System.err.println( result.getCommandOutput() );
throw new ScmException( "Error!" );
}
}
public void checkout()
throws ScmException, IOException
{
ScmRepository repository = getScmRepository();
checkoutDirectory = new File( workingDirectory );
if ( checkoutDirectory.exists() )
{
// TODO: sanity check that it is not . or .. or lower
FileUtils.deleteDirectory( checkoutDirectory );
}
checkoutDirectory.mkdirs();
CheckOutScmResult result = getScmManager().checkOut( repository, new ScmFileSet( checkoutDirectory ), tag );
checkResult( result );
}
public void update()
throws ScmException
{
ScmRepository repository = getScmRepository();
checkoutDirectory = new File( workingDirectory );
// TODO: want includes/excludes?
UpdateScmResult result = getScmManager().update( repository, new ScmFileSet( new File( workingDirectory ) ), tag );
checkResult( result );
}
public List getStatus()
throws ScmException
{
List changedFiles;
ScmRepository repository = getScmRepository();
// TODO: want includes/excludes?
StatusScmResult result = getScmManager().status( repository, new ScmFileSet( new File( workingDirectory ) ) );
checkResult( result );
changedFiles = result.getChangedFiles();
return changedFiles;
}
public void tag()
throws ScmException
{
ScmRepository repository = getScmRepository();
// TODO: want includes/excludes?
TagScmResult result = getScmManager().tag( repository, new ScmFileSet( new File( workingDirectory ) ), tag );
checkResult( result );
}
public void setScmManager( ScmManager scmManager )
{
this.scmManager = scmManager;
}
public void setUrl( String url )
{
this.url = url;
}
public String getUrl()
{
return url;
}
public void setTag( String tag )
{
this.tag = tag;
}
public String getTag()
{
return tag;
}
public void setWorkingDirectory( String workingDirectory )
{
this.workingDirectory = workingDirectory;
}
public String getWorkingDirectory()
{
return workingDirectory;
}
public File getCheckoutDirectory()
{
return checkoutDirectory;
}
public String getTagBase()
{
return tagBase;
}
public void setTagBase( String tagBase )
{
this.tagBase = tagBase;
}
public String getUsername()
{
return username;
}
public void setUsername( String username )
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword( String password )
{
this.password = password;
}
}

View File

@ -25,11 +25,13 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
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.ReleaseArtifactTransformation;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.Repository;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.inheritance.ModelInheritanceAssembler;
@ -279,8 +281,20 @@ public class DefaultMavenProjectBuilder
}
project.setParent( parentProject );
if ( parentProject != null )
{
Artifact parentArtifact = artifactFactory.createArtifact( parentProject.getGroupId(),
parentProject.getArtifactId(),
parentProject.getVersion(),
null,
"pom", null );
project.setParentArtifact( parentArtifact );
}
project.setRemoteArtifactRepositories( remoteRepositories );
project.setArtifacts( createArtifacts( project.getDependencies() ) );
project.setPluginArtifacts( createPluginArtifacts( project.getBuildPlugins() ) );
ModelValidationResult validationResult = validator.validate( model );
@ -336,6 +350,8 @@ public class DefaultMavenProjectBuilder
MavenProject parent = assembleLineage( model, lineage, aggregatedRemoteWagonRepositories, localRepository );
project.setParent( parent );
project.setParentArtifact( artifact );
}
return project;
@ -479,6 +495,35 @@ public class DefaultMavenProjectBuilder
return projectArtifacts;
}
protected Set createPluginArtifacts( List plugins )
{
Set pluginArtifacts = new HashSet();
for ( Iterator i = plugins.iterator(); i.hasNext(); )
{
Plugin p = (Plugin) i.next();
String version;
if ( StringUtils.isEmpty( p.getVersion() ) )
{
version = ReleaseArtifactTransformation.RELEASE_VERSION;
}
else
{
version = p.getVersion();
}
Artifact artifact = artifactFactory.createArtifact( p.getGroupId(), p.getArtifactId(), version,
null, "maven-plugin", null );
if ( artifact != null )
{
pluginArtifacts.add( artifact );
}
}
return pluginArtifacts;
}
public MavenProject buildStandaloneSuperProject( ArtifactRepository localRepository )
throws ProjectBuildingException
{

View File

@ -73,6 +73,10 @@ public class MavenProject
private Set artifacts;
private Artifact parentArtifact;
private Set pluginArtifacts;
private List remoteArtifactRepositories;
private List collectedProjects = Collections.EMPTY_LIST;
@ -668,6 +672,26 @@ public class MavenProject
return artifacts;
}
public void setPluginArtifacts( Set pluginArtifacts )
{
this.pluginArtifacts = pluginArtifacts;
}
public Set getPluginArtifacts()
{
return pluginArtifacts;
}
public void setParentArtifact( Artifact parentArtifact )
{
this.parentArtifact = parentArtifact;
}
public Artifact getParentArtifact()
{
return parentArtifact;
}
public List getRepositories()
{
return model.getRepositories();