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<DependencyNode, Artifact> NODE_ARTIFACT_MAP = new HashMap<>(); + private static final Map<Artifact, org.eclipse.aether.artifact.Artifact> 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