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 8c97b2c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 48 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>/
95 changes: 81 additions & 14 deletions src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java
Expand Up @@ -3,9 +3,11 @@
import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.model.Model;
import org.apache.maven.model.Scm;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -14,6 +16,7 @@
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

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

/**
Expand All @@ -23,8 +26,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 @@ -35,42 +37,107 @@ public class SetScmTagMojo
@Parameter( property = "newTag" )
private String newTag;

/**
* The new SCM tag to set.
*
* @since 2.5
*/
@Parameter( property = "tag" )
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.
*
* @throws org.apache.maven.plugin.MojoExecutionException when things go wrong.
* @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( newTag, connection, developerConnection, url ) )
{
throw new MojoFailureException( "'newTag' cannot be empty" );
throw new MojoFailureException(
"One of: \"newTag\", \"connection\", \"developerConnection\", \"url\" should be provided." );
}

super.execute();
}

@Override
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
{
try
{
Model model = PomHelper.getRawModel( pom );
Scm scm = model.getScm();
Scm scm = PomHelper.getRawModel( pom ).getScm();
if ( scm == null )
{
throw new MojoFailureException( "No <scm> was present" );
}
getLog().info( "Updating from tag " + scm.getTag() + " > " + newTag );

boolean success = PomHelper.setProjectValue( pom, "/project/scm/tag", newTag );
if ( !success )
List<String> failures = new ArrayList<>();
if ( !isBlank( newTag ) )
{
getLog().info( "Updating tag: " + scm.getTag() + " > " + newTag );
if ( !PomHelper.setProjectValue( pom, "/project/scm/tag", newTag ) )
{
failures.add( "tag: " + newTag );
}
}
if ( !isBlank( connection ) )
{
getLog().info( "Updating connection: " + scm.getConnection() + " -> " + connection );
if ( !PomHelper.setProjectValue( pom, "/project/scm/connection", connection ) )
{
failures.add( "connection: " + connection );
}
}
if ( !isBlank( developerConnection ) )
{
getLog().info( "Updating developerConnection: " + scm.getDeveloperConnection() + " -> "
+ developerConnection );
if ( !PomHelper.setProjectValue( pom, "/project/scm/developerConnection", developerConnection ) )
{
failures.add( "developerConnection: " + developerConnection );
}
}
if ( !isBlank( url ) )
{
getLog().info( "Updating url: " + scm.getUrl() + " -> " + url );
if ( !PomHelper.setProjectValue( pom, "/project/scm/url", url ) )
{
failures.add( "url: " + url );
}
}
if ( !failures.isEmpty() )
{
throw new MojoFailureException( "Could not update the SCM tag" );
throw new MojoFailureException( "Could not update one or more SCM elements: " + failures.stream()
.collect( Collectors.joining( ", " ) )
+ ". Please make sure they are present in the original POM. " );
}
}
catch ( IOException e )
Expand Down

0 comments on commit 8c97b2c

Please sign in to comment.