/
AbstractVersionsUpdaterMojo.java
512 lines (461 loc) · 17.3 KB
/
AbstractVersionsUpdaterMojo.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package org.codehaus.mojo.versions;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.path.PathTranslator;
import org.apache.maven.settings.Settings;
import org.codehaus.mojo.versions.api.ArtifactVersions;
import org.codehaus.mojo.versions.api.DefaultVersionsHelper;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.api.VersionsHelper;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.WriterFactory;
import org.codehaus.stax2.XMLInputFactory2;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
/**
* Abstract base class for Versions Mojos.
*
* @author Stephen Connolly
*/
public abstract class AbstractVersionsUpdaterMojo
extends AbstractMojo
{
// ------------------------------ FIELDS ------------------------------
/**
* The Maven Project.
*
* @since 1.0-alpha-1
*/
@Parameter( defaultValue = "${project}", required = true, readonly = true )
protected MavenProject project;
/**
* @since 1.0-alpha-1
*/
@Component
protected org.apache.maven.artifact.factory.ArtifactFactory artifactFactory;
/**
* @since 1.0-alpha-1
*/
@Component
protected org.apache.maven.artifact.resolver.ArtifactResolver resolver;
/**
* @since 1.0-alpha-1
*/
@Component
protected MavenProjectBuilder projectBuilder;
/**
* @since 1.0-alpha-1
*/
@Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true )
protected List reactorProjects;
/**
* The artifact metadata source to use.
*
* @since 1.0-alpha-1
*/
@Component
protected ArtifactMetadataSource artifactMetadataSource;
/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true )
protected List<ArtifactRepository> remoteArtifactRepositories;
/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${project.pluginArtifactRepositories}", readonly = true )
protected List<ArtifactRepository> remotePluginRepositories;
/**
* @since 1.0-alpha-1
*/
@Parameter( defaultValue = "${localRepository}", readonly = true )
protected ArtifactRepository localRepository;
/**
* @component
* @since 1.0-alpha-3
*/
@Component
private WagonManager wagonManager;
/**
* @since 1.0-alpha-3
*/
@Parameter( defaultValue = "${settings}", readonly = true )
protected Settings settings;
/**
* settings.xml's server id for the URL. This is used when wagon needs extra authentication information.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "maven.version.rules.serverId", defaultValue = "serverId" )
private String serverId;
/**
* URI of a ruleSet file containing the rules that control how to compare
* version numbers. The URI could be either a Wagon URI or a classpath URI
* (e.g. <code>classpath:///package/sub/package/rules.xml</code>).
*
* @since 1.0-alpha-3
*/
@Parameter( property = "maven.version.rules" )
private String rulesUri;
/**
* Controls whether a backup pom should be created.
*
* @since 1.0-alpha-3
*/
@Parameter( property = "generateBackupPoms", defaultValue = "true" )
private boolean generateBackupPoms;
/**
* Whether to allow snapshots when searching for the latest version of an artifact.
*
* @since 1.0-alpha-1
*/
@Parameter( property = "allowSnapshots", defaultValue = "false" )
protected boolean allowSnapshots;
/**
* Our versions helper.
*/
private VersionsHelper helper;
/**
* The Maven Session.
*
* @since 1.0-alpha-1
*/
@Parameter( defaultValue = "${session}", required = true, readonly = true )
protected MavenSession session;
@Component
protected PathTranslator pathTranslator;
@Component
protected ArtifactResolver artifactResolver;
// --------------------- GETTER / SETTER METHODS ---------------------
public VersionsHelper getHelper()
throws MojoExecutionException
{
if ( helper == null )
{
helper = new DefaultVersionsHelper( artifactFactory, artifactResolver, artifactMetadataSource,
remoteArtifactRepositories, remotePluginRepositories, localRepository,
wagonManager, settings, serverId, rulesUri, getLog(), session,
pathTranslator );
}
return helper;
}
/**
* Getter for property 'project'.
*
* @return Value for property 'project'.
* @since 1.0-alpha-1
*/
public MavenProject getProject()
{
return project;
}
/**
* Setter for property 'project'.
*
* @param project Value to set for property 'project'.
* @since 1.0-alpha-1
*/
public void setProject( MavenProject project )
{
this.project = project;
}
public String getVersion()
{
return getProject() == null ? null : getProject().getVersion();
}
// ------------------------ INTERFACE METHODS ------------------------
// --------------------- Interface Mojo ---------------------
/**
* {@inheritDoc}
*
* @since 1.0-alpha-1
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
File outFile = project.getFile();
process( outFile );
}
// -------------------------- OTHER METHODS --------------------------
/**
* Finds the latest version of the specified artifact that matches the version range.
*
* @param artifact The artifact.
* @param versionRange The version range.
* @param allowingSnapshots <code>null</code> for no override, otherwise the local override to apply.
* @param usePluginRepositories Use plugin repositories
* @return The latest version of the specified artifact that matches the specified version range or
* <code>null</code> if no matching version could be found.
* @throws ArtifactMetadataRetrievalException If the artifact metadata could not be found.
* @throws MojoExecutionException if something goes wrong.
* @since 1.0-alpha-1
*/
protected ArtifactVersion findLatestVersion( Artifact artifact, VersionRange versionRange,
Boolean allowingSnapshots, boolean usePluginRepositories )
throws ArtifactMetadataRetrievalException, MojoExecutionException
{
boolean includeSnapshots = this.allowSnapshots;
if ( Boolean.TRUE.equals( allowingSnapshots ) )
{
includeSnapshots = true;
}
if ( Boolean.FALSE.equals( allowingSnapshots ) )
{
includeSnapshots = false;
}
final ArtifactVersions artifactVersions = getHelper().lookupArtifactVersions( artifact, usePluginRepositories );
return artifactVersions.getNewestVersion( versionRange, includeSnapshots );
}
/**
* Gets the property value that is defined in the pom. This is an extension point to allow updating a file external
* to the reactor.
*
* @param pom The pom.
* @param property The property.
* @return The value as defined in the pom or <code>null</code> if not defined.
* @since 1.0-alpha-1
*/
protected String getPropertyValue( StringBuilder pom, String property )
{
return project.getProperties().getProperty( property );
}
/**
* Processes the specified file. This is an extension point to allow updating a file external to the reactor.
*
* @param outFile The file to process.
* @throws MojoExecutionException If things go wrong.
* @throws MojoFailureException If things go wrong.
* @since 1.0-alpha-1
*/
protected void process( File outFile )
throws MojoExecutionException, MojoFailureException
{
try
{
StringBuilder input = PomHelper.readXmlFile( outFile );
ModifiedPomXMLEventReader newPom = newModifiedPomXER( input, outFile.getAbsolutePath() );
update( newPom );
if ( newPom.isModified() )
{
if ( generateBackupPoms )
{
File backupFile = new File( outFile.getParentFile(), outFile.getName() + ".versionsBackup" );
if ( !backupFile.exists() )
{
getLog().debug( "Backing up " + outFile + " to " + backupFile );
FileUtils.copyFile( outFile, backupFile );
}
else
{
getLog().debug( "Leaving existing backup " + backupFile + " unmodified" );
}
}
else
{
getLog().debug( "Skipping generation of backup file" );
}
writeFile( outFile, input );
}
}
catch ( IOException | XMLStreamException e )
{
getLog().error( e );
}
catch ( ArtifactMetadataRetrievalException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
/**
* Creates a {@link org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader} from a StringBuilder.
*
* @param input The XML to read and modify.
* @param path Path pointing to the source of the XML
* @return The {@link org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader}.
*/
protected final ModifiedPomXMLEventReader newModifiedPomXER( StringBuilder input, String path )
{
ModifiedPomXMLEventReader newPom = null;
try
{
XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
newPom = new ModifiedPomXMLEventReader( input, inputFactory, path );
}
catch ( XMLStreamException e )
{
getLog().error( e );
}
return newPom;
}
/**
* Writes a StringBuilder into a file.
*
* @param outFile The file to read.
* @param input The contents of the file.
* @throws IOException when things go wrong.
*/
protected final void writeFile( File outFile, StringBuilder input )
throws IOException
{
Writer writer = WriterFactory.newXmlWriter( outFile );
try
{
IOUtil.copy( input.toString(), writer );
}
finally
{
IOUtil.close( writer );
}
}
/**
* Updates the pom.
*
* @param pom The pom to update.
* @throws MojoExecutionException If things go wrong.
* @throws MojoFailureException If things go wrong.
* @throws javax.xml.stream.XMLStreamException If things go wrong.
* @throws ArtifactMetadataRetrievalException if something goes wrong.
* @since 1.0-alpha-1
*/
protected abstract void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException, ArtifactMetadataRetrievalException;
/**
* Returns <code>true</code> if the update should be applied.
*
* @param artifact The artifact.
* @param currentVersion The current version of the artifact.
* @param updateVersion The proposed new version of the artifact.
* @return <code>true</code> if the update should be applied.
* @since 1.0-alpha-1
*/
protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, ArtifactVersion updateVersion )
{
getLog().debug( "Proposal is to update from " + currentVersion + " to " + updateVersion );
if ( updateVersion == null )
{
getLog().warn( "Not updating version: could not resolve any versions" );
return false;
}
artifact.setVersion( updateVersion.toString() );
try
{
resolver.resolveAlways( artifact, remoteArtifactRepositories, localRepository );
}
catch ( ArtifactResolutionException e )
{
getLog().warn( "Not updating version: could not resolve " + artifact, e );
return false;
}
catch ( ArtifactNotFoundException e )
{
getLog().warn( "Not updating version: could not find " + artifact, e );
return false;
}
if ( currentVersion.equals( updateVersion.toString() ) )
{
getLog().info( "Current version of " + artifact + " is the latest." );
return false;
}
return true;
}
/**
* Based on the passed flags, determines which segment is unchangable. This can be used when determining an upper
* bound for the "latest" version.
*
* @param allowMajorUpdates Allow major updates
* @param allowMinorUpdates Allow minor updates
* @param allowIncrementalUpdates Allow incremental updates
* @return Returns the segment that is unchangable. If any segment can change, returns -1.
*/
protected int determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates,
boolean allowIncrementalUpdates )
{
int segment;
if ( allowMajorUpdates )
{
segment = -1;
getLog().info( "Major version changes allowed" );
}
else if ( allowMinorUpdates )
{
segment = 0;
getLog().info( "Minor version changes allowed" );
}
else if ( allowIncrementalUpdates )
{
segment = 1;
getLog().info( "Incremental version changes allowed" );
}
else
{
segment = 2;
getLog().info( "Subincremental version changes allowed" );
}
return segment;
}
protected void updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
PropertyVersions version, String currentVersion )
throws MojoExecutionException, XMLStreamException
{
updatePropertyToNewestVersion( pom, property, version, currentVersion, false, -1 );
}
protected void updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property,
PropertyVersions version, String currentVersion,
boolean allowDowngrade, int segment )
throws MojoExecutionException, XMLStreamException
{
ArtifactVersion winner =
version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects,
this.getHelper(), allowDowngrade, segment );
if ( winner == null || currentVersion.equals( winner.toString() ) )
{
getLog().info( "Property ${" + property.getName() + "}: Leaving unchanged as " + currentVersion );
}
else if ( PomHelper.setPropertyVersion( pom, version.getProfileId(), property.getName(), winner.toString() ) )
{
getLog().info( "Updated ${" + property.getName() + "} from " + currentVersion + " to " + winner );
}
}
}