diff --git a/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/LockFileFacade.java b/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/LockFileFacade.java index 36aed9daa..625b7a709 100644 --- a/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/LockFileFacade.java +++ b/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/LockFileFacade.java @@ -96,7 +96,7 @@ public static LockFile generateLockFileFromProject( AbstractChecksumCalculator checksumCalculator, MetaData metadata) { PluginLogManager.getLog().info(String.format("Generating lock file for project %s", project.getArtifactId())); - Set plugins = new TreeSet<>(); + Set plugins = new TreeSet<>(Comparator.comparing(MavenPlugin::getChecksum)); if (metadata.getConfig().isIncludeMavenPlugins()) { plugins = getAllPlugins(project, session, dependencyCollectorBuilder, checksumCalculator); } @@ -127,7 +127,7 @@ private static Set getAllPlugins( MavenSession session, DependencyCollectorBuilder dependencyCollectorBuilder, AbstractChecksumCalculator checksumCalculator) { - Set plugins = new TreeSet<>(); + Set plugins = new TreeSet<>(Comparator.comparing(MavenPlugin::getChecksum)); for (Artifact pluginArtifact : project.getPluginArtifacts()) { RepositoryInformation repositoryInformation = checksumCalculator.getPluginResolvedField(pluginArtifact); Set pluginDependencies = @@ -301,14 +301,13 @@ private static Set "Built graph with %d nodes for plugin %s", graph.nodes().size(), pluginArtifact)); - DependencyGraph dependencyGraph = DependencyGraph.of(graph, checksumCalculator, false); - + DependencyGraph dependencyGraph = DependencyGraph.of(graph, checksumCalculator,false, true); // Get root dependency nodes (excluding the plugin project itself) - Set roots = dependencyGraph.getRoots(); + Set roots = + dependencyGraph.getRoots(); PluginLogManager.getLog() .info(String.format("Resolved %4d dependencies for plugin %s", roots.size(), pluginArtifact)); return roots; - } catch (Exception e) { PluginLogManager.getLog() .warn(String.format("Could not resolve dependencies for plugin %s", pluginArtifact), e); @@ -335,10 +334,10 @@ private static DependencyGraph graph( .info(String.format( "Resolved %4d dependencies for project %s", graph.nodes().size(), project)); - return DependencyGraph.of(graph, checksumCalculator, reduced); + return DependencyGraph.of(graph, checksumCalculator, reduced,false); } catch (Exception e) { PluginLogManager.getLog().warn("Could not generate graph", e); - return DependencyGraph.of(GraphBuilder.directed().build(), checksumCalculator, reduced); + return DependencyGraph.of(GraphBuilder.directed().build(), checksumCalculator, reduced,false); } } @@ -364,10 +363,10 @@ private static Pom constructRecursivePom( String relativePath = project.getFile() == null ? null : initialProject - .getBasedir() - .toPath() - .relativize(project.getFile().toPath()) - .toString(); + .getBasedir() + .toPath() + .relativize(project.getFile().toPath()) + .toString(); String checksum = null; ResolvedUrl resolved = null; RepositoryId repoId = null; @@ -404,4 +403,4 @@ private static Pom constructRecursivePom( return lastPom; } -} +} \ No newline at end of file diff --git a/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyGraph.java b/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyGraph.java index fd6f184a1..992d3c1d3 100644 --- a/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyGraph.java +++ b/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/graph/DependencyGraph.java @@ -58,13 +58,13 @@ public Optional getParentForNode(DependencyNode node) { public static DependencyGraph of( MutableGraph graph, AbstractChecksumCalculator calc, - boolean reduced) { + boolean reduced,boolean skipTestScope) { var roots = graph.nodes().stream() .filter(it -> graph.predecessors(it).isEmpty()) .collect(Collectors.toList()); Set nodes = new TreeSet<>(Comparator.comparing(DependencyNode::getComparatorString)); for (var artifact : roots) { - createDependencyNode(artifact, graph, calc, true, reduced).ifPresent(nodes::add); + createDependencyNode(artifact, graph, calc, true, reduced,skipTestScope).ifPresent(nodes::add); } // maven dependency tree contains the project itself as a root node. We remove it here. Set dependencyRoots = nodes.stream() @@ -80,7 +80,7 @@ private static Optional createDependencyNode( Graph graph, AbstractChecksumCalculator calc, boolean isRoot, - boolean reduce) { + boolean reduce,boolean skipTestScope) { PluginLogManager.getLog() .debug(String.format("Creating dependency node for: %s, root: %s", node.toNodeString(), isRoot)); var groupId = GroupId.of(node.getArtifact().getGroupId()); @@ -90,6 +90,11 @@ private static Optional createDependencyNode( PluginLogManager.getLog().debug(String.format("Calculating checksum for %s", node.toNodeString())); var checksum = isRoot ? "" : calc.calculateArtifactChecksum(node.getArtifact()); var scope = MavenScope.fromString(node.getArtifact().getScope()); + if (skipPluginTestDependency(node, isRoot, skipTestScope)) { + PluginLogManager.getLog().debug( + "Skipping plugin test-scope dependency: " + node.toNodeString()); + return Optional.empty(); + } PluginLogManager.getLog().debug(String.format("Resolving repository information for %s", node.toNodeString())); var repositoryInformation = isRoot ? RepositoryInformation.Unresolved() : calc.getArtifactResolvedField(node.getArtifact()); @@ -113,8 +118,31 @@ private static Optional createDependencyNode( value.setSelectedVersion(baseVersion); value.setIncluded(included); for (var artifact : graph.successors(node)) { - createDependencyNode(artifact, graph, calc, false, reduce).ifPresent(value::addChild); + createDependencyNode(artifact, graph, calc, false, reduce,skipTestScope).ifPresent(value::addChild); } return Optional.of(value); } -} + private static boolean skipPluginTestDependency( + org.apache.maven.shared.dependency.graph.DependencyNode node, + boolean isRoot, + boolean skipTestScope) { + if (!skipTestScope) { + return false; + } + if (isRoot) { + return false; + } + boolean isTest = "test".equals(node.getArtifact().getScope()); + if (isTest) { + PluginLogManager.getLog().debug( + String.format( + "Skipping test-scoped plugin dependency %s:%s:%s", + node.getArtifact().getGroupId(), + node.getArtifact().getArtifactId(), + node.getArtifact().getVersion() + ) + ); + } + return isTest; + } +} \ No newline at end of file