diff --git a/pom.xml b/pom.xml index e56e137..ffbe45b 100644 --- a/pom.xml +++ b/pom.xml @@ -109,6 +109,12 @@ 4.11.0 test + + org.assertj + assertj-core + 3.26.0 + test + diff --git a/src/it/enforce-bytecode-version-with-banned-deps/verify.groovy b/src/it/enforce-bytecode-version-with-banned-deps/verify.groovy index af58127..4e88bf9 100644 --- a/src/it/enforce-bytecode-version-with-banned-deps/verify.groovy +++ b/src/it/enforce-bytecode-version-with-banned-deps/verify.groovy @@ -1,3 +1,5 @@ +import org.assertj.core.api.Assertions; + File file = new File( basedir, "build.log" ); assert file.exists(); @@ -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; diff --git a/src/it/local-project-repo/verify.groovy b/src/it/local-project-repo/verify.groovy index cc3585a..ce59476 100644 --- a/src/it/local-project-repo/verify.groovy +++ b/src/it/local-project-repo/verify.groovy @@ -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; diff --git a/src/it/mojo-1744/verify.groovy b/src/it/mojo-1744/verify.groovy index d016ff0..21e6094 100644 --- a/src/it/mojo-1744/verify.groovy +++ b/src/it/mojo-1744/verify.groovy @@ -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) -] \ No newline at end of file + "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 \ No newline at end of file diff --git a/src/main/java/org/codehaus/mojo/extraenforcer/dependencies/AbstractResolveDependencies.java b/src/main/java/org/codehaus/mojo/extraenforcer/dependencies/AbstractResolveDependencies.java index 1bbbfdc..05ccafb 100644 --- a/src/main/java/org/codehaus/mojo/extraenforcer/dependencies/AbstractResolveDependencies.java +++ b/src/main/java/org/codehaus/mojo/extraenforcer/dependencies/AbstractResolveDependencies.java @@ -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; @@ -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; @@ -79,70 +74,47 @@ public void execute() throws EnforcerRuleException { protected abstract void handleArtifacts(Set artifacts) throws EnforcerRuleException; private Set getDependenciesToCheck() throws EnforcerRuleException { - Set artifacts = null; try { - Collection dependencies = collectProjectDependencies(); - artifacts = resolveArtifacts(dependencies); - } catch (DependencyCollectionException | ArtifactResolutionException e) { + ArtifactTypeRegistry artifactTypeRegistry = + session.getRepositorySession().getArtifactTypeRegistry(); + + final MavenProject currentProject = session.getCurrentProject(); + + final List 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 dependencies = currentProject.getDependencies().stream() + .map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) + .collect(Collectors.toList()); + + List 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 collectProjectDependencies() throws DependencyCollectionException { - - ArtifactTypeRegistry artifactTypeRegistry = - session.getRepositorySession().getArtifactTypeRegistry(); - - DependencyFilter optionalFilter = createOptionalFilter(); - DependencyFilter scopeFilter = createScopeDependencyFilter(); - DependencyFilter dependencyFilter = AndDependencyFilter.newInstance(optionalFilter, scopeFilter); - - List 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 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 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() { @@ -174,29 +146,6 @@ private DependencyFilter createScopeDependencyFilter() { }; } - private Set resolveArtifacts(Collection dependencies) throws ArtifactResolutionException { - - List requestArtifacts = dependencies.stream() - .map(d -> new ArtifactRequest() - .setDependencyNode(d) - .setRepositories(session.getCurrentProject().getRemoteProjectRepositories())) - .collect(Collectors.toList()); - - List 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. * diff --git a/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java b/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java index 01d9d07..415b39e 100644 --- a/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java +++ b/src/test/java/org/codehaus/mojo/extraenforcer/it/BanDuplicateClassesLogParser.java @@ -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. @@ -49,7 +49,7 @@ public BanDuplicateClassesLogParser(File logFile) { * @throws IOException if the reader for the log file throws one */ public Map, Set> parse() throws IOException { - Map, Set> duplicates = new HashMap<>(); + Map, Set> duplicates = new LinkedHashMap<>(); try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) { String line; @@ -73,7 +73,7 @@ public Map, Set> parse() throws IOException { } private static Set readFoundInJars(BufferedReader reader) throws IOException { - Set jars = new HashSet<>(); + Set jars = new TreeSet<>(); for (String line = reader.readLine(); line != null && !" Duplicate classes:".equals(line); line = reader.readLine()) { @@ -83,7 +83,7 @@ private static Set readFoundInJars(BufferedReader reader) throws IOExcep } private static Set readDuplicateClasses(BufferedReader reader) throws IOException { - Set classes = new HashSet<>(); + Set classes = new TreeSet<>(); for (String line = reader.readLine(); line != null && line.length() > 0; line = reader.readLine()) { classes.add(line.trim()); }