From d88fa7dbeccda4857179e600af94b4cc1ff72b30 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 5 Aug 2026 18:44:59 +0000 Subject: [PATCH 1/2] Honor the mavenProjectId attribute in DependencyFilesetsTask (#371) --- .../ant/tasks/DependencyFilesetsTask.java | 2 +- .../ant/tasks/DependencyFilesetsTaskTest.java | 154 ++++++++++++++++++ 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java diff --git a/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java b/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java index 818f506..7b6bdf7 100644 --- a/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java +++ b/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java @@ -55,7 +55,7 @@ public void execute() { throw new BuildException("Maven project reference not found: " + mavenProjectId); } - MavenProject mavenProject = this.getProject().getReference("maven.project"); + MavenProject mavenProject = this.getProject().getReference(mavenProjectId); // Add filesets for depenedency artifacts Set depArtifacts = filterArtifacts(mavenProject.getArtifacts()); diff --git a/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java b/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java new file mode 100644 index 0000000..83d01fd --- /dev/null +++ b/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java @@ -0,0 +1,154 @@ +/* + * 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. + */ +package org.apache.maven.ant.tasks; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.plugins.antrun.AntRunMojo; +import org.apache.maven.project.MavenProject; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.types.FileSet; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * Test class for {@link DependencyFilesetsTask}. + */ +class DependencyFilesetsTaskTest { + + private static final String CUSTOM_PROJECT_REFID = "maven.project.alt"; + + private static final String DEFAULT_DEPENDENCIES_REFID = "maven.project.dependencies"; + + private static final String LOCAL_REPOSITORY_REFID = "maven.local.repository"; + + @TempDir + File folder; + + /** + * The task must use the {@link MavenProject} registered under the configured {@code mavenProjectId} reference, + * not a hardcoded one. + * + * @throws IOException In case of problems + */ + @Test + void honorsConfiguredMavenProjectId() throws IOException { + Artifact defaultArtifact = newArtifact("org.example", "artX"); + Artifact customArtifact = newArtifact("com.example", "artY"); + MavenProject defaultProject = newProject(defaultArtifact); + MavenProject customProject = newProject(customArtifact); + + Project antProject = new Project(); + antProject.addReference(AntRunMojo.DEFAULT_MAVEN_PROJECT_REFID, defaultProject); + antProject.addReference(CUSTOM_PROJECT_REFID, customProject); + antProject.addReference(LOCAL_REPOSITORY_REFID, newRepositoryStub()); + + DependencyFilesetsTask task = new DependencyFilesetsTask(); + task.setProject(antProject); + task.setMavenProjectId(CUSTOM_PROJECT_REFID); + + task.execute(); + + FileSet fileset = (FileSet) antProject.getReference(DEFAULT_DEPENDENCIES_REFID); + assertThat(includePatterns(antProject, fileset), hasItem(artifactPath(customArtifact))); + assertThat(includePatterns(antProject, fileset), not(hasItem(artifactPath(defaultArtifact)))); + } + + /** + * The task must work without the default {@code maven.project} reference when a custom {@code mavenProjectId} + * is configured, instead of throwing an NPE. + * + * @throws IOException In case of problems + */ + @Test + void worksWithoutDefaultMavenProjectReference() throws IOException { + Artifact customArtifact = newArtifact("com.example", "artY"); + MavenProject customProject = newProject(customArtifact); + + Project antProject = new Project(); + antProject.addReference(CUSTOM_PROJECT_REFID, customProject); + antProject.addReference(LOCAL_REPOSITORY_REFID, newRepositoryStub()); + + DependencyFilesetsTask task = new DependencyFilesetsTask(); + task.setProject(antProject); + task.setMavenProjectId(CUSTOM_PROJECT_REFID); + + assertDoesNotThrow(task::execute); + + FileSet fileset = (FileSet) antProject.getReference(DEFAULT_DEPENDENCIES_REFID); + assertThat(includePatterns(antProject, fileset), hasItem(artifactPath(customArtifact))); + } + + private List includePatterns(Project antProject, FileSet fileset) { + return Arrays.asList(fileset.mergePatterns(antProject).getIncludePatterns(antProject)); + } + + private MavenProject newProject(Artifact artifact) { + MavenProject project = new MavenProject(); + Set artifacts = new HashSet<>(); + artifacts.add(artifact); + project.setArtifacts(artifacts); + return project; + } + + private Artifact newArtifact(String groupId, String artifactId) throws IOException { + Artifact artifact = new DefaultArtifact( + groupId, artifactId, "1.0", Artifact.SCOPE_COMPILE, "jar", null, new DefaultArtifactHandler("jar")); + artifact.setFile( + Files.createTempFile(folder.toPath(), artifactId, ".jar").toFile()); + return artifact; + } + + private ArtifactRepository newRepositoryStub() { + InvocationHandler handler = (proxy, method, args) -> { + switch (method.getName()) { + case "pathOf": + return artifactPath((Artifact) args[0]); + case "getBasedir": + return folder.getAbsolutePath(); + default: + return null; + } + }; + return (ArtifactRepository) + Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {ArtifactRepository.class}, handler); + } + + private static String artifactPath(Artifact artifact) { + return artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + + artifact.getArtifactId() + "-" + artifact.getVersion() + "." + artifact.getType(); + } +} From b62e4253e63f23dbdbaac844e1d13426ac799a04 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 5 Aug 2026 20:28:57 +0000 Subject: [PATCH 2/2] Assert the mavenProjectId behavior via per-artifact fileset references --- .../ant/tasks/DependencyFilesetsTaskTest.java | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java b/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java index 83d01fd..fe3a738 100644 --- a/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java +++ b/src/test/java/org/apache/maven/ant/tasks/DependencyFilesetsTaskTest.java @@ -23,9 +23,7 @@ import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.nio.file.Files; -import java.util.Arrays; import java.util.HashSet; -import java.util.List; import java.util.Set; import org.apache.maven.artifact.Artifact; @@ -35,14 +33,12 @@ import org.apache.maven.plugins.antrun.AntRunMojo; import org.apache.maven.project.MavenProject; import org.apache.tools.ant.Project; -import org.apache.tools.ant.types.FileSet; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; /** * Test class for {@link DependencyFilesetsTask}. @@ -51,8 +47,6 @@ class DependencyFilesetsTaskTest { private static final String CUSTOM_PROJECT_REFID = "maven.project.alt"; - private static final String DEFAULT_DEPENDENCIES_REFID = "maven.project.dependencies"; - private static final String LOCAL_REPOSITORY_REFID = "maven.local.repository"; @TempDir @@ -82,9 +76,8 @@ void honorsConfiguredMavenProjectId() throws IOException { task.execute(); - FileSet fileset = (FileSet) antProject.getReference(DEFAULT_DEPENDENCIES_REFID); - assertThat(includePatterns(antProject, fileset), hasItem(artifactPath(customArtifact))); - assertThat(includePatterns(antProject, fileset), not(hasItem(artifactPath(defaultArtifact)))); + assertNotNull(antProject.getReference(customArtifact.getDependencyConflictId())); + assertNull(antProject.getReference(defaultArtifact.getDependencyConflictId())); } /** @@ -108,12 +101,7 @@ void worksWithoutDefaultMavenProjectReference() throws IOException { assertDoesNotThrow(task::execute); - FileSet fileset = (FileSet) antProject.getReference(DEFAULT_DEPENDENCIES_REFID); - assertThat(includePatterns(antProject, fileset), hasItem(artifactPath(customArtifact))); - } - - private List includePatterns(Project antProject, FileSet fileset) { - return Arrays.asList(fileset.mergePatterns(antProject).getIncludePatterns(antProject)); + assertNotNull(antProject.getReference(customArtifact.getDependencyConflictId())); } private MavenProject newProject(Artifact artifact) {