Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mojo for setting the SCM tag #184

Merged
merged 4 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 2.5 (NOT YET RELEASED)

* [Fixed Issue 182][issue-182]

Add goal for updating the SCM tag in the POM.

* [Fixed Issue 197][issue-197]

Java 1.7 as prerequisite.
Expand Down Expand Up @@ -76,15 +80,21 @@
Upgraded cobertura-maven-plugin used in report section
to 2.7.

[issue-34]: https://github.com/mojohaus/versions-maven-plugin/issues/34
[issue-37]: https://github.com/mojohaus/versions-maven-plugin/issues/37
[issue-46]: https://github.com/mojohaus/versions-maven-plugin/issues/46
[issue-94]: https://github.com/mojohaus/versions-maven-plugin/issues/94
[issue-129]: https://github.com/mojohaus/versions-maven-plugin/issues/129
[issue-162]: https://github.com/mojohaus/versions-maven-plugin/issues/162
[issue-166]: https://github.com/mojohaus/versions-maven-plugin/issues/166
[issue-167]: https://github.com/mojohaus/versions-maven-plugin/issues/167
[issue-168]: https://github.com/mojohaus/versions-maven-plugin/issues/168
[issue-177]: https://github.com/mojohaus/versions-maven-plugin/issues/177

[issue-34]: https://github.com/jenkinsci/java-client-api/issues/34
[issue-37]: https://github.com/jenkinsci/java-client-api/issues/37
[issue-46]: https://github.com/jenkinsci/java-client-api/issues/46
[issue-94]: https://github.com/jenkinsci/java-client-api/issues/94
[issue-129]: https://github.com/jenkinsci/java-client-api/issues/129
[issue-162]: https://github.com/jenkinsci/java-client-api/issues/162
[issue-166]: https://github.com/jenkinsci/java-client-api/issues/166
[issue-167]: https://github.com/jenkinsci/java-client-api/issues/167
[issue-168]: https://github.com/jenkinsci/java-client-api/issues/168
[issue-177]: https://github.com/jenkinsci/java-client-api/issues/177
[issue-182]: https://github.com/mojohaus/versions-maven-plugin/issues/182
[issue-185]: https://github.com/mojohaus/versions-maven-plugin/issues/185
[issue-187]: https://github.com/mojohaus/versions-maven-plugin/issues/187
[pull-189]: https://github.com/mojohaus/versions-maven-plugin/pull/189
[issue-197]: https://github.com/mojohaus/versions-maven-plugin/issues/197
[issue-198]: https://github.com/mojohaus/versions-maven-plugin/issues/198

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
<contributor>
<name>Erik Schepers</name>
</contributor>
<contributor>
<name>Anton Johansson</name>
<email>antoon.johansson@gmail.com</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>
2 changes: 2 additions & 0 deletions src/it/it-set-scm-tag-004/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-004/module-a1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<parent>
<groupId>localhost</groupId>
<artifactId>it-set-scm-tag-003</artifactId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>module-a1</artifactId>
<packaging>jar</packaging>

</project>
13 changes: 13 additions & 0 deletions src/it/it-set-scm-tag-004/module-a2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<parent>
<groupId>localhost</groupId>
<artifactId>it-set-scm-tag-003</artifactId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>module-a2</artifactId>
<packaging>jar</packaging>

</project>
17 changes: 17 additions & 0 deletions src/it/it-set-scm-tag-004/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<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>
<modules>
<module>module-a1</module>
<module>module-a2</module>
</modules>
</project>
32 changes: 32 additions & 0 deletions src/it/it-set-scm-tag-004/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;
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, aggregator = 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 @@ -230,12 +230,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