This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit be59fcc591912c6aba1e485d028a90e251a9547b Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sat Nov 14 17:44:50 2026 +0100 Honor exclusions on direct dependencies in go-offline filterDependencies returns the project's own Dependency objects; rebuilding them from artifact coordinates dropped exclusions, optional and systemPath. --- .../repository/go-offline-exclusion-lib-1.0.pom | 38 ++++++++++++++ .../invoker.properties | 18 +++++++ .../go-offline-direct-exclusion-gh-1803/pom.xml | 41 +++++++++++++++ .../verify.groovy | 23 +++++++++ .../dependency/resolvers/GoOfflineMojo.java | 59 ++++++++-------------- 5 files changed, 141 insertions(+), 38 deletions(-) diff --git a/src/it/mrm/repository/go-offline-exclusion-lib-1.0.pom b/src/it/mrm/repository/go-offline-exclusion-lib-1.0.pom new file mode 100644 index 00000000..76bf00b3 --- /dev/null +++ b/src/it/mrm/repository/go-offline-exclusion-lib-1.0.pom @@ -0,0 +1,38 @@ +<?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>go-offline-exclusion-lib</artifactId> + <version>1.0</version> + + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>go-offline-exclusion-absent</artifactId> + <version>1.0</version> + </dependency> + </dependencies> + +</project> diff --git a/src/it/projects/go-offline-direct-exclusion-gh-1803/invoker.properties b/src/it/projects/go-offline-direct-exclusion-gh-1803/invoker.properties new file mode 100644 index 00000000..e5961219 --- /dev/null +++ b/src/it/projects/go-offline-direct-exclusion-gh-1803/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 = ${project.groupId}:${project.artifactId}:${project.version}:go-offline diff --git a/src/it/projects/go-offline-direct-exclusion-gh-1803/pom.xml b/src/it/projects/go-offline-direct-exclusion-gh-1803/pom.xml new file mode 100644 index 00000000..e92f6273 --- /dev/null +++ b/src/it/projects/go-offline-direct-exclusion-gh-1803/pom.xml @@ -0,0 +1,41 @@ +<?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.plugins.dependency</groupId> + <artifactId>go-offline-direct-exclusion-gh-1803</artifactId> + <version>1.0.0-SNAPSHOT</version> + <packaging>pom</packaging> + <description>Test that dependency:go-offline honors an exclusion declared on a direct dependency</description> + <dependencies> + <dependency> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>go-offline-exclusion-lib</artifactId> + <version>1.0</version> + <exclusions> + <exclusion> + <groupId>org.apache.maven.its.dependency</groupId> + <artifactId>go-offline-exclusion-absent</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> +</project> diff --git a/src/it/projects/go-offline-direct-exclusion-gh-1803/verify.groovy b/src/it/projects/go-offline-direct-exclusion-gh-1803/verify.groovy new file mode 100644 index 00000000..322b34d3 --- /dev/null +++ b/src/it/projects/go-offline-direct-exclusion-gh-1803/verify.groovy @@ -0,0 +1,23 @@ +/* + * 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. + */ + +// go-offline-exclusion-absent exists in no repository, so resolving it would fail the build +String buildLog = new File(basedir, 'build.log').getText('UTF-8') +assert buildLog.contains('Resolved dependency: go-offline-exclusion-lib-1.0.jar') +assert !buildLog.contains('go-offline-exclusion-absent') diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java index cf526fa1..5df9c6fd 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java @@ -24,7 +24,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -293,47 +293,30 @@ public class GoOfflineMojo extends AbstractDependencyFilterMojo { return Arrays.asList(DependencyUtil.cleanToBeTokenizedString(list).split(",")); } - private Collection<Dependency> filterDependencies(Collection<Dependency> deps) throws ArtifactFilterException { - - Set<Artifact> artifacts = createArtifactSetFromDependencies(deps); - - final FilterArtifacts filter = getArtifactsFilter(); - artifacts = filter.filter(artifacts); - - return createDependencySetFromArtifacts(artifacts); - } - - private Set<Artifact> createArtifactSetFromDependencies(Collection<Dependency> deps) { - Set<Artifact> artifacts = new HashSet<>(); + /** + * Applies the artifact filters of this goal to the given dependencies. The original dependencies are returned, + * not copies rebuilt from their coordinates, so their exclusions, optional flag and system path are kept. + */ + private List<Dependency> filterDependencies(Collection<Dependency> deps) throws ArtifactFilterException { + Map<Dependency, Artifact> artifacts = new IdentityHashMap<>(); for (Dependency dep : deps) { - DefaultArtifactHandler handler = new DefaultArtifactHandler(dep.getType()); - artifacts.add(new DefaultArtifact( - dep.getGroupId(), - dep.getArtifactId(), - dep.getVersion(), - dep.getScope(), - dep.getType(), - dep.getClassifier(), - handler)); + artifacts.put( + dep, + new DefaultArtifact( + dep.getGroupId(), + dep.getArtifactId(), + dep.getVersion(), + dep.getScope(), + dep.getType(), + dep.getClassifier(), + new DefaultArtifactHandler(dep.getType()))); } - return artifacts; - } - private Collection<Dependency> createDependencySetFromArtifacts(Set<Artifact> artifacts) { - Set<Dependency> dependencies = new HashSet<>(); - - for (Artifact artifact : artifacts) { - Dependency d = new Dependency(); - d.setGroupId(artifact.getGroupId()); - d.setArtifactId(artifact.getArtifactId()); - d.setVersion(artifact.getVersion()); - d.setType(artifact.getType()); - d.setClassifier(artifact.getClassifier()); - d.setScope(artifact.getScope()); - dependencies.add(d); - } + Set<Artifact> selected = getArtifactsFilter().filter(new LinkedHashSet<>(artifacts.values())); - return dependencies; + return deps.stream() + .filter(dep -> selected.contains(artifacts.get(dep))) + .collect(Collectors.toList()); } /**
