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
Original file line number Diff line number Diff line change
Expand Up @@ -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<MavenPlugin> plugins = new TreeSet<>();
Set<MavenPlugin> plugins = new TreeSet<>(Comparator.comparing(MavenPlugin::getChecksum));
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change required/necessary?

if (metadata.getConfig().isIncludeMavenPlugins()) {
plugins = getAllPlugins(project, session, dependencyCollectorBuilder, checksumCalculator);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ private static Set<MavenPlugin> getAllPlugins(
MavenSession session,
DependencyCollectorBuilder dependencyCollectorBuilder,
AbstractChecksumCalculator checksumCalculator) {
Set<MavenPlugin> plugins = new TreeSet<>();
Set<MavenPlugin> plugins = new TreeSet<>(Comparator.comparing(MavenPlugin::getChecksum));
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto - is this required/necessary?

for (Artifact pluginArtifact : project.getPluginArtifacts()) {
RepositoryInformation repositoryInformation = checksumCalculator.getPluginResolvedField(pluginArtifact);
Set<io.github.chains_project.maven_lockfile.graph.DependencyNode> pluginDependencies =
Expand Down Expand Up @@ -296,19 +296,15 @@ private static Set<io.github.chains_project.maven_lockfile.graph.DependencyNode>
MutableGraph<DependencyNode> graph = GraphBuilder.directed().build();
rootNode.accept(new GraphBuildingNodeVisitor(graph));

PluginLogManager.getLog()
.debug(String.format(
"Built graph with %d nodes for plugin %s",
PluginLogManager.getLog().debug(String.format("Built graph with %d nodes for plugin %s",
graph.nodes().size(), pluginArtifact));

DependencyGraph dependencyGraph = DependencyGraph.of(graph, checksumCalculator, false);

// Ignore test-scoped dependencies for plugins
DependencyGraph dependencyGraph = DependencyGraph.of(graph, checksumCalculator,false, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth adding a comment here that we are excluding test scope.

Suggested change
DependencyGraph dependencyGraph = DependencyGraph.of(graph, checksumCalculator,false, true);
// Ignore test scoped dependencies for plugins.
DependencyGraph dependencyGraph = DependencyGraph.of(graph, checksumCalculator,false, true);

// Get root dependency nodes (excluding the plugin project itself)
Set<io.github.chains_project.maven_lockfile.graph.DependencyNode> 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);
Expand All @@ -335,10 +331,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);
}
}

Expand All @@ -364,10 +360,10 @@ private static Pom constructRecursivePom(
String relativePath = project.getFile() == null
? null
: initialProject
.getBasedir()
Copy link
Contributor

Choose a reason for hiding this comment

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

Unnecessary formatting change, please revert.

.toPath()
.relativize(project.getFile().toPath())
.toString();
.getBasedir()
.toPath()
.relativize(project.getFile().toPath())
.toString();
String checksum = null;
ResolvedUrl resolved = null;
RepositoryId repoId = null;
Expand Down Expand Up @@ -404,4 +400,4 @@ private static Pom constructRecursivePom(

return lastPom;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,19 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hashCode(graph);
}

public Optional<DependencyNode> getParentForNode(DependencyNode node) {
return graph.stream().filter(n -> n.id.equals(node.getParent())).findFirst();
}

public static DependencyGraph of(
MutableGraph<org.apache.maven.shared.dependency.graph.DependencyNode> graph,
AbstractChecksumCalculator calc,
boolean reduced) {
boolean reduced,boolean skipPluginTestScope) {
var roots = graph.nodes().stream()
.filter(it -> graph.predecessors(it).isEmpty())
.collect(Collectors.toList());
Set<DependencyNode> 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,skipPluginTestScope).ifPresent(nodes::add);
}
// maven dependency tree contains the project itself as a root node. We remove it here.
Set<DependencyNode> dependencyRoots = nodes.stream()
Expand All @@ -74,13 +72,12 @@ public static DependencyGraph of(
dependencyRoots.forEach(v -> v.setParent(null));
return new DependencyGraph(dependencyRoots);
}

private static Optional<DependencyNode> createDependencyNode(
org.apache.maven.shared.dependency.graph.DependencyNode node,
Graph<org.apache.maven.shared.dependency.graph.DependencyNode> graph,
AbstractChecksumCalculator calc,
boolean isRoot,
boolean reduce) {
boolean reduce,boolean skipPluginTestScope) {
PluginLogManager.getLog()
.debug(String.format("Creating dependency node for: %s, root: %s", node.toNodeString(), isRoot));
var groupId = GroupId.of(node.getArtifact().getGroupId());
Expand All @@ -90,6 +87,11 @@ private static Optional<DependencyNode> 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 (skipPluginTestScope && !isRoot && scope == MavenScope.TEST) {
PluginLogManager.getLog().debug(String.format("Skipping test-scoped plugin dependency %s:%s:%s",
node.getArtifact().getGroupId(),node.getArtifact().getArtifactId(),node.getArtifact().getVersion()));
return Optional.empty();
}
PluginLogManager.getLog().debug(String.format("Resolving repository information for %s", node.toNodeString()));
var repositoryInformation =
isRoot ? RepositoryInformation.Unresolved() : calc.getArtifactResolvedField(node.getArtifact());
Expand All @@ -113,8 +115,8 @@ private static Optional<DependencyNode> 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,skipPluginTestScope).ifPresent(value::addChild);
}
return Optional.of(value);
}
}
}