/
SetScmTagMojo.java
148 lines (134 loc) · 4.85 KB
/
SetScmTagMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.codehaus.mojo.versions;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
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;
import static org.apache.commons.lang3.StringUtils.isAllBlank;
import static org.apache.commons.lang3.StringUtils.isBlank;
/**
* Updates the current project's SCM tag.
*
* @author Anton Johansson
* @since 2.5
*/
@Mojo( name = "set-scm-tag", requiresDirectInvocation = true, aggregator = true, threadSafe = true )
public class SetScmTagMojo extends AbstractVersionsUpdaterMojo
{
/**
* The new SCM tag to set.
*
* @since 2.5
*/
@Parameter( property = "newTag", alias = "tag" )
private String newTag;
/**
* The new SCM tag to set.
*
* @since 2.5
*/
@Parameter( property = "tag" )
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.
*
* @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 ( isAllBlank( newTag, connection, developerConnection, url ) )
{
throw new MojoFailureException(
"One of: \"newTag\", \"connection\", \"developerConnection\", \"url\" should be provided." );
}
super.execute();
}
@Override
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException
{
try
{
Scm scm = PomHelper.getRawModel( pom ).getScm();
if ( scm == null )
{
throw new MojoFailureException( "No <scm> was present" );
}
List<String> 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 one or more SCM elements: " + failures.stream()
.collect( Collectors.joining( ", " ) )
+ ". Please make sure they are present in the original POM. " );
}
}
catch ( IOException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
}