From fc5f3348ca7db32de573ff3263c4cd3cf12adf43 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Mon, 2 Jan 2023 16:28:27 +0100 Subject: [PATCH] Fixes #896: Fixing available version retrieval; enabling allowSnapshots - Version range should not be specified in the retrieval at all; removed. - If provided, the actual version range restriction should be used, and not the result of the toString() method - Also, allowSnapshots was ignored for the mojo; corrected --- .../versions/api/DefaultVersionsHelper.java | 8 ++++- .../invoker.properties | 21 ++++++++++++ .../pom.xml | 34 +++++++++++++++++++ .../verify.groovy | 2 ++ .../invoker.properties | 21 ++++++++++++ .../pom.xml | 34 +++++++++++++++++++ .../verify.groovy | 2 ++ .../invoker.properties | 21 ++++++++++++ .../pom.xml | 34 +++++++++++++++++++ .../verify.groovy | 2 ++ .../versions/DisplayParentUpdatesMojo.java | 15 ++------ 11 files changed, 180 insertions(+), 14 deletions(-) create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-no-updates/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-no-updates/pom.xml create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-no-updates/verify.groovy create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-simple-update/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-simple-update/pom.xml create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-simple-update/verify.groovy create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-snapshots/invoker.properties create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-snapshots/pom.xml create mode 100644 versions-maven-plugin/src/it/it-display-parent-updates-snapshots/verify.groovy diff --git a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java index edee268bd..5d3850f70 100644 --- a/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/versions-common/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -53,6 +53,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.artifact.versioning.Restriction; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Dependency; @@ -184,7 +185,12 @@ public ArtifactVersions lookupArtifactVersions( mavenSession.getRepositorySession(), new VersionRangeRequest( toArtifact(artifact) - .setVersion(versionRange != null ? versionRange.toString() : "(,)"), + .setVersion(ofNullable(versionRange) + .map(VersionRange::getRestrictions) + .flatMap(list -> list.stream() + .findFirst() + .map(Restriction::toString)) + .orElse("(,)")), usePluginRepositories ? mavenSession .getCurrentProject() diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/invoker.properties b/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/invoker.properties new file mode 100644 index 000000000..20d50bca8 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/invoker.properties @@ -0,0 +1,21 @@ +# +# 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. +# + +invoker.goals = ${project.groupId}:${project.artifactId}:2.13.0:display-parent-updates +invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8 diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/pom.xml b/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/pom.xml new file mode 100644 index 000000000..13ca1ff41 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + localhost + dummy-parent2 + 3.1 + + + + dummy-test + 1.0 + + diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/verify.groovy b/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/verify.groovy new file mode 100644 index 000000000..ff53d5c99 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-no-updates/verify.groovy @@ -0,0 +1,2 @@ +def output = new File(basedir, "output.txt").text +assert output =~ /\Qlocalhost:dummy-parent2\E\s*\.*\s*\Q3.1\E\s*$/ diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/invoker.properties b/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/invoker.properties new file mode 100644 index 000000000..d71819f43 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/invoker.properties @@ -0,0 +1,21 @@ +# +# 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. +# + +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:display-parent-updates +invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8 diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/pom.xml b/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/pom.xml new file mode 100644 index 000000000..bc899703c --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + localhost + dummy-parent2 + 1.0 + + + + dummy-test + 1.0 + + diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/verify.groovy b/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/verify.groovy new file mode 100644 index 000000000..1384c8e65 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-simple-update/verify.groovy @@ -0,0 +1,2 @@ +def output = new File(basedir, "output.txt").text +assert output =~ /\Qlocalhost:dummy-parent2\E\s*\.*\s*\Q1.0\E\s+->\s+\Q3.1\E\b/ diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/invoker.properties b/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/invoker.properties new file mode 100644 index 000000000..d610a8a74 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/invoker.properties @@ -0,0 +1,21 @@ +# +# 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. +# + +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:display-parent-updates +invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8 -DallowSnapshots=true diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/pom.xml b/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/pom.xml new file mode 100644 index 000000000..a163f27d6 --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/pom.xml @@ -0,0 +1,34 @@ + + + + + 4.0.0 + + localhost + dummy-parent4 + 70 + + + + dummy-test + 1.0 + + diff --git a/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/verify.groovy b/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/verify.groovy new file mode 100644 index 000000000..97bffac7c --- /dev/null +++ b/versions-maven-plugin/src/it/it-display-parent-updates-snapshots/verify.groovy @@ -0,0 +1,2 @@ +def output = new File(basedir, "output.txt").text +assert output =~ /\Qlocalhost:dummy-parent4\E\s*\.*\s*\Q70\E\s+->\s+\Q71-SNAPSHOT\E\b/ 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 af8ee80e3..78272957b 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 @@ -25,8 +25,6 @@ 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.plugins.annotations.Mojo; @@ -71,26 +69,17 @@ public void execute() throws MojoExecutionException, MojoFailureException { } String currentVersion = getProject().getParent().getVersion(); - String version = currentVersion; - - VersionRange versionRange; - try { - versionRange = VersionRange.createFromVersionSpec(version); - } catch (InvalidVersionSpecificationException e) { - throw new MojoExecutionException("Invalid version range specification: " + version, e); - } - Artifact artifact = getHelper() .createDependencyArtifact(DependencyBuilder.newBuilder() .withGroupId(getProject().getParent().getGroupId()) .withArtifactId(getProject().getParent().getArtifactId()) - .withVersion(version) + .withVersion(currentVersion) .withType("pom") .build()); ArtifactVersion artifactVersion; try { - artifactVersion = findLatestVersion(artifact, versionRange, null, false); + artifactVersion = findLatestVersion(artifact, null, allowSnapshots, false); } catch (VersionRetrievalException e) { throw new MojoExecutionException(e.getMessage(), e); }