You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/10/13 21:50:03 UTC

(maven-dependency-plugin) 01/01: [MDEP-650] Resolve version ranges of copy and unpack artifact items

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch agent/mdep-650-version-ranges
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git

commit 846fc3c0ad3b88383de6826b683ad62e0d6d9e3f
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Tue Oct 13 23:47:03 2026 +0200

    [MDEP-650] Resolve version ranges of copy and unpack artifact items
---
 .../repository/version-range-1.0.jar/version.txt   | 19 +++++++
 src/it/mrm/repository/version-range-1.0.pom        | 30 ++++++++++
 .../repository/version-range-1.5.jar/version.txt   | 19 +++++++
 src/it/mrm/repository/version-range-1.5.pom        | 30 ++++++++++
 .../repository/version-range-2.0.jar/version.txt   | 19 +++++++
 src/it/mrm/repository/version-range-2.0.pom        | 30 ++++++++++
 .../mdep-650-copy-version-range/invoker.properties | 18 ++++++
 .../projects/mdep-650-copy-version-range/pom.xml   | 66 ++++++++++++++++++++++
 .../mdep-650-copy-version-range/verify.groovy      | 22 ++++++++
 .../AbstractFromConfigurationMojo.java             | 12 +++-
 .../plugins/dependency/utils/ResolverUtil.java     | 26 +++++++++
 .../dependency/fromConfiguration/TestCopyMojo.java | 52 +++++++++++++++++
 12 files changed, 342 insertions(+), 1 deletion(-)

diff --git a/src/it/mrm/repository/version-range-1.0.jar/version.txt b/src/it/mrm/repository/version-range-1.0.jar/version.txt
new file mode 100644
index 00000000..89c8446d
--- /dev/null
+++ b/src/it/mrm/repository/version-range-1.0.jar/version.txt
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+version 1.0
diff --git a/src/it/mrm/repository/version-range-1.0.pom b/src/it/mrm/repository/version-range-1.0.pom
new file mode 100644
index 00000000..3dfc96ba
--- /dev/null
+++ b/src/it/mrm/repository/version-range-1.0.pom
@@ -0,0 +1,30 @@
+<?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>
+
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>version-range</artifactId>
+  <version>1.0</version>
+
+</project>
diff --git a/src/it/mrm/repository/version-range-1.5.jar/version.txt b/src/it/mrm/repository/version-range-1.5.jar/version.txt
new file mode 100644
index 00000000..820ee183
--- /dev/null
+++ b/src/it/mrm/repository/version-range-1.5.jar/version.txt
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+version 1.5
diff --git a/src/it/mrm/repository/version-range-1.5.pom b/src/it/mrm/repository/version-range-1.5.pom
new file mode 100644
index 00000000..927b75a9
--- /dev/null
+++ b/src/it/mrm/repository/version-range-1.5.pom
@@ -0,0 +1,30 @@
+<?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>
+
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>version-range</artifactId>
+  <version>1.5</version>
+
+</project>
diff --git a/src/it/mrm/repository/version-range-2.0.jar/version.txt b/src/it/mrm/repository/version-range-2.0.jar/version.txt
new file mode 100644
index 00000000..2c51822d
--- /dev/null
+++ b/src/it/mrm/repository/version-range-2.0.jar/version.txt
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+version 2.0
diff --git a/src/it/mrm/repository/version-range-2.0.pom b/src/it/mrm/repository/version-range-2.0.pom
new file mode 100644
index 00000000..4c1cd250
--- /dev/null
+++ b/src/it/mrm/repository/version-range-2.0.pom
@@ -0,0 +1,30 @@
+<?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>
+
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>version-range</artifactId>
+  <version>2.0</version>
+
+</project>
diff --git a/src/it/projects/mdep-650-copy-version-range/invoker.properties b/src/it/projects/mdep-650-copy-version-range/invoker.properties
new file mode 100644
index 00000000..2e199f7b
--- /dev/null
+++ b/src/it/projects/mdep-650-copy-version-range/invoker.properties
@@ -0,0 +1,18 @@
+# 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 = clean package
diff --git a/src/it/projects/mdep-650-copy-version-range/pom.xml b/src/it/projects/mdep-650-copy-version-range/pom.xml
new file mode 100644
index 00000000..addf2d6c
--- /dev/null
+++ b/src/it/projects/mdep-650-copy-version-range/pom.xml
@@ -0,0 +1,66 @@
+<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.dependency</groupId>
+  <artifactId>mdep-650-copy-version-range</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>Test</name>
+  <description>
+    Test that dependency:copy resolves a version range to the highest matching version (MDEP-650)
+  </description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <phase>package</phase>
+            <goals>
+              <goal>copy</goal>
+            </goals>
+            <configuration>
+              <artifactItems>
+                <artifactItem>
+                  <groupId>org.apache.maven.its.dependency</groupId>
+                  <artifactId>version-range</artifactId>
+                  <version>[1.0,2.0)</version>
+                </artifactItem>
+              </artifactItems>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/mdep-650-copy-version-range/verify.groovy b/src/it/projects/mdep-650-copy-version-range/verify.groovy
new file mode 100644
index 00000000..46aae603
--- /dev/null
+++ b/src/it/projects/mdep-650-copy-version-range/verify.groovy
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+assert new File( basedir, 'target/dependency/version-range-1.5.jar' ).isFile()
+assert !new File( basedir, 'target/dependency/version-range-2.0.jar' ).exists()
+assert !new File( basedir, 'target/dependency/version-range-1.0.jar' ).exists()
diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
index 0fe60cbd..11558695 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/AbstractFromConfigurationMojo.java
@@ -41,6 +41,7 @@ import org.eclipse.aether.RepositorySystem;
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.artifact.DefaultArtifact;
 import org.eclipse.aether.resolution.ArtifactResolutionException;
+import org.eclipse.aether.resolution.VersionRangeResolutionException;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
 /**
@@ -239,7 +240,7 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo
                 extension = artifactItem.getType();
             }
 
-            DefaultArtifact artifact = new DefaultArtifact(
+            org.eclipse.aether.artifact.Artifact artifact = new DefaultArtifact(
                     artifactItem.getGroupId(),
                     artifactItem.getArtifactId(),
                     classifier,
@@ -248,10 +249,19 @@ public abstract class AbstractFromConfigurationMojo extends AbstractDependencyMo
 
             RepositorySystemSession repositorySession = createSystemSessionForLocalRepo();
 
+            String version = artifactItem.getVersion().trim();
+            if (version.startsWith("[") || version.startsWith("(")) {
+                artifact = resolverUtil.resolveVersionRange(
+                        artifact, getProject().getRemoteProjectRepositories(), repositorySession);
+                getLog().debug("Version range " + version + " resolved to " + artifact.getVersion());
+            }
+
             org.eclipse.aether.artifact.Artifact resolvedArtifact = resolverUtil.resolveArtifactWithFallback(
                     artifact, getProject().getRemoteProjectRepositories(), repositorySession);
             return RepositoryUtils.toArtifact(resolvedArtifact);
 
+        } catch (VersionRangeResolutionException e) {
+            throw new MojoExecutionException("Unable to resolve version range " + artifactItem.getVersion() + ".", e);
         } catch (ArtifactResolutionException e) {
             throw new MojoExecutionException("Unable to find/resolve artifact.", e);
         }
diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java b/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java
index b48a54a4..f3041508 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/utils/ResolverUtil.java
@@ -72,9 +72,13 @@ import org.eclipse.aether.resolution.ArtifactResult;
 import org.eclipse.aether.resolution.DependencyRequest;
 import org.eclipse.aether.resolution.DependencyResolutionException;
 import org.eclipse.aether.resolution.DependencyResult;
+import org.eclipse.aether.resolution.VersionRangeRequest;
+import org.eclipse.aether.resolution.VersionRangeResolutionException;
+import org.eclipse.aether.resolution.VersionRangeResult;
 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
 import org.eclipse.aether.util.artifact.SubArtifact;
 import org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator;
+import org.eclipse.aether.version.Version;
 
 /**
  * Helper class for using Resolver API.
@@ -205,6 +209,28 @@ public class ResolverUtil {
         return resolveArtifactDirectly(artifactDescriptorResult.getArtifact(), repositories, session);
     }
 
+    /**
+     * Resolve the version range of a given artifact to the highest version in the range.
+     *
+     * @param artifact     an artifact with a version range as its version
+     * @param repositories remote repositories list
+     * @param session      a repository system session
+     * @return the artifact with the highest version of the range as its version
+     * @throws VersionRangeResolutionException if the range could not be resolved or contains no version
+     */
+    public Artifact resolveVersionRange(
+            Artifact artifact, List<RemoteRepository> repositories, RepositorySystemSession session)
+            throws VersionRangeResolutionException {
+        VersionRangeResult result =
+                repositorySystem.resolveVersionRange(session, new VersionRangeRequest(artifact, repositories, null));
+        Version highest = result.getHighestVersion();
+        if (highest == null) {
+            throw new VersionRangeResolutionException(
+                    result, "No version of " + artifact + " matches the range " + artifact.getVersion());
+        }
+        return artifact.setVersion(highest.toString());
+    }
+
     /**
      * Resolve a given artifact, falling back to its original coordinates when its descriptor cannot be read.
      *
diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
index 019116e8..9c0001d2 100644
--- a/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
+++ b/src/test/java/org/apache/maven/plugins/dependency/fromConfiguration/TestCopyMojo.java
@@ -22,8 +22,11 @@ import javax.inject.Inject;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.maven.api.plugin.testing.InjectMojo;
@@ -45,6 +48,7 @@ import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.junit.jupiter.api.AssertionsKt.assertNull;
@@ -598,6 +602,54 @@ class TestCopyMojo {
         assertEquals("3.1", item.getVersion());
     }
 
+    private void installVersions(String... versions) throws IOException {
+        File artifactDir = new File(tempDir, "localTestRepo/remote/ranged");
+        Files.createDirectories(artifactDir.toPath());
+        StringBuilder metadata = new StringBuilder(
+                "<metadata><groupId>remote</groupId><artifactId>ranged</artifactId><versioning><versions>");
+        for (String version : versions) {
+            File dir = new File(artifactDir, version);
+            Files.createDirectories(dir.toPath());
+            Files.write(new File(dir, "ranged-" + version + ".jar").toPath(), version.getBytes(StandardCharsets.UTF_8));
+            metadata.append("<version>").append(version).append("</version>");
+        }
+        metadata.append("</versions></versioning></metadata>");
+        Files.write(
+                new File(artifactDir, "maven-metadata-local.xml").toPath(),
+                metadata.toString().getBytes(StandardCharsets.UTF_8));
+    }
+
+    private ArtifactItem rangedItem(String range) {
+        ArtifactItem item = new ArtifactItem();
+        item.setGroupId("remote");
+        item.setArtifactId("ranged");
+        item.setVersion(range);
+        return item;
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    void testVersionRangeResolvesToHighestVersion(CopyMojo mojo) throws Exception {
+        installVersions("1.0", "1.5", "2.0");
+        mojo.setArtifactItems(Collections.singletonList(rangedItem("[1.0,2.0)")));
+
+        mojo.execute();
+
+        assertTrue(new File(mojo.getOutputDirectory(), "ranged-1.5.jar").isFile());
+        assertFalse(new File(mojo.getOutputDirectory(), "ranged-2.0.jar").exists());
+    }
+
+    @Test
+    @InjectMojo(goal = "copy")
+    void testVersionRangeWithoutMatchingVersion(CopyMojo mojo) throws Exception {
+        installVersions("1.0", "2.0");
+        mojo.setArtifactItems(Collections.singletonList(rangedItem("[3.0,4.0)")));
+
+        MojoExecutionException e = assertThrows(MojoExecutionException.class, mojo::execute);
+
+        assertTrue(e.getMessage().contains("[3.0,4.0)"), e.getMessage());
+    }
+
     @Test
     @InjectMojo(goal = "copy")
     void testArtifactNotFound(CopyMojo mojo) throws Exception {