diff --git a/pom.xml b/pom.xml index 7b725ed537..a7fe240511 100644 --- a/pom.xml +++ b/pom.xml @@ -73,6 +73,11 @@ Erik Schepers + + Anton Johansson + antoon.johansson@gmail.com + +1 + diff --git a/src/it/it-set-scm-tag-001/invoker.properties b/src/it/it-set-scm-tag-001/invoker.properties new file mode 100644 index 0000000000..d6f553b788 --- /dev/null +++ b/src/it/it-set-scm-tag-001/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0 +invoker.buildResult.1=success diff --git a/src/it/it-set-scm-tag-001/pom.xml b/src/it/it-set-scm-tag-001/pom.xml new file mode 100644 index 0000000000..52c747ae7c --- /dev/null +++ b/src/it/it-set-scm-tag-001/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + + localhost + it-set-scm-tag-001 + 1.0 + pom + set-scm-tag + + HEAD + + diff --git a/src/it/it-set-scm-tag-001/verify.bsh b/src/it/it-set-scm-tag-001/verify.bsh new file mode 100644 index 0000000000..fb60555c17 --- /dev/null +++ b/src/it/it-set-scm-tag-001/verify.bsh @@ -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\\E\\s*\\Q\\Ev1\\.0\\Q\\E\\s*\\Q\\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; diff --git a/src/it/it-set-scm-tag-002/invoker.properties b/src/it/it-set-scm-tag-002/invoker.properties new file mode 100644 index 0000000000..8fa4808bef --- /dev/null +++ b/src/it/it-set-scm-tag-002/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag -DnewTag=v1.0 +invoker.buildResult.1=failure diff --git a/src/it/it-set-scm-tag-002/pom.xml b/src/it/it-set-scm-tag-002/pom.xml new file mode 100644 index 0000000000..7a49f2794b --- /dev/null +++ b/src/it/it-set-scm-tag-002/pom.xml @@ -0,0 +1,10 @@ + + 4.0.0 + + localhost + it-set-scm-tag-002 + 1.0 + pom + set-scm-tag + diff --git a/src/it/it-set-scm-tag-003/invoker.properties b/src/it/it-set-scm-tag-003/invoker.properties new file mode 100644 index 0000000000..aec98b183d --- /dev/null +++ b/src/it/it-set-scm-tag-003/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals.1=${project.groupId}:${project.artifactId}:${project.version}:set-scm-tag +invoker.buildResult.1=failure diff --git a/src/it/it-set-scm-tag-003/pom.xml b/src/it/it-set-scm-tag-003/pom.xml new file mode 100644 index 0000000000..d42bfa29d5 --- /dev/null +++ b/src/it/it-set-scm-tag-003/pom.xml @@ -0,0 +1,13 @@ + + 4.0.0 + + localhost + it-set-scm-tag-003 + 1.0 + pom + set-scm-tag + + HEAD + + diff --git a/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java b/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java new file mode 100644 index 0000000000..4d6a333c34 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java @@ -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 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 ); + } + } +} diff --git a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index 27382aadd7..511e596288 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -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 true 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 stack = new Stack(); String path = ""; final Pattern matchScopeRegex; boolean madeReplacement = false; - matchScopeRegex = Pattern.compile( "/project/version" ); + matchScopeRegex = Pattern.compile( pattern ); pom.rewind();