Skip to content

Commit

Permalink
Add Mojo for setting the SCM tag
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-johansson committed Jul 13, 2017
1 parent 54dd631 commit c189331
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
<contributor>
<name>Erik Schepers</name>
</contributor>
<contributor>
<name>Anton Johansson</name>
<email>[email protected]</email>
<timezone>+1</timezone>
</contributor>
</contributors>

<prerequisites>
Expand Down
2 changes: 2 additions & 0 deletions src/it/it-set-scm-tag-001/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0
invoker.buildResult.1=success
13 changes: 13 additions & 0 deletions src/it/it-set-scm-tag-001/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-set-scm-tag-001</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>set-scm-tag</name>
<scm>
<tag>HEAD</tag>
</scm>
</project>
32 changes: 32 additions & 0 deletions src/it/it-set-scm-tag-001/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.*;
import java.util.regex.*;

try
{
File file = new File( basedir, "pom.xml" );

BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( file ), "UTF-8" ) );
StringBuilder buf = new StringBuilder();
String line = in.readLine();
while ( line != null )
{
buf.append( line );
buf.append( " " );
line = in.readLine();
}

Pattern p = Pattern.compile( "\\Q<scm>\\E\\s*\\Q<tag>\\Ev1\\.0\\Q</tag>\\E\\s*\\Q</scm>\\E" );
Matcher m = p.matcher( buf.toString() );
if ( !m.find() )
{
System.out.println( "Setting new tag" );
return false;
}
}
catch( Throwable t )
{
t.printStackTrace();
return false;
}

return true;
2 changes: 2 additions & 0 deletions src/it/it-set-scm-tag-002/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0
invoker.buildResult.1=failure
10 changes: 10 additions & 0 deletions src/it/it-set-scm-tag-002/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-set-scm-tag-002</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>set-scm-tag</name>
</project>
2 changes: 2 additions & 0 deletions src/it/it-set-scm-tag-003/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag
invoker.buildResult.1=failure
13 changes: 13 additions & 0 deletions src/it/it-set-scm-tag-003/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>localhost</groupId>
<artifactId>it-set-scm-tag-003</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>set-scm-tag</name>
<scm>
<tag>HEAD</tag>
</scm>
</project>
80 changes: 80 additions & 0 deletions src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.codehaus.mojo.versions;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.io.IOException;

import javax.xml.stream.XMLStreamException;

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

/**
* Updates the current project's SCM tag.
*
* @author Anton Johansson
* @since 2.5
*/
@Mojo( name = "set-scm-tag", requiresDirectInvocation = true )
public class SetScmTagMojo
extends AbstractVersionsUpdaterMojo
{

/**
* The new SCM tag to set.
*
* @since 2.5
*/
@Parameter( property = "newTag" )
private String newTag;

/**
* 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
{
if ( isBlank(newTag) )
{
throw new MojoFailureException("'newTag' cannot be empty");
}

super.execute();
}

@Override
protected void update(ModifiedPomXMLEventReader pom) throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
{
try
{
Model model = PomHelper.getRawModel( pom );
Scm scm = model.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 )
{
throw new MojoFailureException( "Could not update the SCM tag" );
}
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
}
17 changes: 16 additions & 1 deletion src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,27 @@ else if ( matchScopeRegex.matcher( path ).matches() )
*/
public static boolean setProjectVersion( final ModifiedPomXMLEventReader pom, final String value )
throws XMLStreamException
{
return setProjectValue( pom, "/project/version", value );
}

/**
* Searches the pom re-defining a project value using the given pattern.
*
* @param pom The pom to modify.
* @param pattern The pattern to look for.
* @param value The new value of the property.
* @return <code>true</code> if a replacement was made.
* @throws XMLStreamException if something went wrong.
*/
public static boolean setProjectValue( final ModifiedPomXMLEventReader pom, String pattern, final String value )
throws XMLStreamException
{
Stack<String> stack = new Stack<String>();
String path = "";
final Pattern matchScopeRegex;
boolean madeReplacement = false;
matchScopeRegex = Pattern.compile( "/project/version" );
matchScopeRegex = Pattern.compile( pattern );

pom.rewind();

Expand Down

0 comments on commit c189331

Please sign in to comment.