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

[MSHARED-1454] Expose ConflictData on DependencyNode #50

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

LogFlames
Copy link

Following this checklist to help us incorporate your
contribution quickly and easily:

  • Make sure there is a JIRA issue filed
    for the change (usually before you start working on it). Trivial changes like typos do not
    require a JIRA issue. Your pull request should address just this issue, without
    pulling in other changes.
  • Each commit in the pull request should have a meaningful subject line and body.
  • Format the pull request title like [MSHARED-XXX] - Fixes bug in ApproximateQuantiles,
    where you replace MSHARED-XXX with the appropriate JIRA issue. Best practice
    is to use the JIRA issue title in the pull request title and in the first line of the
    commit message.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Run mvn clean verify to make sure basic checks pass. A more thorough check will
    be performed on your pull request automatically.

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.


In maven-dependency-plugin there are multiple export formats for the dependency tree, these include Json, DOT, GraphML etc. When creating the normal tree all information is there to format it nicely as e.g. Json. However, when running mvn dependency:tree -Dverbose, all nodes are included (as they are VerboseDependencyNode) but since VerboseDependencyNode is internal in maven-dependency-tree there is no (clean) way to include information of which nodes are included or not (see https://issues.apache.org/jira/browse/MDEP-962).

The only current way to include this information would be to parse the toNodeString() output (where excluded nodes are wrapped in parenthesis).

I propose to expose ConflictData on the DependencyNode and make it null for DefaultDependencyNode to allow for more detailed information in the machine-readable formats.

@LogFlames
Copy link
Author

Would this be a feasible change to enable verbose logging output in maven-dependency-plugin? If it is preferable I could create a PR to maven-dependency-plugin that is dependent on this one to have all code-changes in context.

@elharo wdyt?

Copy link
Contributor

@elharo elharo left a comment

Choose a reason for hiding this comment

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

Needs tests.

At first read, I don't understand what this PR is doing or why

@slawekjaranowski
Copy link
Member

I would like to not use this library in dependency plugin .... https://issues.apache.org/jira/browse/MDEP-969

@LogFlames
Copy link
Author

LogFlames commented Dec 10, 2024

I will add tests 👍

This PR would be a prerequisite for https://issues.apache.org/jira/browse/MDEP-962. Currently if running mvn dependency:tree -Dverbose=true it outputs:

[INFO] --- dependency:3.6.1:tree (default-cli) @ attackvector ---
[INFO] org.evil:attackvector:jar:1.2
[INFO] +- junit:junit:jar:4.11:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.postgresql:postgresql:jar:42.6.0:compile
[INFO] |  \- (org.checkerframework:checker-qual:jar:3.31.0:runtime - omitted for duplicate)
[INFO] +- org.springframework:spring-jdbc:jar:5.3.27:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.3.27:compile
[INFO] |  |  \- (org.springframework:spring-core:jar:5.3.27:compile - omitted for duplicate)
[INFO] |  +- org.springframework:spring-core:jar:5.3.27:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.3.27:compile
[INFO] |  \- org.springframework:spring-tx:jar:5.3.27:compile
[INFO] |     +- (org.springframework:spring-beans:jar:5.3.27:compile - omitted for duplicate)
[INFO] |     \- (org.springframework:spring-core:jar:5.3.27:compile - omitted for duplicate)
[...]

This includes dependencies which were omitted in parenthesis and information such as omitted for duplicate, etc

The corresponding JSON output (mvn dependency:tree -Dverbose=true -DoutputType=json) looks like this:

 --- dependency:3.7.0:tree (default-cli) @ attackvector ---
[INFO] {
[INFO]   "groupId": "org.evil",
[INFO]   "artifactId": "attackvector",
[INFO]   "version": "1.2",
[INFO]   "type": "jar",
[INFO]   "scope": "",
[INFO]   "classifier": "",
[INFO]   "optional": "false",
[INFO]   "children": [
[INFO]     {
[INFO]       "groupId": "junit",
[INFO]       "artifactId": "junit",
[INFO]       "version": "4.11",
[INFO]       "type": "jar",
[INFO]       "scope": "test",
[INFO]       "classifier": "",
[INFO]       "optional": "false",
[INFO]       "children": [
[INFO]         {
[INFO]           "groupId": "org.hamcrest",
[INFO]           "artifactId": "hamcrest-core",
[INFO]           "version": "1.3",
[INFO]           "type": "jar",
[INFO]           "scope": "test",
[INFO]           "classifier": "",
[INFO]           "optional": "false"
[INFO]         }
[INFO]         ]
[INFO]     },
[INFO]     {
[INFO]       "groupId": "org.postgresql",
[INFO]       "artifactId": "postgresql",
[INFO]       "version": "42.6.0",
[INFO]       "type": "jar",
[INFO]       "scope": "compile",
[INFO]       "classifier": "",
[INFO]       "optional": "false",
[INFO]       "children": [
[INFO]         {
[INFO]           "groupId": "org.checkerframework",
[INFO]           "artifactId": "checker-qual",
[INFO]           "version": "3.31.0",
[INFO]           "type": "jar",
[INFO]           "scope": "runtime",
[INFO]           "classifier": "",
[INFO]           "optional": "false"
[INFO]         }
[INFO]         ]
[INFO]     },
[INFO]     {
[INFO]       "groupId": "org.springframework",
[INFO]       "artifactId": "spring-jdbc",
[INFO]       "version": "5.3.27",
[INFO]       "type": "jar",
[INFO]       "scope": "compile",
[INFO]       "classifier": "",
[INFO]       "optional": "false",
[INFO]       "children": [
[INFO]         {
[INFO]           "groupId": "org.springframework",
[INFO]           "artifactId": "spring-beans",
[INFO]           "version": "5.3.27",
[INFO]           "type": "jar",
[INFO]           "scope": "compile",
[INFO]           "classifier": "",
[INFO]           "optional": "false",
[INFO]           "children": [
[INFO]             {
[INFO]               "groupId": "org.springframework",
[INFO]               "artifactId": "spring-core",
[INFO]               "version": "5.3.27",
[INFO]               "type": "jar",
[INFO]               "scope": "compile",
[INFO]               "classifier": "",
[INFO]               "optional": "false"
[INFO]             }
[INFO]             ]
[INFO]         },
[INFO]         {
[INFO]           "groupId": "org.springframework",
[INFO]           "artifactId": "spring-core",
[INFO]           "version": "5.3.27",
[INFO]           "type": "jar",
[INFO]           "scope": "compile",
[INFO]           "classifier": "",
[INFO]           "optional": "false",
[INFO]           "children": [
[INFO]             {
[INFO]               "groupId": "org.springframework",
[INFO]               "artifactId": "spring-jcl",
[INFO]               "version": "5.3.27",
[INFO]               "type": "jar",
[INFO]               "scope": "compile",
[INFO]               "classifier": "",
[INFO]               "optional": "false"
[INFO]             }
[INFO]             ]
[INFO]         },
[INFO]         {
[INFO]           "groupId": "org.springframework",
[INFO]           "artifactId": "spring-tx",
[INFO]           "version": "5.3.27",
[INFO]           "type": "jar",
[INFO]           "scope": "compile",
[INFO]           "classifier": "",
[INFO]           "optional": "false",
[INFO]           "children": [
[INFO]             {
[INFO]               "groupId": "org.springframework",
[INFO]               "artifactId": "spring-beans",
[INFO]               "version": "5.3.27",
[INFO]               "type": "jar",
[INFO]               "scope": "compile",
[INFO]               "classifier": "",
[INFO]               "optional": "false"
[INFO]             },
[INFO]             {
[INFO]               "groupId": "org.springframework",
[INFO]               "artifactId": "spring-core",
[INFO]               "version": "5.3.27",
[INFO]               "type": "jar",
[INFO]               "scope": "compile",
[INFO]               "classifier": "",
[INFO]               "optional": "false"
[INFO]             }
[INFO]             ]
[INFO]         }
[INFO]         ]
[INFO]     },
[...]

The argument -Dverbose=true causes maven-dependency-tree it to return VerboseDependencyNodes instead of DefaultDependencyNodes, but important information such as which are omitted, is missing. This is however currently not exposed from the VerboseDependencyNode as these are internal, other than the function toNodeString, where the formatting with parethesis and omitted reason are included.

My goal with this change would be to construct more useful JSON programmatically, where more key-value pairs could be added: "included": true/false, "omitted_reason": ..., without having to parse the toNodeString for example. It would allow the JSON to be constructed with (https://github.com/apache/maven-dependency-plugin/blob/ae19d893bb61a4b8414877721ca25b580c454dad/src/main/java/org/apache/maven/plugins/dependency/tree/JsonDependencyNodeVisitor.java#L123)

    /**
     * Appends the artifact values to the string builder.
     *
     * @param sb  the string builder to append to
     * @param indent  the current indent level
     * @param artifact  the artifact to write
     * @param hasChildren  true if the artifact has children
     */
-    private void appendNodeValues(StringBuilder sb, int indent, Artifact artifact, boolean hasChildren) {
+    private void appendNodeValues(StringBuilder sb, int indent, Artifact artifact, DependencyNode node, boolean hasChildren) {
         appendKeyValue(sb, indent, "groupId", artifact.getGroupId());
         appendKeyValue(sb, indent, "artifactId", artifact.getArtifactId());
         appendKeyValue(sb, indent, "version", artifact.getVersion());
         appendKeyValue(sb, indent, "type", artifact.getType());
         appendKeyValue(sb, indent, "scope", artifact.getScope());
         appendKeyValue(sb, indent, "classifier", artifact.getClassifier());
+        if (node.getGetConflictData() != null) {
+            ConflictData conflictData = node.getGetConflictData();
+            appendKeyValue(sb, indent, "included", conflictData.getWinnerVersion() == null);
+            if (conflictData.getOriginalVersion() != null) {
+                appendKeyValue(sb, indent, "version_updated_from", conflictData.getOriginalVersion());
+            }
+            if (conflictData.getIgnoredScope() != null) {
+                appendKeyValue(sb, indent, "scope_not_updated_to", conflictData.getIgnoredScope());
+            }
+        }
[...]

Using the resolver API directly would be a nice way to solve this as well, as ConflictData is a subset of the Aether's DependencyNode.getData(), which seems to be what the resolver API works with.

@LogFlames
Copy link
Author

LogFlames commented Jan 8, 2025

Happy new year!

Have you had time to look at the comments above? Is it still unclear?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants