[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056822446 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DependencyConvergence.java: ## @@ -51,58 +46,41 @@ public class DependencyConvergence implements EnforcerRule { private List excludes; +private DependencyVersionMap dependencyVersionMap; + public void setUniqueVersions(boolean uniqueVersions) { this.uniqueVersions = uniqueVersions; } -// CHECKSTYLE_OFF: LineLength -/** - * Uses the {@link EnforcerRuleHelper} to populate the values of the - * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} - * factory method. - * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do. - * - * @param helper - * @return a Dependency Node which is the root of the project's dependency tree - * @throws EnforcerRuleException - */ -// CHECKSTYLE_ON: LineLength -private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { -try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); -DependencyCollectorBuilder dependencyCollectorBuilder = -helper.getComponent(DependencyCollectorBuilder.class); -ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); -buildingRequest.setLocalRepository(repository); -ArtifactFilter filter = (Artifact a) -> -("compile".equalsIgnoreCase(a.getScope()) || "runtime".equalsIgnoreCase(a.getScope())) -&& !a.isOptional(); - -return dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); -} catch (ExpressionEvaluationException | ComponentLookupException e) { -throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); -} catch (DependencyCollectorBuilderException e) { -throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e); -} -} - @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { -DependencyNode node = getNode(helper); -DependencyVersionMap visitor = new DependencyVersionMap(log); -visitor.setUniqueVersions(uniqueVersions); -node.accept(visitor); -List errorMsgs = new ArrayList<>(); - errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes))); +DependencyNode node = ArtifactUtils.resolveTransitiveDependencies( +helper, +// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes +new DependencySelector() { +@Override +public boolean selectDependency(Dependency dependency) { +// regular OptionalDependencySelector only discriminates optional dependencies at level 2+ +return !dependency.isOptional() +// regular ScopeDependencySelector is case-sensitive +&& !dependency.getScope().equalsIgnoreCase(Artifact.SCOPE_TEST); +} + +@Override +public DependencySelector deriveChildSelector(DependencyCollectionContext context) { +return this; +} +}, +new ExclusionDependencySelector()); Review Comment: @slawekjaranowski unfortunately, out of the box selectors only work on transitive dependencies: direct dependencies are always included. That was the primary reason I used this, I'll put a comment about it. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056807536 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DependencyConvergence.java: ## @@ -51,58 +46,41 @@ public class DependencyConvergence implements EnforcerRule { private List excludes; +private DependencyVersionMap dependencyVersionMap; + public void setUniqueVersions(boolean uniqueVersions) { this.uniqueVersions = uniqueVersions; } -// CHECKSTYLE_OFF: LineLength -/** - * Uses the {@link EnforcerRuleHelper} to populate the values of the - * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} - * factory method. - * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do. - * - * @param helper - * @return a Dependency Node which is the root of the project's dependency tree - * @throws EnforcerRuleException - */ -// CHECKSTYLE_ON: LineLength -private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { -try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); -DependencyCollectorBuilder dependencyCollectorBuilder = -helper.getComponent(DependencyCollectorBuilder.class); -ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); -buildingRequest.setLocalRepository(repository); -ArtifactFilter filter = (Artifact a) -> -("compile".equalsIgnoreCase(a.getScope()) || "runtime".equalsIgnoreCase(a.getScope())) -&& !a.isOptional(); - -return dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); -} catch (ExpressionEvaluationException | ComponentLookupException e) { -throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); -} catch (DependencyCollectorBuilderException e) { -throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e); -} -} - @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { -DependencyNode node = getNode(helper); -DependencyVersionMap visitor = new DependencyVersionMap(log); -visitor.setUniqueVersions(uniqueVersions); -node.accept(visitor); -List errorMsgs = new ArrayList<>(); - errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes))); +DependencyNode node = ArtifactUtils.resolveTransitiveDependencies( +helper, +// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes +new DependencySelector() { +@Override +public boolean selectDependency(Dependency dependency) { +// regular OptionalDependencySelector only discriminates optional dependencies at level 2+ +return !dependency.isOptional() +// regular ScopeDependencySelector is case-sensitive +&& !dependency.getScope().equalsIgnoreCase(Artifact.SCOPE_TEST); +} + +@Override +public DependencySelector deriveChildSelector(DependencyCollectionContext context) { +return this; +} +}, +new ExclusionDependencySelector()); Review Comment: will check why -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056806899 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DependencyConvergence.java: ## @@ -51,58 +46,41 @@ public class DependencyConvergence implements EnforcerRule { private List excludes; +private DependencyVersionMap dependencyVersionMap; + public void setUniqueVersions(boolean uniqueVersions) { this.uniqueVersions = uniqueVersions; } -// CHECKSTYLE_OFF: LineLength -/** - * Uses the {@link EnforcerRuleHelper} to populate the values of the - * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} - * factory method. - * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do. - * - * @param helper - * @return a Dependency Node which is the root of the project's dependency tree - * @throws EnforcerRuleException - */ -// CHECKSTYLE_ON: LineLength -private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { -try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); -DependencyCollectorBuilder dependencyCollectorBuilder = -helper.getComponent(DependencyCollectorBuilder.class); -ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); -buildingRequest.setLocalRepository(repository); -ArtifactFilter filter = (Artifact a) -> -("compile".equalsIgnoreCase(a.getScope()) || "runtime".equalsIgnoreCase(a.getScope())) -&& !a.isOptional(); - -return dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); -} catch (ExpressionEvaluationException | ComponentLookupException e) { -throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); -} catch (DependencyCollectorBuilderException e) { -throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e); -} -} - @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { -DependencyNode node = getNode(helper); -DependencyVersionMap visitor = new DependencyVersionMap(log); -visitor.setUniqueVersions(uniqueVersions); -node.accept(visitor); -List errorMsgs = new ArrayList<>(); - errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes))); +DependencyNode node = ArtifactUtils.resolveTransitiveDependencies( +helper, +// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes +new DependencySelector() { +@Override +public boolean selectDependency(Dependency dependency) { +// regular OptionalDependencySelector only discriminates optional dependencies at level 2+ +return !dependency.isOptional() +// regular ScopeDependencySelector is case-sensitive +&& !dependency.getScope().equalsIgnoreCase(Artifact.SCOPE_TEST); +} + +@Override +public DependencySelector deriveChildSelector(DependencyCollectionContext context) { +return this; +} +}, +new ExclusionDependencySelector()); Review Comment: Nope: [INFO] The build exited with code 1. See /Users/ajarmoniuk/IdeaProjects/maven-enforcer/maven-enforcer-plugin/target/it/dependencies_not_converging_test_scope/build.log for details. [INFO] dependencies_not_converging_test_scope/pom.xml ... FAILED (2.0 s) -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056806120 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DependencyConvergence.java: ## @@ -51,58 +46,41 @@ public class DependencyConvergence implements EnforcerRule { private List excludes; +private DependencyVersionMap dependencyVersionMap; + public void setUniqueVersions(boolean uniqueVersions) { this.uniqueVersions = uniqueVersions; } -// CHECKSTYLE_OFF: LineLength -/** - * Uses the {@link EnforcerRuleHelper} to populate the values of the - * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} - * factory method. - * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do. - * - * @param helper - * @return a Dependency Node which is the root of the project's dependency tree - * @throws EnforcerRuleException - */ -// CHECKSTYLE_ON: LineLength -private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { -try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); -DependencyCollectorBuilder dependencyCollectorBuilder = -helper.getComponent(DependencyCollectorBuilder.class); -ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); -buildingRequest.setLocalRepository(repository); -ArtifactFilter filter = (Artifact a) -> -("compile".equalsIgnoreCase(a.getScope()) || "runtime".equalsIgnoreCase(a.getScope())) -&& !a.isOptional(); - -return dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); -} catch (ExpressionEvaluationException | ComponentLookupException e) { -throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); -} catch (DependencyCollectorBuilderException e) { -throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e); -} -} - @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { -DependencyNode node = getNode(helper); -DependencyVersionMap visitor = new DependencyVersionMap(log); -visitor.setUniqueVersions(uniqueVersions); -node.accept(visitor); -List errorMsgs = new ArrayList<>(); - errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes))); +DependencyNode node = ArtifactUtils.resolveTransitiveDependencies( +helper, +// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes +new DependencySelector() { +@Override +public boolean selectDependency(Dependency dependency) { +// regular OptionalDependencySelector only discriminates optional dependencies at level 2+ +return !dependency.isOptional() +// regular ScopeDependencySelector is case-sensitive +&& !dependency.getScope().equalsIgnoreCase(Artifact.SCOPE_TEST); +} + +@Override +public DependencySelector deriveChildSelector(DependencyCollectionContext context) { +return this; +} +}, +new ExclusionDependencySelector()); Review Comment: Changed to: new OptionalDependencySelector(), new ScopeDependencySelector(SCOPE_TEST, SCOPE_PROVIDED), new ExclusionDependencySelector()); -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056805792 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DependencyConvergence.java: ## @@ -51,58 +46,41 @@ public class DependencyConvergence implements EnforcerRule { private List excludes; +private DependencyVersionMap dependencyVersionMap; + public void setUniqueVersions(boolean uniqueVersions) { this.uniqueVersions = uniqueVersions; } -// CHECKSTYLE_OFF: LineLength -/** - * Uses the {@link EnforcerRuleHelper} to populate the values of the - * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} - * factory method. - * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do. - * - * @param helper - * @return a Dependency Node which is the root of the project's dependency tree - * @throws EnforcerRuleException - */ -// CHECKSTYLE_ON: LineLength -private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { -try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); -DependencyCollectorBuilder dependencyCollectorBuilder = -helper.getComponent(DependencyCollectorBuilder.class); -ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); -buildingRequest.setLocalRepository(repository); -ArtifactFilter filter = (Artifact a) -> -("compile".equalsIgnoreCase(a.getScope()) || "runtime".equalsIgnoreCase(a.getScope())) -&& !a.isOptional(); - -return dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); -} catch (ExpressionEvaluationException | ComponentLookupException e) { -throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); -} catch (DependencyCollectorBuilderException e) { -throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e); -} -} - @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { -DependencyNode node = getNode(helper); -DependencyVersionMap visitor = new DependencyVersionMap(log); -visitor.setUniqueVersions(uniqueVersions); -node.accept(visitor); -List errorMsgs = new ArrayList<>(); - errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes))); +DependencyNode node = ArtifactUtils.resolveTransitiveDependencies( +helper, +// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes +new DependencySelector() { +@Override +public boolean selectDependency(Dependency dependency) { +// regular OptionalDependencySelector only discriminates optional dependencies at level 2+ +return !dependency.isOptional() +// regular ScopeDependencySelector is case-sensitive +&& !dependency.getScope().equalsIgnoreCase(Artifact.SCOPE_TEST); +} + +@Override +public DependencySelector deriveChildSelector(DependencyCollectionContext context) { +return this; +} +}, +new ExclusionDependencySelector()); Review Comment: the scope selector was not passing tests if the scope case was wrong; but I'll apply toLowerCase in ArtifactUtils.toArtifact(DependencyNode) -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056805579 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/DependencyConvergence.java: ## @@ -51,58 +46,41 @@ public class DependencyConvergence implements EnforcerRule { private List excludes; +private DependencyVersionMap dependencyVersionMap; + public void setUniqueVersions(boolean uniqueVersions) { this.uniqueVersions = uniqueVersions; } -// CHECKSTYLE_OFF: LineLength -/** - * Uses the {@link EnforcerRuleHelper} to populate the values of the - * {@link DependencyTreeBuilder#buildDependencyTree(MavenProject, ArtifactRepository, ArtifactFactory, ArtifactMetadataSource, ArtifactFilter, ArtifactCollector)} - * factory method. - * This method simply exists to hide all the ugly lookup that the {@link EnforcerRuleHelper} has to do. - * - * @param helper - * @return a Dependency Node which is the root of the project's dependency tree - * @throws EnforcerRuleException - */ -// CHECKSTYLE_ON: LineLength -private DependencyNode getNode(EnforcerRuleHelper helper) throws EnforcerRuleException { -try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); -DependencyCollectorBuilder dependencyCollectorBuilder = -helper.getComponent(DependencyCollectorBuilder.class); -ArtifactRepository repository = (ArtifactRepository) helper.evaluate("${localRepository}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); -buildingRequest.setLocalRepository(repository); -ArtifactFilter filter = (Artifact a) -> -("compile".equalsIgnoreCase(a.getScope()) || "runtime".equalsIgnoreCase(a.getScope())) -&& !a.isOptional(); - -return dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter); -} catch (ExpressionEvaluationException | ComponentLookupException e) { -throw new EnforcerRuleException("Unable to lookup a component " + e.getLocalizedMessage(), e); -} catch (DependencyCollectorBuilderException e) { -throw new EnforcerRuleException("Could not build dependency tree " + e.getLocalizedMessage(), e); -} -} - @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { if (log == null) { log = helper.getLog(); } try { -DependencyNode node = getNode(helper); -DependencyVersionMap visitor = new DependencyVersionMap(log); -visitor.setUniqueVersions(uniqueVersions); -node.accept(visitor); -List errorMsgs = new ArrayList<>(); - errorMsgs.addAll(getConvergenceErrorMsgs(visitor.getConflictedVersionNumbers(includes, excludes))); +DependencyNode node = ArtifactUtils.resolveTransitiveDependencies( +helper, +// TODO: use a modified version of ExclusionDependencySelector to process excludes and includes +new DependencySelector() { +@Override +public boolean selectDependency(Dependency dependency) { +// regular OptionalDependencySelector only discriminates optional dependencies at level 2+ +return !dependency.isOptional() +// regular ScopeDependencySelector is case-sensitive +&& !dependency.getScope().equalsIgnoreCase(Artifact.SCOPE_TEST); +} + +@Override +public DependencySelector deriveChildSelector(DependencyCollectionContext context) { +return this; +} +}, +new ExclusionDependencySelector()); Review Comment: > Why we need here special selectors? It's explained in the comment: // regular OptionalDependencySelector only discriminates optional dependencies at level 2+ -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056805513 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java: ## @@ -18,70 +18,188 @@ */ package org.apache.maven.plugins.enforcer.utils; +import static java.util.Optional.ofNullable; + +import java.util.Collection; import java.util.HashSet; -import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern; -import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.collection.DependencyCollectionException; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; /** * * @author Robert Scholte * @since 3.0.0 */ public final class ArtifactUtils { -private ArtifactUtils() {} -public static Set getAllDescendants(DependencyNode node) { -Set children = null; -if (node.getChildren() != null) { -children = new HashSet<>(); -for (DependencyNode depNode : node.getChildren()) { -children.add(depNode.getArtifact()); -Set subNodes = getAllDescendants(depNode); -if (subNodes != null) { -children.addAll(subNodes); -} +/** + * Converts {@link DependencyNode} to {@link Artifact}; in comparison + * to {@link RepositoryUtils#toArtifact(org.eclipse.aether.artifact.Artifact)}, this method + * assigns {@link Artifact#getScope()} and {@link Artifact#isOptional()} based on + * the dependency information from the node. + * + * @param node {@link DependencyNode} to convert to {@link Artifact} + * @return target artifact + */ +public static Artifact toArtifact(DependencyNode node) { +Artifact artifact = RepositoryUtils.toArtifact(node.getArtifact()); +ofNullable(node.getDependency()).ifPresent(dependency -> { +ofNullable(dependency.getScope()).ifPresent(artifact::setScope); +artifact.setOptional(dependency.isOptional()); +}); +return artifact; +} + +/** + * Retrieves the {@link DependencyNode} instance containing the result of the transitive dependency + * for the current {@link MavenProject}. + * + * @param helper (may not be null) an instance of the {@link EnforcerRuleHelper} class + * @param selectors zero or more {@link DependencySelector} instances + * @return a Dependency Node which is the root of the project's dependency tree + * @throws EnforcerRuleException thrown if the lookup fails + */ +public static DependencyNode resolveTransitiveDependencies( +EnforcerRuleHelper helper, DependencySelector... selectors) throws EnforcerRuleException { +try { +RepositorySystem repositorySystem = helper.getComponent(RepositorySystem.class); +MavenSession session = (MavenSession) helper.evaluate("${session}"); +MavenProject project = session.getCurrentProject(); +ArtifactTypeRegistry artifactTypeRegistry = +session.getRepositorySession().getArtifactTypeRegistry(); + +DefaultRepositorySystemSession repositorySystemSession = +new DefaultRepositorySystemSession(session.getRepositorySession()); +repositorySystemSession.setDependencyGraphTransformer(new ConflictResolver( +new NearestVersionSelector(), +
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056275747 ## maven-enforcer-plugin/src/it/projects/banned-dependencies-fail/invoker.properties: ## @@ -0,0 +1,18 @@ +# 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. + +invoker.buildResult = failure Review Comment: ok -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056199938 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedPlugins.java: ## @@ -29,12 +28,13 @@ */ public class BannedPlugins extends BannedDependencies { @Override -protected Set getDependenciesToCheck(ProjectBuildingRequest buildingRequest) { -return buildingRequest.getProject().getPluginArtifacts(); Review Comment: please approve the flow 嵐 -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056198836 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedPlugins.java: ## @@ -29,12 +28,13 @@ */ public class BannedPlugins extends BannedDependencies { @Override -protected Set getDependenciesToCheck(ProjectBuildingRequest buildingRequest) { -return buildingRequest.getProject().getPluginArtifacts(); Review Comment: Corrected that, and added a simple test on banned dependencies failure. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056159955 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedPlugins.java: ## @@ -29,12 +28,13 @@ */ public class BannedPlugins extends BannedDependencies { @Override -protected Set getDependenciesToCheck(ProjectBuildingRequest buildingRequest) { -return buildingRequest.getProject().getPluginArtifacts(); Review Comment: Apologies for that, let me check why it's failing and I'll create more tests once done. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1056156097 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedPlugins.java: ## @@ -29,12 +28,13 @@ */ public class BannedPlugins extends BannedDependencies { @Override -protected Set getDependenciesToCheck(ProjectBuildingRequest buildingRequest) { -return buildingRequest.getProject().getPluginArtifacts(); Review Comment: No, it doesn't work based on artifact sets anymore. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1052618360 ## maven-enforcer-extension/pom.xml: ## @@ -71,6 +71,7 @@ maven-invoker-plugin true + true Review Comment: Not using streamLogs - as requested. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051782468 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanTransitiveDependencies.java: ## @@ -127,55 +130,27 @@ private static boolean searchTree(DependencyNode node, int level, ArtifactMatche @Override public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { -this.helper = helper; - -if (excludes == null) { -excludes = Collections.emptyList(); -} -if (includes == null) { -includes = Collections.emptyList(); -} - -final ArtifactMatcher exclusions = new ArtifactMatcher(excludes, includes); - -DependencyNode rootNode = null; - +MavenSession session; try { -MavenProject project = (MavenProject) helper.evaluate("${project}"); -MavenSession session = (MavenSession) helper.evaluate("${session}"); - -ProjectBuildingRequest buildingRequest = -new DefaultProjectBuildingRequest(session.getProjectBuildingRequest()); -buildingRequest.setProject(project); - -rootNode = createDependencyGraphBuilder().buildDependencyGraph(buildingRequest, null); -} catch (Exception e) { -throw new EnforcerRuleException("Error: Could not construct dependency tree.", e); -} - -String message = getMessage(); -StringBuilder generatedMessage = null; -if (message == null) { -generatedMessage = new StringBuilder(); +session = (MavenSession) helper.evaluate("${session}"); +} catch (ExpressionEvaluationException e) { +throw new RuntimeException(e); } - +ArtifactTypeRegistry artifactTypeRegistry = +session.getRepositorySession().getArtifactTypeRegistry(); +ArtifactMatcher exclusions = new ArtifactMatcher(excludes, includes); +Set directDependencies = session.getCurrentProject().getDependencies().parallelStream() +.map(d -> RepositoryUtils.toDependency(d, artifactTypeRegistry)) +.collect(Collectors.toSet()); Review Comment: Collectors are not. noneMatch, anyMatch should be safe, but I can replace those too just to be on the safe side. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051447866 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java: ## @@ -18,70 +18,201 @@ */ package org.apache.maven.plugins.enforcer.utils; +import static java.util.Optional.ofNullable; + +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern; -import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.collection.DependencyCollectionException; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; /** * * @author Robert Scholte * @since 3.0.0 */ public final class ArtifactUtils { -private ArtifactUtils() {} +private static final Map NODE_ARTIFACT_MAP = new HashMap<>(); +private static final Map AETHER_ARTIFACT_MAP = new HashMap<>(); Review Comment: Sorry for the confusion. Removed the caching altogether -- the gain is negligible and simplicity is much more important. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051437004 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java: ## @@ -18,22 +18,15 @@ */ Review Comment: Imho, the documentation is clear on how to create custom rules -- enforcer-api must be used. Is non-standard use supported? What if there are projects that depend on the current internal implementation? Should this also be supported? -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051437004 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java: ## @@ -18,22 +18,15 @@ */ Review Comment: Imho, the documentation is clear on how to create custom rules -- enforcer-api must be supported. Is non-standard use supported? What if there are projects that depend on the current internal implementation? Should this also be supported? -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051434788 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java: ## @@ -18,70 +18,201 @@ */ package org.apache.maven.plugins.enforcer.utils; +import static java.util.Optional.ofNullable; + +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern; -import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.collection.DependencyCollectionException; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; /** * * @author Robert Scholte * @since 3.0.0 */ public final class ArtifactUtils { -private ArtifactUtils() {} +private static final Map NODE_ARTIFACT_MAP = new HashMap<>(); +private static final Map AETHER_ARTIFACT_MAP = new HashMap<>(); + +/** + * Converts {@link DependencyNode} to {@link Artifact}; in comparison + * to {@linkpl RepositoryUtils#toArtifact(org.eclipse.aether.artifact.Artifact)}, this method + * assigns {@link Artifact#getScope()} and {@link Artifact#isOptional()} based on + * the dependency information from the node. + * + * @param node {@link DependencyNode} to convert to {@link Artifact} + * @return target artifact + */ +public static Artifact toArtifact(DependencyNode node) { +Artifact result = NODE_ARTIFACT_MAP.computeIfAbsent(node, n -> RepositoryUtils.toArtifact(n.getArtifact())); +ofNullable(node.getDependency()).ifPresent(dependency -> { +ofNullable(dependency.getScope()).ifPresent(result::setScope); +result.setOptional(dependency.isOptional()); +}); Review Comment: Updated: caching the built objects. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051434499 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java: ## @@ -18,70 +18,201 @@ */ package org.apache.maven.plugins.enforcer.utils; +import static java.util.Optional.ofNullable; + +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern; -import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.collection.DependencyCollectionException; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; /** * * @author Robert Scholte * @since 3.0.0 */ public final class ArtifactUtils { -private ArtifactUtils() {} +private static final Map NODE_ARTIFACT_MAP = new HashMap<>(); +private static final Map AETHER_ARTIFACT_MAP = new HashMap<>(); Review Comment: ConcurrentHashMap or should I rather move this to the instance? -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051432041 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java: ## @@ -18,70 +18,201 @@ */ package org.apache.maven.plugins.enforcer.utils; +import static java.util.Optional.ofNullable; + +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern; -import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.collection.DependencyCollectionException; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; /** * * @author Robert Scholte * @since 3.0.0 */ public final class ArtifactUtils { -private ArtifactUtils() {} +private static final Map NODE_ARTIFACT_MAP = new HashMap<>(); +private static final Map AETHER_ARTIFACT_MAP = new HashMap<>(); + +/** + * Converts {@link DependencyNode} to {@link Artifact}; in comparison + * to {@linkpl RepositoryUtils#toArtifact(org.eclipse.aether.artifact.Artifact)}, this method + * assigns {@link Artifact#getScope()} and {@link Artifact#isOptional()} based on + * the dependency information from the node. + * + * @param node {@link DependencyNode} to convert to {@link Artifact} + * @return target artifact + */ +public static Artifact toArtifact(DependencyNode node) { +Artifact result = NODE_ARTIFACT_MAP.computeIfAbsent(node, n -> RepositoryUtils.toArtifact(n.getArtifact())); +ofNullable(node.getDependency()).ifPresent(dependency -> { +ofNullable(dependency.getScope()).ifPresent(result::setScope); +result.setOptional(dependency.isOptional()); +}); Review Comment: Agreed. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051432007 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractPropertyEnforcerRule.java: ## @@ -32,16 +32,16 @@ public abstract class AbstractPropertyEnforcerRule extends AbstractNonCacheableE /** * Match the property value to a given regular expression. Defaults to null (any value is ok). * - * @see {@link #setRegex(String)} - * @see {@link #getRegex()} + * @see #setRegex(String) + * @see #getRegex() */ private String regex = null; /** * Specify a warning message if the regular expression is not matched. * - * @see {@link #setRegexMessage(String)} - * @see {@link #getRegexMessage()} + * @see #setRegexMessage(String) + * @see #getRegexMessage() Review Comment: My linter complained about it, but can be done in a separate PR indeed. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1051431973 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/ArtifactUtils.java: ## @@ -18,70 +18,201 @@ */ package org.apache.maven.plugins.enforcer.utils; +import static java.util.Optional.ofNullable; + +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.DependencyManagement; import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern; -import org.apache.maven.shared.dependency.graph.DependencyNode; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.ArtifactTypeRegistry; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.collection.DependencyCollectionException; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.graph.DependencyNode; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.JavaScopeSelector; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; /** * * @author Robert Scholte * @since 3.0.0 */ public final class ArtifactUtils { -private ArtifactUtils() {} +private static final Map NODE_ARTIFACT_MAP = new HashMap<>(); +private static final Map AETHER_ARTIFACT_MAP = new HashMap<>(); + +/** + * Converts {@link DependencyNode} to {@link Artifact}; in comparison + * to {@linkpl RepositoryUtils#toArtifact(org.eclipse.aether.artifact.Artifact)}, this method + * assigns {@link Artifact#getScope()} and {@link Artifact#isOptional()} based on + * the dependency information from the node. + * + * @param node {@link DependencyNode} to convert to {@link Artifact} + * @return target artifact + */ +public static Artifact toArtifact(DependencyNode node) { +Artifact result = NODE_ARTIFACT_MAP.computeIfAbsent(node, n -> RepositoryUtils.toArtifact(n.getArtifact())); +ofNullable(node.getDependency()).ifPresent(dependency -> { +ofNullable(dependency.getScope()).ifPresent(result::setScope); +result.setOptional(dependency.isOptional()); +}); +AETHER_ARTIFACT_MAP.putIfAbsent(result, node.getArtifact()); +return result; +} + +/** + * Converts {@link Artifact} to {@link org.eclipse.aether.artifact.Artifact} + * @param artifact source artifact + * @return target artifact + */ +public static org.eclipse.aether.artifact.Artifact toArtifact(Artifact artifact) { +return AETHER_ARTIFACT_MAP.computeIfAbsent(artifact, RepositoryUtils::toArtifact); +} + +/** + * Retrieves the {@link DependencyNode} instance containing the result of the transitive dependency + * for the current {@link MavenProject}. + * + * @param helper (may not be null) an instance of the {@link EnforcerRuleHelper} class + * @param selectors zero or more {@link DependencySelector} instances + * @return a Dependency Node which is the root of the project's dependency tree + * @throws EnforcerRuleException thrown if the lookup fails + */ +public static DependencyNode resolveTransitiveDependencies( +EnforcerRuleHelper helper, DependencySelector... selectors) throws EnforcerRuleException { +try { +RepositorySystem repositorySystem = helper.getComponent(RepositorySystem.class); +MavenSession session = (MavenSession) helper.evaluate("${session}"); +MavenProject project = session.getCurrentProject(); +ArtifactTypeRegistry artifactTypeRegistry = +session.getRepositorySession().getArtifactTypeRegistry(); -public static Set getAllDescendants(DependencyNode node) { -Set children = null; -if
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1050911744 ## enforcer-rules/pom.xml: ## @@ -112,22 +112,6 @@ mockito-core test - Review Comment: Ok, will restore; please keep the review comments coming, I'll process them later. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1050900528 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BannedDependenciesBase.java: ## @@ -0,0 +1,195 @@ +/* + * 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.maven.plugins.enforcer; + +import com.google.common.base.Strings; +import java.util.List; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugins.enforcer.utils.ArtifactUtils; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.eclipse.aether.graph.DependencyNode; + +/** + * Abstract base class for rules which validate the transitive + * dependency tree by traversing all children and validating every + * dependency artifact. + */ +public abstract class BannedDependenciesBase extends AbstractNonCacheableEnforcerRule { Review Comment: A final abstract class? -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [maven-enforcer] ajarmoniuk commented on a diff in pull request #198: [MENFORCER-435] Replacing maven-compat and maven-dependency-tree usage with Resolver
ajarmoniuk commented on code in PR #198: URL: https://github.com/apache/maven-enforcer/pull/198#discussion_r1050790825 ## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/AbstractBanDependencies.java: ## @@ -18,22 +18,15 @@ */ Review Comment: Really??? If so, I consider this a bad API. Especially because a collection of artifacts is unsuitable for a general purpose. I might as well simply move the rules out of this class. -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org