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/it/it-set-scm-tag-002/invoker.properties b/src/it/it-set-scm-tag-002/invoker.properties index 8fa4808bef..7c52b98340 100644 --- a/src/it/it-set-scm-tag-002/invoker.properties +++ b/src/it/it-set-scm-tag-002/invoker.properties @@ -1,2 +1 @@ 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/verify.groovy b/src/it/it-set-scm-tag-002/verify.groovy new file mode 100644 index 0000000000..38db6f9a93 --- /dev/null +++ b/src/it/it-set-scm-tag-002/verify.groovy @@ -0,0 +1,3 @@ +pom = new File( basedir, "pom.xml" ).text; + +assert pom =~ /v1\.0<\/tag>/ \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index 83121bda23..1bb55612f9 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -165,7 +165,7 @@ public abstract class AbstractVersionsUpdaterMojo * @since 1.0-alpha-3 */ @Parameter( property = "generateBackupPoms", defaultValue = "true" ) - private boolean generateBackupPoms; + protected boolean generateBackupPoms; /** * Whether to allow snapshots when searching for the latest version of an artifact. diff --git a/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java b/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java index 8c7300331a..3883f41f2c 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetScmTagMojo.java @@ -2,18 +2,25 @@ import javax.xml.stream.XMLStreamException; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.io.Writer; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.model.Model; import org.apache.maven.model.Scm; +import org.apache.maven.model.io.xpp3.MavenXpp3Writer; 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; +import org.codehaus.plexus.util.FileUtils; +import static org.apache.commons.lang3.StringUtils.isAllBlank; import static org.apache.commons.lang3.StringUtils.isBlank; /** @@ -23,8 +30,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 { /** @@ -33,7 +39,31 @@ public class SetScmTagMojo * @since 2.5 */ @Parameter( property = "newTag" ) - private String newTag; + 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. @@ -42,40 +72,114 @@ 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( tag, connection, developerConnection, url ) ) { - throw new MojoFailureException( "'newTag' cannot be empty" ); + throw new MojoFailureException( + "One of: \"newTag\", \"connection\", \"developerConnection\", \"url\" should be provided." ); } super.execute(); } + /** + * {@inheritDoc} + */ @Override - protected void update( ModifiedPomXMLEventReader pom ) - throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException + protected void process( File file ) throws MojoExecutionException, MojoFailureException { try { - Model model = PomHelper.getRawModel( pom ); - Scm scm = model.getScm(); - if ( scm == null ) + Model model = PomHelper.getRawModel( file ); + update( model ); + if ( generateBackupPoms ) + { + backup( file ); + } + else { - throw new MojoFailureException( "No was present" ); + getLog().debug( "Skipping generation of backup file" ); } - getLog().info( "Updating from tag " + scm.getTag() + " > " + newTag ); - boolean success = PomHelper.setProjectValue( pom, "/project/scm/tag", newTag ); - if ( !success ) + try ( Writer writer = new BufferedWriter( new FileWriter( file ) ) ) { - throw new MojoFailureException( "Could not update the SCM tag" ); + new MavenXpp3Writer().write( writer, model ); } } catch ( IOException e ) { - throw new MojoExecutionException( e.getMessage(), e ); + throw new MojoFailureException( e.getMessage(), e ); + } + } + + @Override + @SuppressWarnings( "deprecated" ) + protected void update( ModifiedPomXMLEventReader pom ) + throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException + { + // nop + } + + private void backup( File file ) throws IOException + { + File backupFile = new File( file.getParentFile(), file.getName() + ".versionsBackup" ); + if ( !backupFile.exists() ) + { + getLog().debug( "Backing up " + file + " to " + backupFile ); + FileUtils.copyFile( file, backupFile ); + } + else + { + getLog().debug( "Leaving existing backup " + backupFile + " unmodified" ); + } + } + + /** + * Updates the scm data in the given Maven model file, creating the scm element if necessary. + * + * @param model Maven model + */ + protected void update( Model model ) + { + + if ( model.getScm() == null ) + { + model.setScm( new Scm() ); + getLog().debug( "Creating the element" ); + } + + if ( !isBlank( tag ) ) + { + if ( getLog().isDebugEnabled() ) + { + getLog().debug( "Updating the \"tag\" element to " + tag ); + } + model.getScm().setTag( tag ); + } + if ( !isBlank( connection ) ) + { + if ( getLog().isDebugEnabled() ) + { + getLog().debug( "Updating the \"connection\" element to " + connection ); + } + model.getScm().setConnection( connection ); + } + if ( !isBlank( developerConnection ) ) + { + if ( getLog().isDebugEnabled() ) + { + getLog().debug( "Updating the \"connection\" element to " + connection ); + } + model.getScm().setDeveloperConnection( developerConnection ); + } + if ( !isBlank( url ) ) + { + if ( getLog().isDebugEnabled() ) + { + getLog().debug( "Updating the \"url\" element to " + url ); + } + model.getScm().setUrl( url ); } } }