Split ReleaseMojo to PrepareReleaseMojo and PerformReleaseMojo

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@170521 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Emmanuel Venisse 2005-05-17 06:53:39 +00:00
parent d85ce5d9ef
commit c85f4fd8c8
3 changed files with 254 additions and 204 deletions

View File

@ -0,0 +1,156 @@
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.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 java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
/**
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
* @version $Id: DoxiaMojo.java 169372 2005-05-09 22:47:34Z evenisse $
*/
public abstract class AbstractReleaseMojo
extends AbstractMojo
implements Contextualizable
{
/**
* @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;
private PlexusContainer container;
private ScmManager scmManager;
protected ScmManager getScmManager()
{
return scmManager;
}
protected 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;
}
public void execute()
throws MojoExecutionException
{
try
{
initScmManager();
}
catch ( Exception e )
{
throw new MojoExecutionException( "Can't initialize ReleaseMojo.", e );
}
try
{
executeTask();
}
finally
{
releaseScmManager();
}
}
protected abstract void executeTask()
throws MojoExecutionException;
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,50 @@
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.plugin.MojoExecutionException;
/**
* @goal perform
* @description Perform a release from SCM
* @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 PerformReleaseMojo
extends AbstractReleaseMojo
{
protected void executeTask()
throws MojoExecutionException
{
checkout();
}
private void checkout()
throws MojoExecutionException
{
try
{
getScm().checkout();
}
catch ( Exception e )
{
throw new MojoExecutionException( "An error is occurred in the checkout process.", e );
}
}
}

View File

@ -17,29 +17,18 @@ package org.apache.maven.plugin.release;
*/
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.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
@ -47,16 +36,15 @@ import java.util.List;
import java.util.Set;
/**
* @goal release
* @description Release plugin
* @goal prepare
* @description Prepare for a release in SCM
* @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
public class PrepareReleaseMojo
extends AbstractReleaseMojo
{
/**
* @parameter expression="${basedir}"
@ -65,64 +53,6 @@ public class ReleaseMojo
*/
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
@ -130,37 +60,9 @@ public class ReleaseMojo
*/
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()
protected void executeTask()
throws MojoExecutionException
{
checkStatus();
@ -174,28 +76,39 @@ public class ReleaseMojo
tag();
}
private void performRelease()
throws MojoExecutionException
{
checkout();
}
private boolean isSnapshot( String version )
{
return version.endsWith( SNAPSHOT );
}
private ScmBean getScm()
private void checkStatus()
throws MojoExecutionException
{
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;
getLog().info( "Verifying no modifications are present." );
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 );
}
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( "Cannot prepare a release - You have some uncommitted files : \n" + message.toString() );
}
}
private void checkDependencies()
@ -216,7 +129,7 @@ public class ReleaseMojo
List snapshotDependencies = new ArrayList();
for ( Iterator i = dependencies.iterator(); i.hasNext(); )
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getVersion() ) )
@ -224,7 +137,7 @@ public class ReleaseMojo
snapshotDependencies.add( artifact );
}
}
for ( Iterator i = plugins.iterator(); i.hasNext(); )
for ( Iterator i = project.getPluginArtifacts().iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getVersion() ) )
@ -247,40 +160,8 @@ public class ReleaseMojo
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() );
throw new MojoExecutionException( "Can't release project due to non released dependencies :\n"
+ message.toString() );
}
}
@ -306,7 +187,7 @@ public class ReleaseMojo
}
//Rewrite dependencies version
for ( Iterator i = dependencies.iterator(); i.hasNext(); )
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getBaseVersion() ) )
@ -314,10 +195,10 @@ public class ReleaseMojo
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() ) )
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() );
}
@ -326,7 +207,7 @@ public class ReleaseMojo
}
//Rewrite plugins version
for ( Iterator i = plugins.iterator(); i.hasNext(); )
for ( Iterator i = project.getPluginArtifacts().iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
if ( isSnapshot( artifact.getBaseVersion() ) )
@ -334,10 +215,10 @@ public class ReleaseMojo
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() ) )
if ( artifact.getGroupId().equals( plugin.getGroupId() )
&& artifact.getArtifactId().equals( plugin.getArtifactId() ) )
{
plugin.setGroupId( artifact.getGroupId() );
plugin.setGroupId( artifact.getGroupId() );
plugin.setVersion( artifact.getVersion() );
}
}
@ -347,9 +228,9 @@ public class ReleaseMojo
MavenXpp3Writer modelWriter = new MavenXpp3Writer();
try
{
//TODO: Write in pom file
//TODO: Write in pom file
//TODO: Write only necessary informations
java.io.StringWriter writer = new java.io.StringWriter();
StringWriter writer = new StringWriter();
modelWriter.write( writer, model );
getLog().info( writer.toString() );
}
@ -371,41 +252,4 @@ public class ReleaseMojo
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 );
}
}