diff --git a/src/it/it-set-scm-tag-001/invoker.properties b/src/it/it-set-scm-tag-001/invoker.properties index d6f553b788..62d509b5b2 100644 --- a/src/it/it-set-scm-tag-001/invoker.properties +++ b/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 diff --git a/src/it/it-set-scm-tag-001/pom.xml b/src/it/it-set-scm-tag-001/pom.xml index 52c747ae7c..ff6cdb58b5 100644 --- a/src/it/it-set-scm-tag-001/pom.xml +++ b/src/it/it-set-scm-tag-001/pom.xml @@ -9,5 +9,8 @@ set-scm-tag HEAD + dummy + dummy + dummy diff --git a/src/it/it-set-scm-tag-001/verify.bsh b/src/it/it-set-scm-tag-001/verify.bsh deleted file mode 100644 index fb60555c17..0000000000 --- a/src/it/it-set-scm-tag-001/verify.bsh +++ /dev/null @@ -1,32 +0,0 @@ -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-001/verify.groovy b/src/it/it-set-scm-tag-001/verify.groovy new file mode 100644 index 0000000000..ff47e6ab50 --- /dev/null +++ b/src/it/it-set-scm-tag-001/verify.groovy @@ -0,0 +1,6 @@ +pom = new File( basedir, "pom.xml" ).text; + +assert pom =~ /v1\.0<\/tag>/ +assert pom =~ /connection<\/connection>/ +assert pom =~ /developerConnection<\/developerConnection>/ +assert pom =~ /url<\/url>/ \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java b/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java index 8c7300331a..b7bbc2b66a 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java @@ -3,9 +3,10 @@ import javax.xml.stream.XMLStreamException; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; 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; @@ -14,6 +15,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; /** @@ -23,8 +25,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 { /** @@ -35,6 +36,30 @@ public class SetScmTagMojo @Parameter( property = "newTag" ) private String newTag; + /** + * 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. * @@ -42,12 +67,12 @@ 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( 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(); @@ -55,22 +80,54 @@ public void 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 was present" ); } - getLog().info( "Updating from tag " + scm.getTag() + " > " + newTag ); - boolean success = PomHelper.setProjectValue( pom, "/project/scm/tag", newTag ); - if ( !success ) + List 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: " + String.join( ", ", + failures ) + ". Please make sure they are present in the original POM. " ); } } catch ( IOException e )