Skip to content

Commit

Permalink
Fixes mojohaus#896: Only using the range specified in versionRange fo…
Browse files Browse the repository at this point in the history
…r the retrieval of available versions if it's specified.

Previously I (mistakenly) used the "recommended version" as version range, which caused artifact version retrieval to only report the given version.
  • Loading branch information
andrzejj0 committed Jan 2, 2023
1 parent 2a755ad commit bf93fea
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions versions-maven-plugin/src/it/it-display-parent-updates-001/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>localhost</groupId>
<artifactId>dummy-parent2</artifactId>
<version>1.0</version>
<relativePath/>
</parent>

<artifactId>dummy-test</artifactId>
<version>1.0</version>

</project>
Original file line number Diff line number Diff line change
@@ -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/
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions versions-maven-plugin/src/it/it-display-parent-updates-002/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>localhost</groupId>
<artifactId>dummy-parent2</artifactId>
<version>[1.0, 2.0]</version>
<relativePath/>
</parent>

<artifactId>dummy-test</artifactId>
<version>1.0</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def output = new File(basedir, "output.txt").text
assert output =~ /\Qlocalhost:dummy-parent2\E\s*\.*\s*\Q[1.0, 2.0]\E\s+->\s+\Q2.0\E\b/
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.DependencyBuilder;

import static java.util.Optional.ofNullable;

/**
* Displays any updates of the project's parent project
*
Expand Down Expand Up @@ -70,21 +72,24 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

String currentVersion = getProject().getParent().getVersion();
String version = currentVersion;
// trying to use the original in order to retrieve the actual version range (if a version range exists)
String currentVersion = ofNullable(getProject().getOriginalModel())
.orElse(getProject().getModel())
.getParent()
.getVersion();

VersionRange versionRange;
try {
versionRange = VersionRange.createFromVersionSpec(version);
versionRange = VersionRange.createFromVersionSpec(currentVersion);
} catch (InvalidVersionSpecificationException e) {
throw new MojoExecutionException("Invalid version range specification: " + version, e);
throw new MojoExecutionException("Invalid version range specification: " + currentVersion, e);
}

Artifact artifact = getHelper()
.createDependencyArtifact(DependencyBuilder.newBuilder()
.withGroupId(getProject().getParent().getGroupId())
.withArtifactId(getProject().getParent().getArtifactId())
.withVersion(version)
.withVersion(currentVersion)
.withType("pom")
.build());

Expand Down

0 comments on commit bf93fea

Please sign in to comment.