/
SetScmTagMojo.java
185 lines (167 loc) · 5.41 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.codehaus.mojo.versions;
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;
/**
* 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" )
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( tag, connection, developerConnection, url ) )
{
throw new MojoFailureException(
"One of: \"newTag\", \"connection\", \"developerConnection\", \"url\" should be provided." );
}
super.execute();
}
/**
* {@inheritDoc}
*/
@Override
protected void process( File file ) throws MojoExecutionException, MojoFailureException
{
try
{
Model model = PomHelper.getRawModel( file );
update( model );
if ( generateBackupPoms )
{
backup( file );
}
else
{
getLog().debug( "Skipping generation of backup file" );
}
try ( Writer writer = new BufferedWriter( new FileWriter( file ) ) )
{
new MavenXpp3Writer().write( writer, model );
}
}
catch ( IOException 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 <code>scm</code> element if necessary.
*
* @param model Maven model
*/
protected void update( Model model )
{
if ( model.getScm() == null )
{
model.setScm( new Scm() );
getLog().debug( "Creating the <scm> 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 );
}
}
}