Skip to content

Commit

Permalink
New feature (mojohaus#477): connection, developerConnection, url argu…
Browse files Browse the repository at this point in the history
…ments for set-scm-tag
  • Loading branch information
jarmoniuk committed Aug 31, 2022
1 parent a440adc commit f548113
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 54 deletions.
3 changes: 1 addition & 2 deletions src/it/it-set-scm-tag-001/invoker.properties
@@ -1,2 +1 @@
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0
invoker.buildResult.1=success
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0 -Dconnection=connection -DdeveloperConnection=developerConnection -Durl=url
3 changes: 3 additions & 0 deletions src/it/it-set-scm-tag-001/pom.xml
Expand Up @@ -9,5 +9,8 @@
<name>set-scm-tag</name>
<scm>
<tag>HEAD</tag>
<connection>dummy</connection>
<developerConnection>dummy</developerConnection>
<url>dummy</url>
</scm>
</project>
32 changes: 0 additions & 32 deletions src/it/it-set-scm-tag-001/verify.bsh

This file was deleted.

6 changes: 6 additions & 0 deletions src/it/it-set-scm-tag-001/verify.groovy
@@ -0,0 +1,6 @@
pom = new File( basedir, "pom.xml" ).text;

assert pom =~ /<tag>v1\.0<\/tag>/
assert pom =~ /<connection>connection<\/connection>/
assert pom =~ /<developerConnection>developerConnection<\/developerConnection>/
assert pom =~ /<url>url<\/url>/
1 change: 0 additions & 1 deletion src/it/it-set-scm-tag-002/invoker.properties
@@ -1,2 +1 @@
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0
invoker.buildResult.1=failure
3 changes: 3 additions & 0 deletions src/it/it-set-scm-tag-002/verify.groovy
@@ -0,0 +1,3 @@
pom = new File( basedir, "pom.xml" ).text;

assert pom =~ /<tag>v1\.0<\/tag>/
Expand Up @@ -165,7 +165,7 @@ public abstract class AbstractVersionsUpdaterMojo
* @since 1.0-alpha-3
*/
@Parameter( property = "generateBackupPoms", defaultValue = "true" )
private boolean generateBackupPoms;
protected boolean generateBackupPoms;

/**
* Whether to allow snapshots when searching for the latest version of an artifact.
Expand Down
140 changes: 122 additions & 18 deletions src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java
Expand Up @@ -2,18 +2,25 @@

import javax.xml.stream.XMLStreamException;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.model.Model;
import org.apache.maven.model.Scm;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.plexus.util.FileUtils;

import static org.apache.commons.lang3.StringUtils.isAllBlank;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
Expand All @@ -23,8 +30,7 @@
* @since 2.5
*/
@Mojo( name = "set-scm-tag", requiresDirectInvocation = true, aggregator = true, threadSafe = true )
public class SetScmTagMojo
extends AbstractVersionsUpdaterMojo
public class SetScmTagMojo extends AbstractVersionsUpdaterMojo
{

/**
Expand All @@ -33,7 +39,31 @@ public class SetScmTagMojo
* @since 2.5
*/
@Parameter( property = "newTag" )
private String newTag;
private String tag;

/**
* The new SCM connection property
*
* @since 2.12.0
*/
@Parameter( property = "connection" )
private String connection;

/**
* The new SCM developerConnection property
*
* @since 2.12.0
*/
@Parameter( property = "developerConnection" )
private String developerConnection;

/**
* The new SCM url property
*
* @since 2.12.0
*/
@Parameter( property = "url" )
private String url;

/**
* Called when this mojo is executed.
Expand All @@ -42,40 +72,114 @@ public class SetScmTagMojo
* @throws org.apache.maven.plugin.MojoFailureException when things go wrong.
*/
@Override
public void execute()
throws MojoExecutionException, MojoFailureException
public void execute() throws MojoExecutionException, MojoFailureException
{
if ( isBlank( newTag ) )
if ( isAllBlank( tag, connection, developerConnection, url ) )
{
throw new MojoFailureException( "'newTag' cannot be empty" );
throw new MojoFailureException(
"One of: \"newTag\", \"connection\", \"developerConnection\", \"url\" should be provided." );
}

super.execute();
}

/**
* {@inheritDoc}
*/
@Override
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
protected void process( File file ) throws MojoExecutionException, MojoFailureException
{
try
{
Model model = PomHelper.getRawModel( pom );
Scm scm = model.getScm();
if ( scm == null )
Model model = PomHelper.getRawModel( file );
update( model );
if ( generateBackupPoms )
{
backup( file );
}
else
{
throw new MojoFailureException( "No <scm> was present" );
getLog().debug( "Skipping generation of backup file" );
}
getLog().info( "Updating from tag " + scm.getTag() + " > " + newTag );

boolean success = PomHelper.setProjectValue( pom, "/project/scm/tag", newTag );
if ( !success )
try ( Writer writer = new BufferedWriter( new FileWriter( file ) ) )
{
throw new MojoFailureException( "Could not update the SCM tag" );
new MavenXpp3Writer().write( writer, model );
}
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
throw new MojoFailureException( e.getMessage(), e );
}
}

@Override
@SuppressWarnings( "deprecated" )
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
{
// nop
}

private void backup( File file ) throws IOException
{
File backupFile = new File( file.getParentFile(), file.getName() + ".versionsBackup" );
if ( !backupFile.exists() )
{
getLog().debug( "Backing up " + file + " to " + backupFile );
FileUtils.copyFile( file, backupFile );
}
else
{
getLog().debug( "Leaving existing backup " + backupFile + " unmodified" );
}
}

/**
* Updates the scm data in the given Maven model file, creating the <code>scm</code> element if necessary.
*
* @param model Maven model
*/
protected void update( Model model )
{

if ( model.getScm() == null )
{
model.setScm( new Scm() );
getLog().debug( "Creating the <scm> element" );
}

if ( !isBlank( tag ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Updating the \"tag\" element to " + tag );
}
model.getScm().setTag( tag );
}
if ( !isBlank( connection ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Updating the \"connection\" element to " + connection );
}
model.getScm().setConnection( connection );
}
if ( !isBlank( developerConnection ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Updating the \"connection\" element to " + connection );
}
model.getScm().setDeveloperConnection( developerConnection );
}
if ( !isBlank( url ) )
{
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Updating the \"url\" element to " + url );
}
model.getScm().setUrl( url );
}
}
}

0 comments on commit f548113

Please sign in to comment.