You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/10/25 15:03:21 UTC

[GitHub] [nifi-maven] exceptionfactory commented on a diff in pull request #23: NIFI-10457 Add a new goal that checks dependency duplications in nars

exceptionfactory commented on code in PR #23:
URL: https://github.com/apache/nifi-maven/pull/23#discussion_r1004604656


##########
src/main/java/org/apache/nifi/utils/Utils.java:
##########
@@ -0,0 +1,108 @@
+package org.apache.nifi.utils;

Review Comment:
   This class is missing a license header.



##########
src/main/java/org/apache/nifi/NarDuplicateDependenciesMojo.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuilder;
+import org.apache.maven.project.ProjectBuildingException;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilderException;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.apache.nifi.utils.Utils;
+import org.eclipse.aether.RepositorySystemSession;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+import java.util.HashMap;
+
+/**
+ * Generates a list of duplicate dependencies with compile scope in the nar.
+ */
+@Mojo(name = "duplicate-nar-dependencies", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME)
+public class NarDuplicateDependenciesMojo extends AbstractMojo {
+
+
+    private static final String NAR = "nar";

Review Comment:
   Can this be replaced with the value from `Utils.NAR`?



##########
src/main/java/org/apache/nifi/utils/Utils.java:
##########
@@ -0,0 +1,108 @@
+package org.apache.nifi.utils;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuilder;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingResult;
+import org.apache.maven.project.ProjectBuildingException;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Utils {

Review Comment:
   Recommend renaming this class to something more specific such as `NarDependencyUtils`.



##########
src/main/java/org/apache/nifi/NarDuplicateDependenciesMojo.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuilder;
+import org.apache.maven.project.ProjectBuildingException;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilderException;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.apache.nifi.utils.Utils;
+import org.eclipse.aether.RepositorySystemSession;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+import java.util.HashMap;
+
+/**
+ * Generates a list of duplicate dependencies with compile scope in the nar.
+ */
+@Mojo(name = "duplicate-nar-dependencies", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME)
+public class NarDuplicateDependenciesMojo extends AbstractMojo {
+
+
+    private static final String NAR = "nar";
+
+    /**
+     * The Maven project.
+     */
+    @Parameter(defaultValue = "${project}", readonly = true, required = true)
+    private MavenProject project;
+
+
+    /**
+     * The {@link RepositorySystemSession} used for obtaining the local and remote artifact repositories.
+     */
+    @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
+    private RepositorySystemSession repoSession;
+
+
+    /**
+     * The dependency tree builder to use for verbose output.
+     */
+    @Component
+    private DependencyCollectorBuilder dependencyCollectorBuilder;
+
+    /**
+     * *
+     * The {@link ArtifactHandlerManager} into which any extension {@link ArtifactHandler} instances should have been injected when the extensions were loaded.
+     */
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * The {@link ProjectBuilder} used to generate the {@link MavenProject} for the nar artifact the dependency tree is being generated for.
+     */
+    @Component
+    private ProjectBuilder projectBuilder;
+
+    /*
+     * @see org.apache.maven.plugin.Mojo#execute()
+     */
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        try {
+            Utils.ensureSingleNarDependencyExists(project);
+            // build the project for the nar artifact
+            final ProjectBuildingRequest narRequest = new DefaultProjectBuildingRequest();
+            narRequest.setRepositorySession(repoSession);
+            narRequest.setSystemProperties(System.getProperties());
+
+            artifactHandlerManager.addHandlers(Utils.createNarHandlerMap(narRequest, project, projectBuilder));
+
+            // get the dependency tree
+            final DependencyNode root = dependencyCollectorBuilder.collectDependencyGraph(narRequest, null);
+
+            DependencyNode narParent = root.getChildren()
+                    .stream()
+                    .filter(child -> NAR.equals(child.getArtifact().getType()))
+                    .findFirst()
+                    .orElseThrow(() -> new MojoExecutionException("Project does not have any NAR dependencies."));
+
+            getLog().info("Analyzing dependencies of " + narRequest.getProject().getFile().getPath());
+
+            // all compiled dependencies except inherited from parent
+            Map<String, List<Artifact>> directDependencies = new HashMap<>();
+
+            root.accept(new DependencyNodeVisitor() {
+                final Stack<Artifact> hierarchy = new Stack<>();
+
+                @Override
+                public boolean visit(DependencyNode node) {
+                    if (node == root) {
+                        return true;
+                    }
+                    Artifact artifact = node.getArtifact();
+                    hierarchy.push(artifact);
+                    if ("compile".equals(artifact.getScope()) && !NAR.equals(artifact.getType())) {
+                        directDependencies.put(artifact.toString(), new ArrayList<>(hierarchy));
+                        return true;
+                    }
+                    return false;
+                }
+
+                @Override
+                public boolean endVisit(DependencyNode node) {
+                    if (node != root) {
+                        hierarchy.pop();
+                    }
+                    return true;
+                }
+            });
+
+            List<String> errors = new ArrayList<>();
+
+            narParent.accept(new DependencyNodeVisitor() {
+                final Stack<Artifact> hierarchy = new Stack<>();
+
+                @Override
+                public boolean visit(DependencyNode node) {
+                    Artifact artifact = node.getArtifact();
+                    hierarchy.push(artifact);
+                    if ("compile".equals(artifact.getScope()) && directDependencies.containsKey(artifact.toString())) {
+                        StringBuilder sb = new StringBuilder()
+                                .append(artifact).append(" already included in the bundle\n")
+                                .append(root.getArtifact()).append(" (this nar)\n");
+                        List<Artifact> otherHierarchy = directDependencies.get(artifact.toString());
+                        // print other hierarchy
+                        for (int i = 0; i < otherHierarchy.size(); i++) {
+                            sb.append(indent(i)).append(otherHierarchy.get(i));
+                            // print the last artifact in the hierarchy
+                            if (i == otherHierarchy.size() - 1) {
+                                sb.append(" (duplicate)");
+                            }
+                            sb.append("\n");

Review Comment:
   Recommend replacing all references to `\n` with `System.lineSeparator()` for better handling across platforms.



##########
src/main/java/org/apache/nifi/NarDuplicateDependenciesMojo.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.ProjectBuilder;
+import org.apache.maven.project.ProjectBuildingException;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilderException;
+import org.apache.maven.shared.dependency.graph.DependencyGraphBuilder;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.apache.nifi.utils.Utils;
+import org.eclipse.aether.RepositorySystemSession;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Stack;
+import java.util.HashMap;
+
+/**
+ * Generates a list of duplicate dependencies with compile scope in the nar.
+ */
+@Mojo(name = "duplicate-nar-dependencies", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME)
+public class NarDuplicateDependenciesMojo extends AbstractMojo {
+
+
+    private static final String NAR = "nar";
+
+    /**
+     * The Maven project.
+     */
+    @Parameter(defaultValue = "${project}", readonly = true, required = true)
+    private MavenProject project;
+
+
+    /**
+     * The {@link RepositorySystemSession} used for obtaining the local and remote artifact repositories.
+     */
+    @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
+    private RepositorySystemSession repoSession;
+
+
+    /**
+     * The dependency tree builder to use for verbose output.
+     */
+    @Component
+    private DependencyCollectorBuilder dependencyCollectorBuilder;
+
+    /**
+     * *
+     * The {@link ArtifactHandlerManager} into which any extension {@link ArtifactHandler} instances should have been injected when the extensions were loaded.
+     */
+    @Component
+    private ArtifactHandlerManager artifactHandlerManager;
+
+    /**
+     * The {@link ProjectBuilder} used to generate the {@link MavenProject} for the nar artifact the dependency tree is being generated for.
+     */
+    @Component
+    private ProjectBuilder projectBuilder;
+
+    /*
+     * @see org.apache.maven.plugin.Mojo#execute()
+     */
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        try {
+            Utils.ensureSingleNarDependencyExists(project);
+            // build the project for the nar artifact
+            final ProjectBuildingRequest narRequest = new DefaultProjectBuildingRequest();
+            narRequest.setRepositorySession(repoSession);
+            narRequest.setSystemProperties(System.getProperties());
+
+            artifactHandlerManager.addHandlers(Utils.createNarHandlerMap(narRequest, project, projectBuilder));
+
+            // get the dependency tree
+            final DependencyNode root = dependencyCollectorBuilder.collectDependencyGraph(narRequest, null);
+
+            DependencyNode narParent = root.getChildren()
+                    .stream()
+                    .filter(child -> NAR.equals(child.getArtifact().getType()))
+                    .findFirst()
+                    .orElseThrow(() -> new MojoExecutionException("Project does not have any NAR dependencies."));
+
+            getLog().info("Analyzing dependencies of " + narRequest.getProject().getFile().getPath());
+
+            // all compiled dependencies except inherited from parent
+            Map<String, List<Artifact>> directDependencies = new HashMap<>();
+
+            root.accept(new DependencyNodeVisitor() {
+                final Stack<Artifact> hierarchy = new Stack<>();
+
+                @Override
+                public boolean visit(DependencyNode node) {
+                    if (node == root) {
+                        return true;
+                    }
+                    Artifact artifact = node.getArtifact();
+                    hierarchy.push(artifact);
+                    if ("compile".equals(artifact.getScope()) && !NAR.equals(artifact.getType())) {

Review Comment:
   References to the string `compile` should be replaced with a private static variable named something like `COMPILE_SCOPE`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org