From 5b5f97aa73dd80d22e63bc32715112ea7af55b7d Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Mon, 23 Oct 2023 20:45:23 +0200 Subject: [PATCH 1/9] Resolves #1016 - Align update-parent and display-parent-update --- .../versions/DisplayParentUpdatesMojo.java | 177 ++++++- .../DisplayParentUpdatesMojoTest.java | 434 ++++++++++++++++++ 2 files changed, 599 insertions(+), 12 deletions(-) create mode 100644 versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index 78272957b3..039fbb9311 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -21,20 +21,39 @@ import javax.inject.Inject; +import java.util.Arrays; +import java.util.Collections; import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; import org.apache.maven.wagon.Wagon; +import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.api.Segment; import org.codehaus.mojo.versions.api.VersionRetrievalException; import org.codehaus.mojo.versions.api.recording.ChangeRecorder; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; +import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.apache.maven.shared.utils.StringUtils.isBlank; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; + /** * Displays any updates of the project's parent project * @@ -46,6 +65,78 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { public static final int MESSAGE_LENGTH = 68; + // ------------------------------ FIELDS ------------------------------ + + /** + *

If {@code skipResolution} is not set, specifies the bottom version considered + * for target version resolution. If it is a version range, the resolved version will be + * restricted by that range.

+ * + *

If {@code skipResolution} is {@code true}, will specify the target version to which + * the parent artifact will be updated.

+ * @since 2.16.1 + */ + @Parameter(property = "parentVersion") + protected String parentVersion = null; + + /** + * to update parent version by force when it is RELEASE or LATEST + * + * @since 2.16.1 + */ + @Parameter(property = "forceUpdate", defaultValue = "false") + protected boolean forceUpdate = false; + + /** + * Skips version resolution, only valid if {@code parentVersion} is set. + * Will effectively set the new parent version to the one from {@code parentVersion} + * + * @since 2.16.1 + */ + @Parameter(property = "skipResolution", defaultValue = "false") + protected boolean skipResolution = false; + + /** + *

Whether to downgrade a snapshot dependency if allowSnapshots is false + * and there exists a version within the range fulfilling the criteria.

+ *

Default false

+ * + * @since 2.16.1 + */ + @Parameter(property = "allowDowngrade", defaultValue = "false") + protected boolean allowDowngrade; + + /** + * Whether to allow the major version number to be changed. + * + * @since 2.16.1 + */ + @Parameter(property = "allowMajorUpdates", defaultValue = "true") + protected boolean allowMajorUpdates = true; + + /** + *

Whether to allow the minor version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

+ * + * @since 2.16.1 + */ + @Parameter(property = "allowMinorUpdates", defaultValue = "true") + protected boolean allowMinorUpdates = true; + + /** + *

Whether to allow the incremental version number to be changed.

+ * + *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} + * and {@linkplain #allowMinorUpdates} {@code false}

+ * + * @since 2.16.1 + */ + @Parameter(property = "allowIncrementalUpdates", defaultValue = "true") + protected boolean allowIncrementalUpdates = true; + + // -------------------------- OTHER METHODS -------------------------- + @Inject public DisplayParentUpdatesMojo( RepositorySystem repositorySystem, @@ -67,20 +158,13 @@ public void execute() throws MojoExecutionException, MojoFailureException { logLine(false, "Parent project is part of the reactor."); return; } - - String currentVersion = getProject().getParent().getVersion(); - Artifact artifact = getHelper() - .createDependencyArtifact(DependencyBuilder.newBuilder() - .withGroupId(getProject().getParent().getGroupId()) - .withArtifactId(getProject().getParent().getArtifactId()) - .withVersion(currentVersion) - .withType("pom") - .build()); - + String currentVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; ArtifactVersion artifactVersion; try { - artifactVersion = findLatestVersion(artifact, null, allowSnapshots, false); - } catch (VersionRetrievalException e) { + artifactVersion = skipResolution + ? DefaultArtifactVersionCache.of(parentVersion) + : resolveTargetVersion(currentVersion); + } catch (VersionRetrievalException | InvalidVersionSpecificationException | InvalidSegmentException e) { throw new MojoExecutionException(e.getMessage(), e); } @@ -122,6 +206,75 @@ public void execute() throws MojoExecutionException, MojoFailureException { } } + protected ArtifactVersion resolveTargetVersion(String initialVersion) + throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException, + InvalidSegmentException { + Artifact artifact = getHelper() + .createDependencyArtifact(DependencyBuilder.newBuilder() + .withGroupId(getProject().getParent().getGroupId()) + .withArtifactId(getProject().getParent().getArtifactId()) + .withVersion(initialVersion) + .withType("pom") + .build()); + + VersionRange targetVersionRange = VersionRange.createFromVersionSpec(initialVersion); + if (targetVersionRange.getRecommendedVersion() != null) { + targetVersionRange = targetVersionRange.restrict( + VersionRange.createFromVersionSpec("[" + targetVersionRange.getRecommendedVersion() + ",)")); + } + + final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); + Log log = getLog(); + if (log != null && !allowIncrementalUpdates) { + log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); + } + + if (log != null && !allowMinorUpdates) { + log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); + } + + Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates + ? empty() + : allowMinorUpdates && allowIncrementalUpdates + ? of(MAJOR) + : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); + if (log != null && log.isDebugEnabled()) { + log.debug(unchangedSegment1 + .map(Segment::minorTo) + .map(Segment::toString) + .orElse("ALL") + " version changes allowed"); + } + Optional unchangedSegment = unchangedSegment1; + + // currentVersion (set to parentVersion here) is not included in the version range for searching upgrades + // unless we set allowDowngrade to true + for (ArtifactVersion candidate : reverse(versions.getNewerVersions( + initialVersion, unchangedSegment, allowSnapshots, !isBlank(parentVersion) || allowDowngrade))) { + if (allowDowngrade + || targetVersionRange == null + || ArtifactVersions.isVersionInRange(candidate, targetVersionRange)) { + if (shouldApplyUpdate(artifact, getProject().getParent().getVersion(), candidate, forceUpdate)) { + return candidate; + } else { + getLog().debug("Update not applied. Exiting."); + return null; + } + } + } + + if (versions.isEmpty(allowSnapshots)) { + getLog().info("No versions found"); + } else { + getLog().info("The parent project is the latest version"); + } + + return null; + } + + private static Iterable reverse(T[] array) { + return Arrays.stream(array).sorted(Collections.reverseOrder()).collect(Collectors.toList()); + } + @Override protected void update(ModifiedPomXMLEventReader pom) {} } diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java new file mode 100644 index 0000000000..947e94e367 --- /dev/null +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java @@ -0,0 +1,434 @@ +package org.codehaus.mojo.versions; + +/* + * Copyright MojoHaus and Contributors + * Licensed 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 javax.xml.stream.XMLStreamException; + +import java.util.Collections; +import java.util.HashMap; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; +import org.apache.maven.artifact.versioning.VersionRange; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.Model; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; +import org.apache.maven.project.MavenProject; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.api.VersionRetrievalException; +import org.codehaus.mojo.versions.change.DefaultDependencyVersionChange; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; +import org.codehaus.mojo.versions.utils.TestChangeRecorder; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.MockedStatic; + +import static java.util.Collections.singleton; +import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; +import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject; +import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; +import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +public class DisplayParentUpdatesMojoTest { + private TestChangeRecorder changeRecorder; + + private DisplayParentUpdatesMojo mojo; + + private static RepositorySystem repositorySystem; + + private static org.eclipse.aether.RepositorySystem aetherRepositorySystem; + + @BeforeClass + public static void setUpStatic() { + repositorySystem = mockRepositorySystem(); + aetherRepositorySystem = mockAetherRepositorySystem(new HashMap() { + { + put("parent-artifact", new String[] {"0.9.0", "1.0.0", "1.0.1-SNAPSHOT"}); + put("issue-670-artifact", new String[] {"0.0.1-1", "0.0.1-1-impl-SNAPSHOT"}); + put("dummy-parent2", new String[] {"1.0", "2.0", "3.0", "3.0-alpha-1", "3.0-beta-1"}); + put("test-incremental", new String[] {"1.0.0", "1.1.0", "1.1.1", "2.0.0"}); + put("unknown-artifact", new String[0]); + } + }); + } + + @Before + public void setUp() throws IllegalAccessException { + changeRecorder = new TestChangeRecorder(); + mojo = + new DisplayParentUpdatesMojo( + repositorySystem, aetherRepositorySystem, null, changeRecorder.asTestMap()) { + { + setProject(createProject()); + reactorProjects = Collections.emptyList(); + session = mockMavenSession(); + } + }; + } + + private MavenProject createProject() { + return new MavenProject() { + { + setModel(new Model() { + { + setGroupId("default-group"); + setArtifactId("project-artifact"); + setVersion("1.0.1-SNAPSHOT"); + } + }); + + setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("parent-artifact"); + setVersion("1.0.1-SNAPSHOT"); + } + }); + } + }; + } + + private static RepositorySystem mockRepositorySystem() { + RepositorySystem repositorySystem = mock(RepositorySystem.class); + when(repositorySystem.createDependencyArtifact(any(Dependency.class))).thenAnswer(invocation -> { + Dependency dependency = invocation.getArgument(0); + return new DefaultArtifact( + dependency.getGroupId(), + dependency.getArtifactId(), + dependency.getVersion(), + dependency.getScope(), + dependency.getType(), + dependency.getClassifier() != null ? dependency.getClassifier() : "default", + new DefaultArtifactHandlerStub("default")); + }); + return repositorySystem; + } + + @Test + @SuppressWarnings("deprecation") + public void testArtifactIdDoesNotExist() + throws VersionRetrievalException, MojoExecutionException, XMLStreamException, MojoFailureException, + InvalidVersionSpecificationException, VersionRetrievalException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("unknown-artifact"); + setVersion("1.0.1-SNAPSHOT"); + } + }); + + Artifact artifact = new DefaultArtifact( + "default-group", + "unknown-artifact", + "1.0.1-SNAPSHOT", + SCOPE_COMPILE, + "pom", + "default", + new DefaultArtifactHandlerStub("default")); + assertThat( + mojo.findLatestVersion(artifact, VersionRange.createFromVersionSpec("1.0.1-SNAPSHOT"), null, false), + is(nullValue())); + + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + } + + @Test + @Ignore + public void testParentDowngradeAllowed() + throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + mojo.allowDowngrade = true; + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultDependencyVersionChange( + "default-group", "parent-artifact", "1.0.1-SNAPSHOT", "1.0.0"))); + } + + @Test + public void testParentDowngradeForbidden() + throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + mojo.allowDowngrade = false; + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + assertThat(changeRecorder.getChanges(), is(empty())); + } + + @Test + public void testParentDowngradeAllowedWithRange() + throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException, + InvalidSegmentException { + mojo.allowDowngrade = true; + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("parent-artifact"); + } + }); + + ArtifactVersion newVersion = mojo.resolveTargetVersion("[1.0.1-SNAPSHOT,)"); + assertThat(newVersion, notNullValue()); + assertThat(newVersion.toString(), is("1.0.0")); + } + + @Test + public void testParentDowngradeForbiddenWithRange() + throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException, + InvalidSegmentException { + mojo.allowDowngrade = false; + ArtifactVersion newVersion = mojo.resolveTargetVersion("[1.0.1-SNAPSHOT,)"); + assertThat(newVersion, nullValue()); + } + + @Test + public void testAllowSnapshots() + throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException, + InvalidSegmentException { + mojo.allowSnapshots = true; + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("issue-670-artifact"); + } + }); + + ArtifactVersion newVersion = mojo.resolveTargetVersion("0.0.1-1"); + assertThat(newVersion, notNullValue()); + assertThat(newVersion.toString(), is("0.0.1-1-impl-SNAPSHOT")); + } + + @Test + @Ignore + public void testAllowSnapshotsWithParentVersion() + throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + mojo.allowSnapshots = true; + mojo.parentVersion = "0.0.1-1-impl-SNAPSHOT"; + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("issue-670-artifact"); + setVersion("0.0.1-1"); + } + }); + + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultDependencyVersionChange( + "default-group", "issue-670-artifact", "0.0.1-1", "0.0.1-1-impl-SNAPSHOT"))); + } + + @Test + public void testIgnoredVersions() + throws MojoExecutionException, IllegalAccessException, VersionRetrievalException, + InvalidVersionSpecificationException, InvalidSegmentException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("parent-artifact"); + } + }); + setVariableValueToObject(mojo, "ignoredVersions", singleton("1.0.0")); + assertThat(mojo.resolveTargetVersion("0.9.0"), nullValue()); + } + + @Test + @Ignore + public void testSkipResolutionDowngradeUnknownVersion() throws VersionRetrievalException { + testSkipResolution("0.8.0"); + } + + @Test + @Ignore + public void testSkipResolutionDowngrade() throws VersionRetrievalException { + testSkipResolution("0.9.0"); + } + + @Test + @Ignore + public void testSkipResolutionUpgradeUnknownVersion() throws VersionRetrievalException { + testSkipResolution("2.0.0"); + } + + private void testSkipResolution(String version) throws VersionRetrievalException { + mojo.parentVersion = version; + mojo.skipResolution = true; + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("parent-artifact"); + setVersion("1.0.0"); + } + }); + + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultDependencyVersionChange("default-group", "parent-artifact", "1.0.0", version))); + } + + @Test + @Ignore + public void testShouldUpgradeToSnapshot() + throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("parent-artifact"); + setVersion("0.9.0"); + } + }); + mojo.allowSnapshots = true; + mojo.parentVersion = "[0,1.0.1-SNAPSHOT]"; + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultDependencyVersionChange( + "default-group", "parent-artifact", "0.9.0", "1.0.1-SNAPSHOT"))); + } + + @Test + public void testAllowMinorUpdates() + throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException, + InvalidSegmentException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("parent-artifact"); + setVersion("0.8.0"); + } + }); + mojo.allowMajorUpdates = false; + mojo.allowMinorUpdates = true; + mojo.allowIncrementalUpdates = true; + + ArtifactVersion newVersion = mojo.resolveTargetVersion("0.8.0"); + + assertThat(newVersion, notNullValue()); + assertThat(newVersion.toString(), is("0.9.0")); + } + + @Test + public void testAllowIncrementalUpdates() + throws MojoExecutionException, VersionRetrievalException, InvalidVersionSpecificationException, + InvalidSegmentException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("test-incremental"); + } + }); + mojo.allowMajorUpdates = false; + mojo.allowMinorUpdates = false; + mojo.allowIncrementalUpdates = true; + + ArtifactVersion newVersion = mojo.resolveTargetVersion("1.1.0"); + + assertThat(newVersion, notNullValue()); + assertThat(newVersion.toString(), is("1.1.1")); + } + + @Test + @Ignore + public void testParentVersionRange() + throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("dummy-parent2"); + setVersion("1.0"); + } + }); + mojo.allowSnapshots = true; + mojo.parentVersion = "[,3.0-!)"; + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + assertThat( + changeRecorder.getChanges(), + hasItem(new DefaultDependencyVersionChange("default-group", "dummy-parent2", "1.0", "2.0"))); + } + + @Test + public void testParentVersionRange2() + throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + mojo.getProject().setParent(new MavenProject() { + { + setGroupId("default-group"); + setArtifactId("dummy-parent2"); + setVersion("2.0"); + } + }); + mojo.allowSnapshots = true; + mojo.parentVersion = "[,3.0-!)"; + try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { + pomHelper + .when(() -> PomHelper.setProjectParentVersion(any(), any())) + .thenReturn(true); + mojo.update(null); + } + assertThat(changeRecorder.getChanges(), empty()); + } +} From fa38387868a82418c8391b96822db3d67235c117 Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Tue, 24 Oct 2023 09:51:11 +0200 Subject: [PATCH 2/9] Introduce DisplayParentUpdatesMojoOutputTest --- .../DisplayParentUpdatesMojoOutputTest.java | 92 +++++++++++++++++++ .../display-parent-updates/no-parent/pom.xml | 9 ++ .../display-parent-updates/parent/pom.xml | 14 +++ 3 files changed, 115 insertions(+) create mode 100644 versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java new file mode 100644 index 0000000000..03bb5794e2 --- /dev/null +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java @@ -0,0 +1,92 @@ +package org.codehaus.mojo.versions; +/* + * Copyright MojoHaus and Contributors + * Licensed 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 + * + * https://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 java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; + +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.MojoRule; +import org.codehaus.mojo.versions.utils.TestUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import static org.apache.commons.codec.CharEncoding.UTF_8; +import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.matchesPattern; + +/** + * Unit tests for {@link DisplayPropertyUpdatesMojo} + */ +public class DisplayParentUpdatesMojoOutputTest extends AbstractMojoTestCase { + @Rule + public MojoRule mojoRule = new MojoRule(this); + + private Path tempDir; + + private Path tempFile; + + @Before + public void setUp() throws Exception { + super.setUp(); + tempDir = TestUtils.createTempDir("display-parent-updates"); + tempFile = Files.createTempFile(tempDir, "output", ""); + } + + @After + public void tearDown() throws Exception { + try { + TestUtils.tearDownTempDir(tempDir); + } finally { + super.tearDown(); + } + } + + @Test + public void testPluginLoads() throws Exception { + TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/display-parent-updates"), tempDir); + DisplayParentUpdatesMojo mojo = (DisplayParentUpdatesMojo) + mojoRule.lookupConfiguredMojo(tempDir.resolve("no-parent").toFile(), "display-parent-updates"); + mojo.outputEncoding = UTF_8; + mojo.outputFile = tempFile.toFile(); + mojo.setPluginContext(new HashMap<>()); + mojo.aetherRepositorySystem = mockAetherRepositorySystem(); + // mojo.includeParent = true; + mojo.execute(); + + assertThat(String.join("", Files.readAllLines(tempFile)), matchesPattern("Project does not have a parent\\.")); + } + + @Test + public void testDisplayParentUpdate() throws Exception { + TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/display-parent-updates"), tempDir); + DisplayParentUpdatesMojo mojo = (DisplayParentUpdatesMojo) + mojoRule.lookupConfiguredMojo(tempDir.resolve("parent").toFile(), "display-parent-updates"); + mojo.outputEncoding = UTF_8; + mojo.outputFile = tempFile.toFile(); + mojo.setPluginContext(new HashMap<>()); + mojo.aetherRepositorySystem = mockAetherRepositorySystem(); + // mojo.includeParent = true; + mojo.execute(); + + // assertThat(String.join("", Files.readAllLines(tempFile)), matchesPattern("Project does not have a + // parent\\.")); + } +} diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml new file mode 100644 index 0000000000..a06ca68136 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml @@ -0,0 +1,9 @@ + + + 4.0.0 + + display-parent-updates + project-has-no-parent + 0.1.0-SNAPSHOT + \ No newline at end of file diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml new file mode 100644 index 0000000000..05e5d316e7 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + + + default-group + artifactA + 1.0.0 + + + display-parent-updates-test + 0.1.0-SNAPSHOT + \ No newline at end of file From fc7a94ef8d478054419a6aac4e59e6a3a548a6f1 Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Sun, 12 Nov 2023 15:58:57 +0100 Subject: [PATCH 3/9] use SegmentUtils.determineUnchangedSegment --- .../versions/DisplayParentUpdatesMojo.java | 35 +++---------------- .../mojo/versions/UpdateParentMojo.java | 35 +++---------------- 2 files changed, 9 insertions(+), 61 deletions(-) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index 039fbb9311..df4a586459 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -19,21 +19,20 @@ * under the License. */ -import javax.inject.Inject; +import static org.apache.maven.shared.utils.StringUtils.isBlank; import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; - +import javax.inject.Inject; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -46,13 +45,7 @@ import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; - -import static java.util.Optional.empty; -import static java.util.Optional.of; -import static org.apache.maven.shared.utils.StringUtils.isBlank; -import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; -import static org.codehaus.mojo.versions.api.Segment.MINOR; +import org.codehaus.mojo.versions.utils.SegmentUtils; /** * Displays any updates of the project's parent project @@ -224,27 +217,7 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion) } final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); - Log log = getLog(); - if (log != null && !allowIncrementalUpdates) { - log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); - } - - if (log != null && !allowMinorUpdates) { - log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); - } - - Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates - ? empty() - : allowMinorUpdates && allowIncrementalUpdates - ? of(MAJOR) - : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); - if (log != null && log.isDebugEnabled()) { - log.debug(unchangedSegment1 - .map(Segment::minorTo) - .map(Segment::toString) - .orElse("ALL") + " version changes allowed"); - } - Optional unchangedSegment = unchangedSegment1; + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment(allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); // currentVersion (set to parentVersion here) is not included in the version range for searching upgrades // unless we set allowDowngrade to true diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index 882fd62713..f1c5c5ccb6 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -15,22 +15,21 @@ * limitations under the License. */ -import javax.inject.Inject; -import javax.xml.stream.XMLStreamException; +import static org.apache.maven.shared.utils.StringUtils.isBlank; import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; - +import javax.inject.Inject; +import javax.xml.stream.XMLStreamException; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.repository.RepositorySystem; @@ -46,11 +45,7 @@ import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DefaultArtifactVersionCache; import org.codehaus.mojo.versions.utils.DependencyBuilder; - -import static java.util.Optional.empty; -import static java.util.Optional.of; -import static org.apache.maven.shared.utils.StringUtils.isBlank; -import static org.codehaus.mojo.versions.api.Segment.*; +import org.codehaus.mojo.versions.utils.SegmentUtils; /** * Sets the parent version to the latest parent version. @@ -215,27 +210,7 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion) } final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); - Log log = getLog(); - if (log != null && !allowIncrementalUpdates) { - log.info("Assuming allowMinorUpdates false because allowIncrementalUpdates is false."); - } - - if (log != null && !allowMinorUpdates) { - log.info("Assuming allowMajorUpdates false because allowMinorUpdates is false."); - } - - Optional unchangedSegment1 = allowMajorUpdates && allowMinorUpdates && allowIncrementalUpdates - ? empty() - : allowMinorUpdates && allowIncrementalUpdates - ? of(MAJOR) - : allowIncrementalUpdates ? of(MINOR) : of(INCREMENTAL); - if (log != null && log.isDebugEnabled()) { - log.debug(unchangedSegment1 - .map(Segment::minorTo) - .map(Segment::toString) - .orElse("ALL") + " version changes allowed"); - } - Optional unchangedSegment = unchangedSegment1; + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment(allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); // currentVersion (set to parentVersion here) is not included in the version range for searching upgrades // unless we set allowDowngrade to true From 27d5710ebefb83d06aab9e9842509ec0464f42b1 Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Sun, 12 Nov 2023 17:21:05 +0100 Subject: [PATCH 4/9] Align DisplayParentUpdatesMojoTest and UpdateParentMojoTest --- .../DisplayParentUpdatesMojoOutputTest.java | 92 --------- .../DisplayParentUpdatesMojoTest.java | 174 +++++++----------- .../display-parent-updates/no-parent/pom.xml | 9 - .../display-parent-updates/parent/pom.xml | 14 -- 4 files changed, 67 insertions(+), 222 deletions(-) delete mode 100644 versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java delete mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml delete mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java deleted file mode 100644 index 03bb5794e2..0000000000 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoOutputTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.codehaus.mojo.versions; -/* - * Copyright MojoHaus and Contributors - * Licensed 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 - * - * https://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 java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; - -import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.apache.maven.plugin.testing.MojoRule; -import org.codehaus.mojo.versions.utils.TestUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; - -import static org.apache.commons.codec.CharEncoding.UTF_8; -import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.matchesPattern; - -/** - * Unit tests for {@link DisplayPropertyUpdatesMojo} - */ -public class DisplayParentUpdatesMojoOutputTest extends AbstractMojoTestCase { - @Rule - public MojoRule mojoRule = new MojoRule(this); - - private Path tempDir; - - private Path tempFile; - - @Before - public void setUp() throws Exception { - super.setUp(); - tempDir = TestUtils.createTempDir("display-parent-updates"); - tempFile = Files.createTempFile(tempDir, "output", ""); - } - - @After - public void tearDown() throws Exception { - try { - TestUtils.tearDownTempDir(tempDir); - } finally { - super.tearDown(); - } - } - - @Test - public void testPluginLoads() throws Exception { - TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/display-parent-updates"), tempDir); - DisplayParentUpdatesMojo mojo = (DisplayParentUpdatesMojo) - mojoRule.lookupConfiguredMojo(tempDir.resolve("no-parent").toFile(), "display-parent-updates"); - mojo.outputEncoding = UTF_8; - mojo.outputFile = tempFile.toFile(); - mojo.setPluginContext(new HashMap<>()); - mojo.aetherRepositorySystem = mockAetherRepositorySystem(); - // mojo.includeParent = true; - mojo.execute(); - - assertThat(String.join("", Files.readAllLines(tempFile)), matchesPattern("Project does not have a parent\\.")); - } - - @Test - public void testDisplayParentUpdate() throws Exception { - TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/display-parent-updates"), tempDir); - DisplayParentUpdatesMojo mojo = (DisplayParentUpdatesMojo) - mojoRule.lookupConfiguredMojo(tempDir.resolve("parent").toFile(), "display-parent-updates"); - mojo.outputEncoding = UTF_8; - mojo.outputFile = tempFile.toFile(); - mojo.setPluginContext(new HashMap<>()); - mojo.aetherRepositorySystem = mockAetherRepositorySystem(); - // mojo.includeParent = true; - mojo.execute(); - - // assertThat(String.join("", Files.readAllLines(tempFile)), matchesPattern("Project does not have a - // parent\\.")); - } -} diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java index 947e94e367..5c08d5d418 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java @@ -15,6 +15,9 @@ * limitations under the License. */ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import javax.xml.stream.XMLStreamException; import java.util.Collections; @@ -32,16 +35,13 @@ import org.apache.maven.plugin.testing.stubs.DefaultArtifactHandlerStub; import org.apache.maven.project.MavenProject; import org.apache.maven.repository.RepositorySystem; -import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.VersionRetrievalException; -import org.codehaus.mojo.versions.change.DefaultDependencyVersionChange; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; -import org.codehaus.mojo.versions.utils.TestChangeRecorder; +import org.codehaus.mojo.versions.utils.TestUtils; +import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; -import org.mockito.MockedStatic; import static java.util.Collections.singleton; import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; @@ -49,25 +49,25 @@ import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem; import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.stringContainsInOrder; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; public class DisplayParentUpdatesMojoTest { - private TestChangeRecorder changeRecorder; - private DisplayParentUpdatesMojo mojo; private static RepositorySystem repositorySystem; private static org.eclipse.aether.RepositorySystem aetherRepositorySystem; + private Path tempDir; + + private Path tempFile; + @BeforeClass public static void setUpStatic() { repositorySystem = mockRepositorySystem(); @@ -83,17 +83,25 @@ public static void setUpStatic() { } @Before - public void setUp() throws IllegalAccessException { - changeRecorder = new TestChangeRecorder(); + public void setUp() throws IllegalAccessException, IOException { + tempDir = TestUtils.createTempDir("display-property-updates"); + tempFile = Files.createTempFile(tempDir, "output", ""); mojo = new DisplayParentUpdatesMojo( - repositorySystem, aetherRepositorySystem, null, changeRecorder.asTestMap()) { + repositorySystem, aetherRepositorySystem, null, null) { { setProject(createProject()); reactorProjects = Collections.emptyList(); session = mockMavenSession(); } }; + mojo.outputFile = tempFile.toFile(); + mojo.setPluginContext(new HashMap<>()); + } + + @After + public void tearDown() throws Exception { + TestUtils.tearDownTempDir(tempDir); } private MavenProject createProject() { @@ -135,10 +143,8 @@ private static RepositorySystem mockRepositorySystem() { } @Test - @SuppressWarnings("deprecation") public void testArtifactIdDoesNotExist() - throws VersionRetrievalException, MojoExecutionException, XMLStreamException, MojoFailureException, - InvalidVersionSpecificationException, VersionRetrievalException { + throws MojoExecutionException, InvalidVersionSpecificationException, VersionRetrievalException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -158,43 +164,28 @@ public void testArtifactIdDoesNotExist() assertThat( mojo.findLatestVersion(artifact, VersionRange.createFromVersionSpec("1.0.1-SNAPSHOT"), null, false), is(nullValue())); - - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } } @Test - @Ignore public void testParentDowngradeAllowed() - throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + throws MojoExecutionException, MojoFailureException, IOException { mojo.allowDowngrade = true; - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } + mojo.execute(); assertThat( - changeRecorder.getChanges(), - hasItem(new DefaultDependencyVersionChange( - "default-group", "parent-artifact", "1.0.1-SNAPSHOT", "1.0.0"))); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project has a newer version:", "default-group:parent-artifact", + "1.0.1-SNAPSHOT -> 1.0.0")); } @Test public void testParentDowngradeForbidden() - throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + throws MojoExecutionException, MojoFailureException, IOException { mojo.allowDowngrade = false; - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } - assertThat(changeRecorder.getChanges(), is(empty())); + mojo.execute(); + assertThat( + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project is the latest version:", "default-group:parent-artifact", + "1.0.1-SNAPSHOT")); } @Test @@ -241,29 +232,16 @@ public void testAllowSnapshots() } @Test - @Ignore public void testAllowSnapshotsWithParentVersion() - throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + throws MojoExecutionException, MojoFailureException, IOException { mojo.allowSnapshots = true; mojo.parentVersion = "0.0.1-1-impl-SNAPSHOT"; - mojo.getProject().setParent(new MavenProject() { - { - setGroupId("default-group"); - setArtifactId("issue-670-artifact"); - setVersion("0.0.1-1"); - } - }); - - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } + mojo.execute(); assertThat( - changeRecorder.getChanges(), - hasItem(new DefaultDependencyVersionChange( - "default-group", "issue-670-artifact", "0.0.1-1", "0.0.1-1-impl-SNAPSHOT"))); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project is the latest version:", "default-group:parent-artifact", + "0.0.1-1-impl-SNAPSHOT")); + } @Test @@ -281,24 +259,21 @@ public void testIgnoredVersions() } @Test - @Ignore - public void testSkipResolutionDowngradeUnknownVersion() throws VersionRetrievalException { + public void testSkipResolutionDowngradeUnknownVersion() throws MojoExecutionException, MojoFailureException, IOException { testSkipResolution("0.8.0"); } @Test - @Ignore - public void testSkipResolutionDowngrade() throws VersionRetrievalException { + public void testSkipResolutionDowngrade() throws MojoExecutionException, MojoFailureException, IOException { testSkipResolution("0.9.0"); } @Test - @Ignore - public void testSkipResolutionUpgradeUnknownVersion() throws VersionRetrievalException { + public void testSkipResolutionUpgradeUnknownVersion() throws MojoExecutionException, MojoFailureException, IOException { testSkipResolution("2.0.0"); } - private void testSkipResolution(String version) throws VersionRetrievalException { + private void testSkipResolution(String version) throws IOException, MojoExecutionException, MojoFailureException { mojo.parentVersion = version; mojo.skipResolution = true; mojo.getProject().setParent(new MavenProject() { @@ -308,23 +283,16 @@ private void testSkipResolution(String version) throws VersionRetrievalException setVersion("1.0.0"); } }); - - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } - + mojo.execute(); assertThat( - changeRecorder.getChanges(), - hasItem(new DefaultDependencyVersionChange("default-group", "parent-artifact", "1.0.0", version))); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project is the latest version:", "default-group:parent-artifact", + version)); } @Test - @Ignore public void testShouldUpgradeToSnapshot() - throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + throws MojoExecutionException, MojoFailureException, IOException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -334,16 +302,12 @@ public void testShouldUpgradeToSnapshot() }); mojo.allowSnapshots = true; mojo.parentVersion = "[0,1.0.1-SNAPSHOT]"; - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } + mojo.execute(); assertThat( - changeRecorder.getChanges(), - hasItem(new DefaultDependencyVersionChange( - "default-group", "parent-artifact", "0.9.0", "1.0.1-SNAPSHOT"))); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project has a newer version:", "default-group:parent-artifact", + "[0,1.0.1-SNAPSHOT]", + "1.0.1-SNAPSHOT")); } @Test @@ -388,9 +352,8 @@ public void testAllowIncrementalUpdates() } @Test - @Ignore public void testParentVersionRange() - throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + throws MojoExecutionException, MojoFailureException, IOException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -400,20 +363,18 @@ public void testParentVersionRange() }); mojo.allowSnapshots = true; mojo.parentVersion = "[,3.0-!)"; - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } + mojo.execute(); + assertThat( - changeRecorder.getChanges(), - hasItem(new DefaultDependencyVersionChange("default-group", "dummy-parent2", "1.0", "2.0"))); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project has a newer version:", "dummy-parent2", + "[,3.0-!)", + "2.0")); } @Test public void testParentVersionRange2() - throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException { + throws MojoExecutionException, XMLStreamException, MojoFailureException, IOException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -423,12 +384,11 @@ public void testParentVersionRange2() }); mojo.allowSnapshots = true; mojo.parentVersion = "[,3.0-!)"; - try (MockedStatic pomHelper = mockStatic(PomHelper.class)) { - pomHelper - .when(() -> PomHelper.setProjectParentVersion(any(), any())) - .thenReturn(true); - mojo.update(null); - } - assertThat(changeRecorder.getChanges(), empty()); + mojo.execute(); + assertThat( + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project is the latest version:", "dummy-parent2", + "[,3.0-!)")); + } } diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml deleted file mode 100644 index a06ca68136..0000000000 --- a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/no-parent/pom.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - 4.0.0 - - display-parent-updates - project-has-no-parent - 0.1.0-SNAPSHOT - \ No newline at end of file diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml deleted file mode 100644 index 05e5d316e7..0000000000 --- a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/display-parent-updates/parent/pom.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - 4.0.0 - - - default-group - artifactA - 1.0.0 - - - display-parent-updates-test - 0.1.0-SNAPSHOT - \ No newline at end of file From b2859a346caa9e324a12044e2a5221b761f6151a Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Sun, 12 Nov 2023 17:29:38 +0100 Subject: [PATCH 5/9] Apply formatting rules --- .../versions/DisplayParentUpdatesMojo.java | 9 +- .../mojo/versions/UpdateParentMojo.java | 11 ++- .../DisplayParentUpdatesMojoTest.java | 98 +++++++++---------- 3 files changed, 61 insertions(+), 57 deletions(-) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index df4a586459..ab4959fbe2 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -19,14 +19,14 @@ * under the License. */ -import static org.apache.maven.shared.utils.StringUtils.isBlank; +import javax.inject.Inject; import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; -import javax.inject.Inject; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; @@ -47,6 +47,8 @@ import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.SegmentUtils; +import static org.apache.maven.shared.utils.StringUtils.isBlank; + /** * Displays any updates of the project's parent project * @@ -217,7 +219,8 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion) } final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment(allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( + allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); // currentVersion (set to parentVersion here) is not included in the version range for searching upgrades // unless we set allowDowngrade to true diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index f1c5c5ccb6..c9ba097102 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -15,15 +15,15 @@ * limitations under the License. */ -import static org.apache.maven.shared.utils.StringUtils.isBlank; +import javax.inject.Inject; +import javax.xml.stream.XMLStreamException; import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; -import javax.inject.Inject; -import javax.xml.stream.XMLStreamException; + import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; @@ -47,6 +47,8 @@ import org.codehaus.mojo.versions.utils.DependencyBuilder; import org.codehaus.mojo.versions.utils.SegmentUtils; +import static org.apache.maven.shared.utils.StringUtils.isBlank; + /** * Sets the parent version to the latest parent version. * @@ -210,7 +212,8 @@ protected ArtifactVersion resolveTargetVersion(String initialVersion) } final ArtifactVersions versions = getHelper().lookupArtifactVersions(artifact, false); - Optional unchangedSegment = SegmentUtils.determineUnchangedSegment(allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); + Optional unchangedSegment = SegmentUtils.determineUnchangedSegment( + allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates, getLog()); // currentVersion (set to parentVersion here) is not included in the version range for searching upgrades // unless we set allowDowngrade to true diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java index 5c08d5d418..c3cc4b3fd3 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojoTest.java @@ -15,11 +15,11 @@ * limitations under the License. */ +import javax.xml.stream.XMLStreamException; + import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import javax.xml.stream.XMLStreamException; - import java.util.Collections; import java.util.HashMap; @@ -86,15 +86,13 @@ public static void setUpStatic() { public void setUp() throws IllegalAccessException, IOException { tempDir = TestUtils.createTempDir("display-property-updates"); tempFile = Files.createTempFile(tempDir, "output", ""); - mojo = - new DisplayParentUpdatesMojo( - repositorySystem, aetherRepositorySystem, null, null) { - { - setProject(createProject()); - reactorProjects = Collections.emptyList(); - session = mockMavenSession(); - } - }; + mojo = new DisplayParentUpdatesMojo(repositorySystem, aetherRepositorySystem, null, null) { + { + setProject(createProject()); + reactorProjects = Collections.emptyList(); + session = mockMavenSession(); + } + }; mojo.outputFile = tempFile.toFile(); mojo.setPluginContext(new HashMap<>()); } @@ -144,7 +142,7 @@ private static RepositorySystem mockRepositorySystem() { @Test public void testArtifactIdDoesNotExist() - throws MojoExecutionException, InvalidVersionSpecificationException, VersionRetrievalException { + throws MojoExecutionException, InvalidVersionSpecificationException, VersionRetrievalException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -167,25 +165,27 @@ public void testArtifactIdDoesNotExist() } @Test - public void testParentDowngradeAllowed() - throws MojoExecutionException, MojoFailureException, IOException { + public void testParentDowngradeAllowed() throws MojoExecutionException, MojoFailureException, IOException { mojo.allowDowngrade = true; mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project has a newer version:", "default-group:parent-artifact", - "1.0.1-SNAPSHOT -> 1.0.0")); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder( + "The parent project has a newer version:", + "default-group:parent-artifact", + "1.0.1-SNAPSHOT -> 1.0.0")); } @Test - public void testParentDowngradeForbidden() - throws MojoExecutionException, MojoFailureException, IOException { + public void testParentDowngradeForbidden() throws MojoExecutionException, MojoFailureException, IOException { mojo.allowDowngrade = false; mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project is the latest version:", "default-group:parent-artifact", - "1.0.1-SNAPSHOT")); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder( + "The parent project is the latest version:", + "default-group:parent-artifact", + "1.0.1-SNAPSHOT")); } @Test @@ -232,16 +232,16 @@ public void testAllowSnapshots() } @Test - public void testAllowSnapshotsWithParentVersion() - throws MojoExecutionException, MojoFailureException, IOException { + public void testAllowSnapshotsWithParentVersion() throws MojoExecutionException, MojoFailureException, IOException { mojo.allowSnapshots = true; mojo.parentVersion = "0.0.1-1-impl-SNAPSHOT"; mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project is the latest version:", "default-group:parent-artifact", - "0.0.1-1-impl-SNAPSHOT")); - + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder( + "The parent project is the latest version:", + "default-group:parent-artifact", + "0.0.1-1-impl-SNAPSHOT")); } @Test @@ -259,7 +259,8 @@ public void testIgnoredVersions() } @Test - public void testSkipResolutionDowngradeUnknownVersion() throws MojoExecutionException, MojoFailureException, IOException { + public void testSkipResolutionDowngradeUnknownVersion() + throws MojoExecutionException, MojoFailureException, IOException { testSkipResolution("0.8.0"); } @@ -269,7 +270,8 @@ public void testSkipResolutionDowngrade() throws MojoExecutionException, MojoFai } @Test - public void testSkipResolutionUpgradeUnknownVersion() throws MojoExecutionException, MojoFailureException, IOException { + public void testSkipResolutionUpgradeUnknownVersion() + throws MojoExecutionException, MojoFailureException, IOException { testSkipResolution("2.0.0"); } @@ -285,14 +287,13 @@ private void testSkipResolution(String version) throws IOException, MojoExecutio }); mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project is the latest version:", "default-group:parent-artifact", - version)); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder( + "The parent project is the latest version:", "default-group:parent-artifact", version)); } @Test - public void testShouldUpgradeToSnapshot() - throws MojoExecutionException, MojoFailureException, IOException { + public void testShouldUpgradeToSnapshot() throws MojoExecutionException, MojoFailureException, IOException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -304,10 +305,12 @@ public void testShouldUpgradeToSnapshot() mojo.parentVersion = "[0,1.0.1-SNAPSHOT]"; mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project has a newer version:", "default-group:parent-artifact", - "[0,1.0.1-SNAPSHOT]", - "1.0.1-SNAPSHOT")); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder( + "The parent project has a newer version:", + "default-group:parent-artifact", + "[0,1.0.1-SNAPSHOT]", + "1.0.1-SNAPSHOT")); } @Test @@ -352,8 +355,7 @@ public void testAllowIncrementalUpdates() } @Test - public void testParentVersionRange() - throws MojoExecutionException, MojoFailureException, IOException { + public void testParentVersionRange() throws MojoExecutionException, MojoFailureException, IOException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -366,15 +368,13 @@ public void testParentVersionRange() mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project has a newer version:", "dummy-parent2", - "[,3.0-!)", - "2.0")); + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project has a newer version:", "dummy-parent2", "[,3.0-!)", "2.0")); } @Test public void testParentVersionRange2() - throws MojoExecutionException, XMLStreamException, MojoFailureException, IOException { + throws MojoExecutionException, XMLStreamException, MojoFailureException, IOException { mojo.getProject().setParent(new MavenProject() { { setGroupId("default-group"); @@ -386,9 +386,7 @@ public void testParentVersionRange2() mojo.parentVersion = "[,3.0-!)"; mojo.execute(); assertThat( - String.join("", Files.readAllLines(tempFile)), - stringContainsInOrder("The parent project is the latest version:", "dummy-parent2", - "[,3.0-!)")); - + String.join("", Files.readAllLines(tempFile)), + stringContainsInOrder("The parent project is the latest version:", "dummy-parent2", "[,3.0-!)")); } } From 4352512065e11def2ece6042bf91805c32f890e0 Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Sun, 12 Nov 2023 20:23:42 +0100 Subject: [PATCH 6/9] update javadoc since annotation to 2.17.0 for newly introduced properties --- .../mojo/versions/DisplayParentUpdatesMojo.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index ab4959fbe2..4921c1c1e7 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -69,7 +69,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { * *

If {@code skipResolution} is {@code true}, will specify the target version to which * the parent artifact will be updated.

- * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "parentVersion") protected String parentVersion = null; @@ -77,7 +77,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { /** * to update parent version by force when it is RELEASE or LATEST * - * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "forceUpdate", defaultValue = "false") protected boolean forceUpdate = false; @@ -86,7 +86,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { * Skips version resolution, only valid if {@code parentVersion} is set. * Will effectively set the new parent version to the one from {@code parentVersion} * - * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "skipResolution", defaultValue = "false") protected boolean skipResolution = false; @@ -96,7 +96,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { * and there exists a version within the range fulfilling the criteria.

*

Default false

* - * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "allowDowngrade", defaultValue = "false") protected boolean allowDowngrade; @@ -104,7 +104,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { /** * Whether to allow the major version number to be changed. * - * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "allowMajorUpdates", defaultValue = "true") protected boolean allowMajorUpdates = true; @@ -114,7 +114,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { * *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} {@code false}

* - * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "allowMinorUpdates", defaultValue = "true") protected boolean allowMinorUpdates = true; @@ -125,7 +125,7 @@ public class DisplayParentUpdatesMojo extends AbstractVersionsDisplayMojo { *

Note: {@code false} also implies {@linkplain #allowMajorUpdates} * and {@linkplain #allowMinorUpdates} {@code false}

* - * @since 2.16.1 + * @since 2.17.0 */ @Parameter(property = "allowIncrementalUpdates", defaultValue = "true") protected boolean allowIncrementalUpdates = true; From af90f2700e88db46c77f7dc17fe9f2782b3dae34 Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Sun, 12 Nov 2023 20:29:16 +0100 Subject: [PATCH 7/9] Align variable name between DisplayParentUpdatesMojo and UpdateParentMojo --- .../mojo/versions/DisplayParentUpdatesMojo.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index 4921c1c1e7..f903802f7c 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -153,17 +153,17 @@ public void execute() throws MojoExecutionException, MojoFailureException { logLine(false, "Parent project is part of the reactor."); return; } - String currentVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; + String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; ArtifactVersion artifactVersion; try { artifactVersion = skipResolution ? DefaultArtifactVersionCache.of(parentVersion) - : resolveTargetVersion(currentVersion); + : resolveTargetVersion(initialVersion); } catch (VersionRetrievalException | InvalidVersionSpecificationException | InvalidSegmentException e) { throw new MojoExecutionException(e.getMessage(), e); } - if (artifactVersion == null || currentVersion.equals(artifactVersion.toString())) { + if (artifactVersion == null || initialVersion.equals(artifactVersion.toString())) { logLine(false, "The parent project is the latest version:"); StringBuilder buf = new StringBuilder(MESSAGE_LENGTH); buf.append(" "); @@ -171,12 +171,12 @@ public void execute() throws MojoExecutionException, MojoFailureException { buf.append(':'); buf.append(getProject().getParent().getArtifactId()); buf.append(' '); - int padding = MESSAGE_LENGTH - currentVersion.length(); + int padding = MESSAGE_LENGTH - initialVersion.length(); while (buf.length() < padding) { buf.append('.'); } buf.append(' '); - buf.append(currentVersion); + buf.append(initialVersion); logLine(false, buf.toString()); } else { logLine(false, "The parent project has a newer version:"); @@ -187,14 +187,14 @@ public void execute() throws MojoExecutionException, MojoFailureException { buf.append(getProject().getParent().getArtifactId()); buf.append(' '); int padding = MESSAGE_LENGTH - - currentVersion.length() + - initialVersion.length() - artifactVersion.toString().length() - " -> ".length(); while (buf.length() < padding) { buf.append('.'); } buf.append(' '); - buf.append(currentVersion); + buf.append(initialVersion); buf.append(" -> "); buf.append(artifactVersion); logLine(false, buf.toString()); From e768e4f721b2ce2e33f641771257ea7ab6bc80c9 Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Sun, 12 Nov 2023 20:32:05 +0100 Subject: [PATCH 8/9] Align skipResolution && isBlank(parentVersion) --- .../org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index f903802f7c..f98cbcca3e 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -153,6 +153,11 @@ public void execute() throws MojoExecutionException, MojoFailureException { logLine(false, "Parent project is part of the reactor."); return; } + + if (skipResolution && isBlank(parentVersion)) { + throw new MojoExecutionException("skipResolution is only valid if parentVersion is set"); + } + String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; ArtifactVersion artifactVersion; try { From e61b10d8e3801c8028e511469028d645adc13cee Mon Sep 17 00:00:00 2001 From: Lars Uffmann Date: Mon, 13 Nov 2023 06:22:35 +0100 Subject: [PATCH 9/9] Use Optional in favor of ternary operator --- .../org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java | 4 ++-- .../java/org/codehaus/mojo/versions/UpdateParentMojo.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java index f98cbcca3e..d30114d948 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayParentUpdatesMojo.java @@ -157,8 +157,8 @@ public void execute() throws MojoExecutionException, MojoFailureException { if (skipResolution && isBlank(parentVersion)) { throw new MojoExecutionException("skipResolution is only valid if parentVersion is set"); } - - String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; + String initialVersion = Optional.ofNullable(parentVersion) + .orElse(getProject().getParent().getVersion()); ArtifactVersion artifactVersion; try { artifactVersion = skipResolution diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java index c9ba097102..02ecb55799 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/UpdateParentMojo.java @@ -163,7 +163,8 @@ protected void update(ModifiedPomXMLEventReader pom) throw new MojoExecutionException("skipResolution is only valid if parentVersion is set"); } - String initialVersion = parentVersion == null ? getProject().getParent().getVersion() : parentVersion; + String initialVersion = Optional.ofNullable(parentVersion) + .orElse(getProject().getParent().getVersion()); try { ArtifactVersion artifactVersion = skipResolution ? DefaultArtifactVersionCache.of(parentVersion)