Skip to content
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

Rewrite dependencies resolution in AbstractResolveDependencies. #303

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.26.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.assertj.core.api.Assertions;

File file = new File( basedir, "build.log" );
assert file.exists();

Expand All @@ -10,7 +12,7 @@ assert text.contains("Found Banned Dependency: org.hibernate:hibernate-core:jar:
assert text.contains("Found Banned Dependency: javax.transaction:jta:jar:1.1")
assert text.contains("Found Banned Dependency: org.slf4j:slf4j-api:jar:1.4.2")
assert text.contains("Found Banned Dependency: dom4j:dom4j:jar:1.6.1")
assert text.contains("Restricted to JDK 1.2 yet org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile contains org/hibernate/annotations/common/AssertionFailure.class targeted to JDK 1.5")
assert text.contains("Restricted to JDK 1.2 yet dom4j:dom4j:jar:1.6.1:compile contains org/dom4j/Attribute.class targeted to JDK 1.3")
Assertions.assertThat(text).matches(~"(?s).*\\QRestricted to JDK 1.2 yet org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA\\E(?::compile)?\\Q contains org/hibernate/annotations/common/AssertionFailure.class targeted to JDK 1.5\\E.*")
Assertions.assertThat(text).matches(~"(?s).*\\QRestricted to JDK 1.2 yet dom4j:dom4j:jar:1.6.1\\E(?::compile)?\\Q contains org/dom4j/Attribute.class targeted to JDK 1.3\\E.*")

return true;
2 changes: 1 addition & 1 deletion src/it/local-project-repo/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ assert file.exists();

String text = file.getText( "utf-8" );

assert text.contains( '[DEBUG] Analyzing artifact dumy:dumy-local-repo:pom:1.0:compile' )
assert text.contains( '[DEBUG] Analyzing artifact dumy:dumy-local-repo:pom:1.0' )

return true;
22 changes: 11 additions & 11 deletions src/it/mojo-1744/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ assert log.exists()
def duplicates = new BanDuplicateClassesLogParser( log ).parse();

assert duplicates == [
(["org.jvnet.hudson.plugins.m2release:nexus:jar:0.0.1:compile",
"org.slf4j:jcl-over-slf4j:jar:1.5.11:compile"] as Set)
: (["org/apache/commons/logging/impl/SLF4JLocationAwareLog.class"] as Set),
(["org.jvnet.hudson.plugins.m2release:nexus:jar:0.0.1:compile",
"org.slf4j:jcl-over-slf4j:jar:1.5.11:compile",
"commons-logging:commons-logging:jar:1.1.1:compile"] as Set)
: (["org/apache/commons/logging/impl/NoOpLog.class"] as Set),
(["commons-logging:commons-logging:jar:1.1.1:compile",
"org.jvnet.hudson.plugins.m2release:nexus:jar:0.0.1:compile"] as Set)
(["org.jvnet.hudson.plugins.m2release:nexus:jar:0.0.1",
"org.slf4j:jcl-over-slf4j:jar:1.5.11"] as TreeSet)
: (["org/apache/commons/logging/impl/SLF4JLocationAwareLog.class"] as TreeSet),
(["commons-logging:commons-logging:jar:1.1.1",
"org.jvnet.hudson.plugins.m2release:nexus:jar:0.0.1"] as TreeSet)
: (["org/apache/commons/logging/impl/SimpleLog.class",
"org/apache/commons/logging/impl/SimpleLog\$1.class",
"org/apache/commons/logging/Log.class"] as Set)
]
"org/apache/commons/logging/Log.class"] as TreeSet),
(["commons-logging:commons-logging:jar:1.1.1",
"org.jvnet.hudson.plugins.m2release:nexus:jar:0.0.1",
"org.slf4j:jcl-over-slf4j:jar:1.5.11"] as TreeSet)
: (["org/apache/commons/logging/impl/NoOpLog.class"] as TreeSet)
] as LinkedHashMap
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.codehaus.mojo.extraenforcer.dependencies;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -17,19 +15,16 @@
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.ArtifactTypeRegistry;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.CollectResult;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.graph.DefaultDependencyNode;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.graph.DependencyVisitor;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
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.util.filter.AndDependencyFilter;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;

Expand Down Expand Up @@ -79,70 +74,47 @@ public void execute() throws EnforcerRuleException {
protected abstract void handleArtifacts(Set<Artifact> artifacts) throws EnforcerRuleException;

private Set<Artifact> getDependenciesToCheck() throws EnforcerRuleException {
Set<Artifact> artifacts = null;
try {
Collection<DependencyNode> dependencies = collectProjectDependencies();
artifacts = resolveArtifacts(dependencies);
} catch (DependencyCollectionException | ArtifactResolutionException e) {
ArtifactTypeRegistry artifactTypeRegistry =
session.getRepositorySession().getArtifactTypeRegistry();

final MavenProject currentProject = session.getCurrentProject();

final List<DependencyFilter> filters = new ArrayList<>(3);
Optional.ofNullable(createOptionalFilter()).ifPresent(filters::add);
Optional.ofNullable(createScopeDependencyFilter()).ifPresent(filters::add);
filters.add((node, parents) -> searchTransitive || parents.size() <= 1);
DependencyFilter dependencyFilter = new AndDependencyFilter(filters);

List<Dependency> dependencies = currentProject.getDependencies().stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList());

List<Dependency> managedDependencies = Optional.ofNullable(currentProject.getDependencyManagement())
.map(DependencyManagement::getDependencies)
.map(list -> list.stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList()))
.orElse(null);

CollectRequest collectRequest = new CollectRequest();
collectRequest.setManagedDependencies(managedDependencies);
collectRequest.setRepositories(currentProject.getRemoteProjectRepositories());
collectRequest.setDependencies(dependencies);
collectRequest.setRootArtifact(RepositoryUtils.toArtifact(currentProject.getArtifact()));

DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, dependencyFilter);

final DependencyResult dependencyResult =
this.repositorySystem.resolveDependencies(session.getRepositorySession(), dependencyRequest);

return dependencyResult.getArtifactResults().stream()
.map(ArtifactResult::getArtifact)
.map(RepositoryUtils::toArtifact)
.collect(Collectors.toSet());
} catch (DependencyResolutionException e) {
throw new EnforcerRuleError(e.getMessage(), e);
}
return artifacts;
}

private Collection<DependencyNode> collectProjectDependencies() throws DependencyCollectionException {

ArtifactTypeRegistry artifactTypeRegistry =
session.getRepositorySession().getArtifactTypeRegistry();

DependencyFilter optionalFilter = createOptionalFilter();
DependencyFilter scopeFilter = createScopeDependencyFilter();
DependencyFilter dependencyFilter = AndDependencyFilter.newInstance(optionalFilter, scopeFilter);

List<org.eclipse.aether.graph.Dependency> dependencies = session.getCurrentProject().getDependencies().stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.filter(d -> dependencyFilter == null
|| dependencyFilter.accept(new DefaultDependencyNode(d), Collections.emptyList()))
.collect(Collectors.toList());

List<Dependency> managedDependencies = Optional.ofNullable(
session.getCurrentProject().getDependencyManagement())
.map(DependencyManagement::getDependencies)
.map(list -> list.stream()
.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry))
.collect(Collectors.toList()))
.orElse(null);

CollectRequest collectRequest = new CollectRequest();
collectRequest.setManagedDependencies(managedDependencies);
collectRequest.setRepositories(session.getCurrentProject().getRemoteProjectRepositories());
collectRequest.setDependencies(dependencies);

CollectResult collectResult =
repositorySystem.collectDependencies(session.getRepositorySession(), collectRequest);

Set<DependencyNode> collectedDependencyNodes = new HashSet<>();
collectResult.getRoot().accept(new DependencyVisitor() {

int depth;

@Override
public boolean visitEnter(org.eclipse.aether.graph.DependencyNode node) {
if ((dependencyFilter == null || dependencyFilter.accept(node, Collections.emptyList()))
&& node.getArtifact() != null) {
collectedDependencyNodes.add(node);
}
depth++;
return searchTransitive || depth <= 1;
}

@Override
public boolean visitLeave(org.eclipse.aether.graph.DependencyNode node) {
depth--;
return true;
}
});

return collectedDependencyNodes;
}

private DependencyFilter createOptionalFilter() {
Expand Down Expand Up @@ -174,29 +146,6 @@ private DependencyFilter createScopeDependencyFilter() {
};
}

private Set<Artifact> resolveArtifacts(Collection<DependencyNode> dependencies) throws ArtifactResolutionException {

List<ArtifactRequest> requestArtifacts = dependencies.stream()
.map(d -> new ArtifactRequest()
.setDependencyNode(d)
.setRepositories(session.getCurrentProject().getRemoteProjectRepositories()))
.collect(Collectors.toList());

List<ArtifactResult> artifactResult =
repositorySystem.resolveArtifacts(session.getRepositorySession(), requestArtifacts);

return artifactResult.stream()
.map(result ->
result.getRequest().getDependencyNode().getDependency().setArtifact(result.getArtifact()))
.map(dependency -> {
Artifact artifact = RepositoryUtils.toArtifact(dependency.getArtifact());
artifact.setScope(dependency.getScope());
artifact.setOptional(dependency.getOptional());
return artifact;
})
.collect(Collectors.toSet());
}

/**
* Convert a wildcard into a regex.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
* LogParser for BanDuplicateClasses Enforcer Rule used for integration test verification by parsing the messages from the BanDuplicateClasses rule.
Expand All @@ -49,7 +49,7 @@ public BanDuplicateClassesLogParser(File logFile) {
* @throws IOException if the reader for the log file throws one
*/
public Map<Set<String>, Set<String>> parse() throws IOException {
Map<Set<String>, Set<String>> duplicates = new HashMap<>();
Map<Set<String>, Set<String>> duplicates = new LinkedHashMap<>();

try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
String line;
Expand All @@ -73,7 +73,7 @@ public Map<Set<String>, Set<String>> parse() throws IOException {
}

private static Set<String> readFoundInJars(BufferedReader reader) throws IOException {
Set<String> jars = new HashSet<>();
Set<String> jars = new TreeSet<>();
for (String line = reader.readLine();
line != null && !" Duplicate classes:".equals(line);
line = reader.readLine()) {
Expand All @@ -83,7 +83,7 @@ private static Set<String> readFoundInJars(BufferedReader reader) throws IOExcep
}

private static Set<String> readDuplicateClasses(BufferedReader reader) throws IOException {
Set<String> classes = new HashSet<>();
Set<String> classes = new TreeSet<>();
for (String line = reader.readLine(); line != null && line.length() > 0; line = reader.readLine()) {
classes.add(line.trim());
}
Expand Down