Skip to content

Commit

Permalink
Fixes mojohaus#896: Fixing available version retrieval; enabling allo…
Browse files Browse the repository at this point in the history
…wSnapshots

- 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
  • Loading branch information
andrzejj0 committed Jan 2, 2023
1 parent 2a755ad commit fc5f334
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 14 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}:2.13.0:display-parent-updates
invoker.mavenOpts = -Dversions.outputFile=./output.txt -DoutputEncoding=UTF-8
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>3.1</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*\Q3.1\E\s*$/
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
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 -DallowSnapshots=true
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-parent4</artifactId>
<version>70</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-parent4\E\s*\.*\s*\Q70\E\s+->\s+\Q71-SNAPSHOT\E\b/
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit fc5f334

Please sign in to comment.