Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-dependency-plugin</artifactId>
<version>3.9.1-SNAPSHOT</version>
<version>3.10.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven Dependency Plugin</name>
Expand Down
30 changes: 30 additions & 0 deletions src/it/mrm/repository/d-without-dep-3.2.1.pom
Original file line number Diff line number Diff line change
@@ -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.dependencies</groupId>
<artifactId>d-without-dep</artifactId>
<version>3.2.1</version>

</project>
18 changes: 18 additions & 0 deletions src/it/projects/copy-dependencies-graphroots/invoker.properties
Original file line number Diff line number Diff line change
@@ -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 process-sources
112 changes: 112 additions & 0 deletions src/it/projects/copy-dependencies-graphroots/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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>

<parent>
<groupId>org.apache</groupId>
<artifactId>apache</artifactId>
<version>5</version>
</parent>

<groupId>org.apache.maven.its.dependency</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Test</name>
<description>
Test dependency:copy-dependencies using graphRoots
</description>

<dependencies>
<dependency>
<groupId>org.apache.maven.its.dependencies</groupId>
<artifactId>d-without-dep</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.its.dependency</groupId>
<artifactId>a-with-dep</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.its.dependency</groupId>
<artifactId>get-artifact</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>

<defaultGoal>package</defaultGoal>

<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>test-1</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<graphRoots>
<graphRoot>
<groupId>org.apache.maven.its.dependency</groupId>
<artifactId>a-with-dep</artifactId>
</graphRoot>
<graphRoot>
<groupId>org.apache.maven.its.dependencies</groupId>
<artifactId>d-without-dep</artifactId>
</graphRoot>
</graphRoots>
<outputDirectory>${project.build.directory}/it/copy-dep-test-1</outputDirectory>
</configuration>
</execution>
<execution>
<id>test-2</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<graphRoots>
<graphRoot>
<groupId>org.apache.maven.its.dependency</groupId>
<artifactId>get-artifact</artifactId>
</graphRoot>
</graphRoots>
<outputDirectory>${project.build.directory}/it/copy-dep-test-2</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
37 changes: 37 additions & 0 deletions src/it/projects/copy-dependencies-graphroots/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

def targetFiles1 = ['a-with-dep-1.0.0.jar', 'b-with-dep-1.0.0.jar', 'c-without-dep-1.0.0.jar', 'd-without-dep-3.2.1.jar']
def directory1 = new File(basedir, 'target/it/copy-dep-test-1')

// Get only file names from the directory (excludes subdirectories)
def actualFiles1 = directory1.listFiles().findAll { it.isFile() }.collect { it.name }

// Check if the sets are identical and have exactly 3 files
assert (actualFiles1.size() == 4 && actualFiles1.containsAll(targetFiles1))


def targetFiles2 = ['get-artifact-1.0.jar', 'get-artifact-transitive-1.0.jar']
def directory2 = new File(basedir, 'target/it/copy-dep-test-2')

// Get only file names from the directory (excludes subdirectories)
def actualFiles2 = directory2.listFiles().findAll { it.isFile() }.collect { it.name }

// Check if the sets are identical and have exactly 3 files
assert (actualFiles2.size() == 2 && actualFiles2.containsAll(targetFiles2))
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.AbstractDependencyMojo;
Expand All @@ -52,7 +56,9 @@
import org.apache.maven.shared.artifact.filter.collection.ProjectTransitivityFilter;
import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.sonatype.plexus.build.incremental.BuildContext;

/**
Expand Down Expand Up @@ -231,6 +237,16 @@ public abstract class AbstractDependencyFilterMojo extends AbstractDependencyMoj
@Parameter(property = "mdep.prependGroupId", defaultValue = "false")
protected boolean prependGroupId = false;

/**
* By default, this goal uses the project itself as the root of the dependency tree.
* With graphRoots, you can select a subtree of dependencies based on groupId and artifactId.
* After that, the general include/exclude filters can be applied.
*
* @since 3.10.0
*/
@Parameter
private List<GraphRoot> graphRoots;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As GraphRoot is an complex object we can provide an example in documentation to be clear how to use in configuration


private final ResolverUtil resolverUtil;

private final ProjectBuilder projectBuilder;
Expand Down Expand Up @@ -292,6 +308,7 @@ protected DependencyStatusSets getDependencySets(boolean stopOnFailure) throws M
*/
protected DependencyStatusSets getDependencySets(boolean stopOnFailure, boolean includeParents)
throws MojoExecutionException {

// add filters in well known order, least specific to most specific
FilterArtifacts filter = new FilterArtifacts();

Expand Down Expand Up @@ -323,7 +340,13 @@ protected DependencyStatusSets getDependencySets(boolean stopOnFailure, boolean
DependencyUtil.cleanToBeTokenizedString(this.excludeArtifactIds)));

// start with all artifacts.
Set<Artifact> artifacts = getProject().getArtifacts();
Set<Artifact> artifacts;

try {
artifacts = collectArtifacts(getProject());
} catch (DependencyResolutionException e) {
throw new MojoExecutionException("Failed to collect artifacts", e);
}

if (includeParents) {
// add dependencies parents
Expand Down Expand Up @@ -479,6 +502,42 @@ private Set<Artifact> resolve(Set<org.eclipse.aether.artifact.Artifact> artifact
return resolvedArtifacts;
}

private Set<Artifact> collectArtifacts(MavenProject project) throws DependencyResolutionException {
if (graphRoots == null || graphRoots.isEmpty()) {
// artifact have already been resolved here due to
// @Mojo(requiresDependencyResolution = ResolutionScope.TEST) on final Mojo
return project.getArtifacts();
} else {
// MavenProject doesn't provide access to the graph of dependencies(only the direct dependencies)
// Hence we need to re-resolve artifacts, but only for the matching graphnodes
List<DependencyMatcher> filterMatchers =
graphRoots.stream().map(GraphRootMatcher::new).collect(Collectors.toList());

DependencyMatcher subTreeMatcher = new OrDependencyMatcher(filterMatchers);

Set<Artifact> artifacts = new HashSet<>();
for (Dependency dep : project.getDependencies()) {
if (subTreeMatcher.matches(dep)) {
artifacts.addAll(resolveDependencyArtifacts(dep));
}
}
return artifacts;
}
}

private Set<Artifact> resolveDependencyArtifacts(Dependency root) throws DependencyResolutionException {
org.eclipse.aether.graph.Dependency dependency = RepositoryUtils.toDependency(
root, session.getRepositorySession().getArtifactTypeRegistry());

List<RemoteRepository> remoteRepositories =
RepositoryUtils.toRepos(session.getProjectBuildingRequest().getRemoteRepositories());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is MavenProject#getRemoteProjectRepositories


Collection<org.eclipse.aether.artifact.Artifact> depArtifacts =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: we do full (transitive) dependency resolution only when the user specified one or more graphRoots, but not when the project itself is the graphRoot. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project dependencies have already been resolved, as the goal of the final Mojo contains requiresDependencyResolution = ResolutionScope.TEST. So it is actually re-resolving the dependencies. The MavenProject doesn't provide the graph, hence the need ask for the graph user maven artifact resolver.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get it. Maybe worth copying that text to an inline comment, in order to prevent future maintainers from asking themselves why the code is there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

resolverUtil.resolveDependencies(dependency.getArtifact(), remoteRepositories);

return depArtifacts.stream().map(RepositoryUtils::toArtifact).collect(Collectors.toSet());
}

/**
* @return returns the markersDirectory
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.fromDependencies;

import org.apache.maven.model.Dependency;

@FunctionalInterface
public interface DependencyMatcher {

boolean matches(Dependency dependency);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.fromDependencies;

public class GraphRoot {

private String groupId;

private String artifactId;

public String getGroupId() {
return groupId;
}

public void setGroupId(String groupId) {
this.groupId = groupId;
}

public String getArtifactId() {
return artifactId;
}

public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}
}
Loading