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

[NETBEANS-6519] Maven dependency produces full tree with duplicates, avoids cycles. #4947

Merged
merged 2 commits into from
Nov 11, 2022
Merged
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 @@ -207,7 +207,7 @@ public boolean accept(Scope s, ArtifactSpec a) {
};

return new MavenDependencyResult(nbMavenProject.getMavenProject(),
convert2(n, compositeFiter, broken), new ArrayList<>(scopes), broken,
convertDependencies(n, compositeFiter, broken), new ArrayList<>(scopes), broken,
project, nbMavenProject);
}

Expand Down Expand Up @@ -236,10 +236,57 @@ static Scope scope(Artifact a) {
}
}

private Dependency convert2(org.apache.maven.shared.dependency.tree.DependencyNode n, Dependency.Filter filter, Set<ArtifactSpec> broken) {
List<Dependency> ch = new ArrayList<>();
private static String getFullArtifactId(Artifact a) {
if (a.getDependencyTrail() == null) {
return "/" + a.getId();
} else {
return String.join("/", a.getDependencyTrail()) + "/" + a.getId(); // NOI18N
}
}

private void findRealNodes(org.apache.maven.shared.dependency.tree.DependencyNode n, Map<String, List<org.apache.maven.shared.dependency.tree.DependencyNode>> result) {
if (n.getArtifact() == null) {
return;
}
Artifact a = n.getArtifact();
if (n.getState() != org.apache.maven.shared.dependency.tree.DependencyNode.INCLUDED) {
return;
}
// register (if not present) using plain artifact ID, but also using the full path, which will be preferred for the lookup.
result.putIfAbsent(a.getId(), n.getChildren());
result.put(getFullArtifactId(a), n.getChildren());

for (org.apache.maven.shared.dependency.tree.DependencyNode c : n.getChildren()) {
Dependency cd = convert2(c, filter, broken);
findRealNodes(c, result);
}
}

private Dependency convertDependencies(org.apache.maven.shared.dependency.tree.DependencyNode n, Dependency.Filter filter, Set<ArtifactSpec> broken) {
Map<String, List<org.apache.maven.shared.dependency.tree.DependencyNode>> realNodes = new HashMap<>();
findRealNodes(n, realNodes);
return convert2(n, filter, realNodes, broken);
}

private Dependency convert2(org.apache.maven.shared.dependency.tree.DependencyNode n, Dependency.Filter filter, Map<String, List<org.apache.maven.shared.dependency.tree.DependencyNode>> realNodes, Set<ArtifactSpec> broken) {
List<Dependency> ch = new ArrayList<>();

List<org.apache.maven.shared.dependency.tree.DependencyNode> children = null;

if (n.getState() == org.apache.maven.shared.dependency.tree.DependencyNode.OMITTED_FOR_CONFLICT ||
n.getState() == org.apache.maven.shared.dependency.tree.DependencyNode.OMITTED_FOR_DUPLICATE) {
// attempt to find / copy the children subtree, [refer full artifact path.
if (n.getRelatedArtifact() != null) {
children = realNodes.get(getFullArtifactId(n.getRelatedArtifact()));
}
if (children == null) {
children = realNodes.getOrDefault(n.getArtifact().getId(), n.getChildren());
}
} else {
children = n.getChildren();
}

for (org.apache.maven.shared.dependency.tree.DependencyNode c : children) {
Dependency cd = convert2(c, filter, realNodes, broken);
if (cd != null) {
ch.add(cd);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Sample of a real-world project
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.1</version>
<packaging>${packaging}</packaging>

<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>3.7.0</version>
</parent>

<properties>
<packaging>jar</packaging>
<jdk.version>8</jdk.version>
<release.version>8</release.version>
<micronaut.version>3.7.0</micronaut.version>
<micronaut.runtime>netty</micronaut.runtime>
<exec.mainClass>com.example.Application</exec.mainClass>
</properties>

<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->

<annotationProcessorPaths combine.children="append">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=com.example</arg>
<arg>-Amicronaut.processing.module=demo</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example;

import io.micronaut.runtime.Micronaut;

public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example;

import io.micronaut.runtime.EmbeddedApplication;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

import jakarta.inject.Inject;

@MicronautTest
class DemoTest {

@Inject
EmbeddedApplication<?> application;

@Test
void testItWorks() {
Assertions.assertTrue(application.isRunning());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[ ] nbtest.grp:test-app:12.6[jar] / compilation
+-- [ ] nbtest.grp:annotation:12.6[jar] / compilation
+-- [ ] nbtest.grp:test-lib:13[jar] / compilation
| +-- [ ] nbtest.grp:annotation:12.6[jar] / compilation
| +-- [ ] javax.annotation:javax.annotation-api:1.3.2[jar] / compilation
| +-- [ ] nbtest.grp:annotation:12.6[jar] / compilation
| +-- [ ] org.slf4j:slf4j-api:1.7.36[jar] / compilation
Loading