Skip to content

Commit e56c8c4

Browse files
Align update-parent and display-parent-update (#1017)
* Resolves #1016 - Align update-parent and display-parent-update * Introduce DisplayParentUpdatesMojoOutputTest * use SegmentUtils.determineUnchangedSegment * Align DisplayParentUpdatesMojoTest and UpdateParentMojoTest * Apply formatting rules * update javadoc since annotation to 2.17.0 for newly introduced properties * Align variable name between DisplayParentUpdatesMojo and UpdateParentMojo * Align skipResolution && isBlank(parentVersion) * Use Optional in favor of ternary operator
1 parent 1136bf4 commit e56c8c4

File tree

3 files changed

+547
-42
lines changed

3 files changed

+547
-42
lines changed

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java

+150-16
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,33 @@
2121

2222
import javax.inject.Inject;
2323

24+
import java.util.Arrays;
25+
import java.util.Collections;
2426
import java.util.Map;
27+
import java.util.Optional;
28+
import java.util.stream.Collectors;
2529

2630
import org.apache.maven.artifact.Artifact;
2731
import org.apache.maven.artifact.versioning.ArtifactVersion;
32+
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
33+
import org.apache.maven.artifact.versioning.VersionRange;
2834
import org.apache.maven.plugin.MojoExecutionException;
2935
import org.apache.maven.plugin.MojoFailureException;
3036
import org.apache.maven.plugins.annotations.Mojo;
37+
import org.apache.maven.plugins.annotations.Parameter;
3138
import org.apache.maven.repository.RepositorySystem;
3239
import org.apache.maven.wagon.Wagon;
40+
import org.codehaus.mojo.versions.api.ArtifactVersions;
41+
import org.codehaus.mojo.versions.api.Segment;
3342
import org.codehaus.mojo.versions.api.VersionRetrievalException;
3443
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
44+
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
3545
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
46+
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
3647
import org.codehaus.mojo.versions.utils.DependencyBuilder;
48+
import org.codehaus.mojo.versions.utils.SegmentUtils;
49+
50+
import static org.apache.maven.shared.utils.StringUtils.isBlank;
3751

3852
/**
3953
* Displays any updates of the project's parent project
@@ -46,6 +60,78 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo {
4660

4761
public static final int MESSAGE_LENGTH = 68;
4862

63+
// ------------------------------ FIELDS ------------------------------
64+
65+
/**
66+
* <p>If {@code skipResolution} is not set, specifies the <em>bottom</em> version considered
67+
* for target version resolution. If it is a version range, the resolved version will be
68+
* restricted by that range.</p>
69+
*
70+
* <p>If {@code skipResolution} is {@code true}, will specify the target version to which
71+
* the parent artifact will be updated.</p>
72+
* @since 2.17.0
73+
*/
74+
@Parameter(property = "parentVersion")
75+
protected String parentVersion = null;
76+
77+
/**
78+
* to update parent version by force when it is RELEASE or LATEST
79+
*
80+
* @since 2.17.0
81+
*/
82+
@Parameter(property = "forceUpdate", defaultValue = "false")
83+
protected boolean forceUpdate = false;
84+
85+
/**
86+
* Skips version resolution, only valid if {@code parentVersion} is set.
87+
* Will effectively set the new parent version to the one from {@code parentVersion}
88+
*
89+
* @since 2.17.0
90+
*/
91+
@Parameter(property = "skipResolution", defaultValue = "false")
92+
protected boolean skipResolution = false;
93+
94+
/**
95+
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
96+
* and there exists a version within the range fulfilling the criteria.</p>
97+
* <p>Default <code>false</code></p>
98+
*
99+
* @since 2.17.0
100+
*/
101+
@Parameter(property = "allowDowngrade", defaultValue = "false")
102+
protected boolean allowDowngrade;
103+
104+
/**
105+
* Whether to allow the major version number to be changed.
106+
*
107+
* @since 2.17.0
108+
*/
109+
@Parameter(property = "allowMajorUpdates", defaultValue = "true")
110+
protected boolean allowMajorUpdates = true;
111+
112+
/**
113+
* <p>Whether to allow the minor version number to be changed.</p>
114+
*
115+
* <p><b>Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}</b></p>
116+
*
117+
* @since 2.17.0
118+
*/
119+
@Parameter(property = "allowMinorUpdates", defaultValue = "true")
120+
protected boolean allowMinorUpdates = true;
121+
122+
/**
123+
* <p>Whether to allow the incremental version number to be changed.</p>
124+
*
125+
* <p><b>Note: {@code false} also implies {@linkplain #allowMajorUpdates}
126+
* and {@linkplain #allowMinorUpdates} {@code false}</b></p>
127+
*
128+
* @since 2.17.0
129+
*/
130+
@Parameter(property = "allowIncrementalUpdates", defaultValue = "true")
131+
protected boolean allowIncrementalUpdates = true;
132+
133+
// -------------------------- OTHER METHODS --------------------------
134+
49135
@Inject
50136
public DisplayParentUpdatesMojo(
51137
RepositorySystem repositorySystem,
@@ -68,36 +154,34 @@ public void execute() throws MojoExecutionException, MojoFailureException {
68154
return;
69155
}
70156

71-
String currentVersion = getProject().getParent().getVersion();
72-
Artifact artifact = getHelper()
73-
.createDependencyArtifact(DependencyBuilder.newBuilder()
74-
.withGroupId(getProject().getParent().getGroupId())
75-
.withArtifactId(getProject().getParent().getArtifactId())
76-
.withVersion(currentVersion)
77-
.withType("pom")
78-
.build());
79-
157+
if (skipResolution && isBlank(parentVersion)) {
158+
throw new MojoExecutionException("skipResolution is only valid if parentVersion is set");
159+
}
160+
String initialVersion = Optional.ofNullable(parentVersion)
161+
.orElse(getProject().getParent().getVersion());
80162
ArtifactVersion artifactVersion;
81163
try {
82-
artifactVersion = findLatestVersion(artifact, null, allowSnapshots, false);
83-
} catch (VersionRetrievalException e) {
164+
artifactVersion = skipResolution
165+
? DefaultArtifactVersionCache.of(parentVersion)
166+
: resolveTargetVersion(initialVersion);
167+
} catch (VersionRetrievalException | InvalidVersionSpecificationException | InvalidSegmentException e) {
84168
throw new MojoExecutionException(e.getMessage(), e);
85169
}
86170

87-
if (artifactVersion == null || currentVersion.equals(artifactVersion.toString())) {
171+
if (artifactVersion == null || initialVersion.equals(artifactVersion.toString())) {
88172
logLine(false, "The parent project is the latest version:");
89173
StringBuilder buf = new StringBuilder(MESSAGE_LENGTH);
90174
buf.append(" ");
91175
buf.append(getProject().getParent().getGroupId());
92176
buf.append(':');
93177
buf.append(getProject().getParent().getArtifactId());
94178
buf.append(' ');
95-
int padding = MESSAGE_LENGTH - currentVersion.length();
179+
int padding = MESSAGE_LENGTH - initialVersion.length();
96180
while (buf.length() < padding) {
97181
buf.append('.');
98182
}
99183
buf.append(' ');
100-
buf.append(currentVersion);
184+
buf.append(initialVersion);
101185
logLine(false, buf.toString());
102186
} else {
103187
logLine(false, "The parent project has a newer version:");
@@ -108,20 +192,70 @@ public void execute() throws MojoExecutionException, MojoFailureException {
108192
buf.append(getProject().getParent().getArtifactId());
109193
buf.append(' ');
110194
int padding = MESSAGE_LENGTH
111-
- currentVersion.length()
195+
- initialVersion.length()
112196
- artifactVersion.toString().length()
113197
- " -> ".length();
114198
while (buf.length() < padding) {
115199
buf.append('.');
116200
}
117201
buf.append(' ');
118-
buf.append(currentVersion);
202+
buf.append(initialVersion);
119203
buf.append(" -> ");
120204
buf.append(artifactVersion);
121205
logLine(false, buf.toString());
122206
}
123207
}
124208

209+
protected ArtifactVersion resolveTargetVersion(String initialVersion)
210+
throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException,
211+
InvalidSegmentException {
212+
Artifact artifact = getHelper()
213+
.createDependencyArtifact(DependencyBuilder.newBuilder()
214+
.withGroupId(getProject().getParent().getGroupId())
215+
.withArtifactId(getProject().getParent().getArtifactId())
216+
.withVersion(initialVersion)
217+
.withType("pom")
218+
.build());
219+
220+
VersionRange targetVersionRange = VersionRange.createFromVersionSpec(initialVersion);
221+
if (targetVersionRange.getRecommendedVersion() != null) {
222+
targetVersionRange = targetVersionRange.restrict(
223+
VersionRange.createFromVersionSpec("[" + targetVersionRange.getRecommendedVersion() + ",)"));
224+
}
225+
226+
final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false);
227+
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
228+
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
229+
230+
// currentVersion (set to parentVersion here) is not included in the version range for searching upgrades
231+
// unless we set allowDowngrade to true
232+
for (ArtifactVersion candidate : reverse(versions.getNewerVersions(
233+
initialVersion, unchangedSegment, allowSnapshots, !isBlank(parentVersion) || allowDowngrade))) {
234+
if (allowDowngrade
235+
|| targetVersionRange == null
236+
|| ArtifactVersions.isVersionInRange(candidate, targetVersionRange)) {
237+
if (shouldApplyUpdate(artifact, getProject().getParent().getVersion(), candidate, forceUpdate)) {
238+
return candidate;
239+
} else {
240+
getLog().debug("Update not applied. Exiting.");
241+
return null;
242+
}
243+
}
244+
}
245+
246+
if (versions.isEmpty(allowSnapshots)) {
247+
getLog().info("No versions found");
248+
} else {
249+
getLog().info("The parent project is the latest version");
250+
}
251+
252+
return null;
253+
}
254+
255+
private static <T> Iterable<T> reverse(T[] array) {
256+
return Arrays.stream(array).sorted(Collections.reverseOrder()).collect(Collectors.toList());
257+
}
258+
125259
@Override
126260
protected void update(ModifiedPomXMLEventReader pom) {}
127261
}

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java

+5-26
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.apache.maven.artifact.versioning.VersionRange;
3131
import org.apache.maven.plugin.MojoExecutionException;
3232
import org.apache.maven.plugin.MojoFailureException;
33-
import org.apache.maven.plugin.logging.Log;
3433
import org.apache.maven.plugins.annotations.Mojo;
3534
import org.apache.maven.plugins.annotations.Parameter;
3635
import org.apache.maven.repository.RepositorySystem;
@@ -46,11 +45,9 @@
4645
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
4746
import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache;
4847
import org.codehaus.mojo.versions.utils.DependencyBuilder;
48+
import org.codehaus.mojo.versions.utils.SegmentUtils;
4949

50-
import static java.util.Optional.empty;
51-
import static java.util.Optional.of;
5250
import static org.apache.maven.shared.utils.StringUtils.isBlank;
53-
import static org.codehaus.mojo.versions.api.Segment.*;
5451

5552
/**
5653
* Sets the parent version to the latest parent version.
@@ -166,7 +163,8 @@ protected void update(ModifiedPomXMLEventReader pom)
166163
throw new MojoExecutionException("skipResolution is only valid if parentVersion is set");
167164
}
168165

169-
String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion;
166+
String initialVersion = Optional.ofNullable(parentVersion)
167+
.orElse(getProject().getParent().getVersion());
170168
try {
171169
ArtifactVersion artifactVersion = skipResolution
172170
? DefaultArtifactVersionCache.of(parentVersion)
@@ -215,27 +213,8 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion)
215213
}
216214

217215
final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false);
218-
Log log = getLog();
219-
if (log != null && !allowIncrementalUpdates) {
220-
log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false.");
221-
}
222-
223-
if (log != null && !allowMinorUpdates) {
224-
log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false.");
225-
}
226-
227-
Optional<Segment> unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates
228-
? empty()
229-
: allowMinorUpdates && allowIncrementalUpdates
230-
? of(MAJOR)
231-
: allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL);
232-
if (log != null && log.isDebugEnabled()) {
233-
log.debug(unchangedSegment1
234-
.map(Segment::minorTo)
235-
.map(Segment::toString)
236-
.orElse("ALL") + " version changes allowed");
237-
}
238-
Optional<Segment> unchangedSegment = unchangedSegment1;
216+
Optional<Segment> unchangedSegment = SegmentUtils.determineUnchangedSegment(
217+
allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog());
239218

240219
// currentVersion (set to parentVersion here) is not included in the version range for searching upgrades
241220
// unless we set allowDowngrade to true

0 commit comments

Comments
 (0)