-
Notifications
You must be signed in to change notification settings - Fork 195
Exclude transitive reactor dependencies #1659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mauhiz
wants to merge
1
commit into
apache:master
Choose a base branch
from
mauhiz:dependencies-transitive
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...pache/maven/plugins/dependency/resolvers/ExcludeReactorProjectsGraphDependencyFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * 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.plugins.dependency.resolvers; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.maven.artifact.ArtifactUtils; | ||
| import org.apache.maven.project.MavenProject; | ||
| import org.eclipse.aether.artifact.Artifact; | ||
| import org.eclipse.aether.graph.DependencyFilter; | ||
| import org.eclipse.aether.graph.DependencyNode; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Aether {@link DependencyFilter} implementation that excludes artifacts found in the Reactor, applied to the whole | ||
| * resolved dependency graph so transitive nodes pointing at reactor modules are skipped during artifact resolution. | ||
| */ | ||
| public class ExcludeReactorProjectsGraphDependencyFilter implements DependencyFilter { | ||
|
|
||
| private final Logger log = LoggerFactory.getLogger(ExcludeReactorProjectsGraphDependencyFilter.class); | ||
| private final Set<String> reactorArtifactKeys; | ||
|
|
||
| public ExcludeReactorProjectsGraphDependencyFilter(final List<MavenProject> reactorProjects) { | ||
| this.reactorArtifactKeys = reactorProjects.stream() | ||
| .map(project -> ArtifactUtils.key(project.getArtifact())) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean accept(DependencyNode node, List<DependencyNode> parents) { | ||
| if (node == null) { | ||
| return true; | ||
| } | ||
| Artifact artifact = node.getArtifact(); | ||
| if (artifact == null) { | ||
| return true; | ||
| } | ||
| String key = ArtifactUtils.key(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()); | ||
| if (reactorArtifactKeys.contains(key)) { | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("Skipped transitive dependency {} because it is present in the reactor", key); | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...e/maven/plugins/dependency/resolvers/ExcludeReactorProjectsGraphDependencyFilterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * 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.plugins.dependency.resolvers; | ||
|
|
||
| import org.apache.maven.artifact.Artifact; | ||
| import org.apache.maven.artifact.DefaultArtifact; | ||
| import org.apache.maven.project.MavenProject; | ||
| import org.eclipse.aether.graph.DefaultDependencyNode; | ||
| import org.eclipse.aether.graph.Dependency; | ||
| import org.eclipse.aether.graph.DependencyNode; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static java.util.Collections.singletonList; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class ExcludeReactorProjectsGraphDependencyFilterTest { | ||
|
|
||
| @Mock | ||
| private MavenProject project; | ||
|
|
||
| @Test | ||
| void testRejectReactorTransitive() { | ||
| Artifact reactorArtifact = anArtifact(); | ||
| when(project.getArtifact()).thenReturn(reactorArtifact); | ||
|
|
||
| ExcludeReactorProjectsGraphDependencyFilter filter = | ||
| new ExcludeReactorProjectsGraphDependencyFilter(singletonList(project)); | ||
|
|
||
| DependencyNode node = | ||
| nodeFor(reactorArtifact.getGroupId(), reactorArtifact.getArtifactId(), reactorArtifact.getVersion()); | ||
|
|
||
| assertFalse(filter.accept(node, emptyList())); | ||
| } | ||
|
|
||
| @Test | ||
| void testAcceptNonReactorTransitive() { | ||
| Artifact reactorArtifact = anArtifact(); | ||
| when(project.getArtifact()).thenReturn(reactorArtifact); | ||
|
|
||
| ExcludeReactorProjectsGraphDependencyFilter filter = | ||
| new ExcludeReactorProjectsGraphDependencyFilter(singletonList(project)); | ||
|
|
||
| DependencyNode node = nodeFor("something-else", reactorArtifact.getArtifactId(), reactorArtifact.getVersion()); | ||
|
|
||
| assertTrue(filter.accept(node, emptyList())); | ||
| } | ||
|
|
||
| @Test | ||
| void testAcceptNullArtifact() { | ||
| Artifact reactorArtifact = anArtifact(); | ||
| when(project.getArtifact()).thenReturn(reactorArtifact); | ||
|
|
||
| ExcludeReactorProjectsGraphDependencyFilter filter = | ||
| new ExcludeReactorProjectsGraphDependencyFilter(singletonList(project)); | ||
|
|
||
| assertTrue(filter.accept(new DefaultDependencyNode((Dependency) null), emptyList())); | ||
| } | ||
|
|
||
| private static DependencyNode nodeFor(String groupId, String artifactId, String version) { | ||
| org.eclipse.aether.artifact.Artifact aetherArtifact = | ||
| new org.eclipse.aether.artifact.DefaultArtifact(groupId, artifactId, "jar", version); | ||
| return new DefaultDependencyNode(new Dependency(aetherArtifact, null)); | ||
| } | ||
|
|
||
| private Artifact anArtifact() { | ||
| return new DefaultArtifact( | ||
| "org.apache.maven.plugins", "maven-dependency-plugin-dummy", "1.0", null, "jar", "", null); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No new dependencies is needed for it?
I would like to not introduce it.