You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/10/13 11:08:45 UTC

[GitHub] [maven-enforcer] kwin commented on a diff in pull request #187: [MENFORCER-427] New rule to ban dynamic versions

kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994502734


##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -0,0 +1,375 @@
+package org.apache.maven.plugins.enforcer;
+
+/*
+ * 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.
+ */
+
+import java.text.ChoiceFormat;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import org.apache.maven.RepositoryUtils;
+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.plugin.logging.Log;
+import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.utils.logging.MessageBuilder;
+import org.apache.maven.shared.utils.logging.MessageUtils;
+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.RepositorySystemSession;
+import org.eclipse.aether.collection.CollectRequest;
+import org.eclipse.aether.collection.CollectResult;
+import org.eclipse.aether.collection.DependencyCollectionException;
+import org.eclipse.aether.collection.DependencySelector;
+import org.eclipse.aether.graph.Dependency;
+import org.eclipse.aether.graph.DependencyNode;
+import org.eclipse.aether.graph.DependencyVisitor;
+import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.util.graph.selector.AndDependencySelector;
+import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
+import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
+import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor;
+import org.eclipse.aether.version.VersionConstraint;
+
+/**
+ * This rule bans dependencies having a version which requires resolution (i.e. dynamic versions which might change with
+ * each build). Dynamic versions are either
+ * <ul>
+ * <li>version ranges,</li>
+ * <li>the special placeholders {@code LATEST} or {@code RELEASE} or</li>
+ * <li>versions ending with {@code -SNAPSHOT}.
+ * </ul>
+ * 
+ * @since 3.2.0
+ */
+public class BanDynamicVersions
+    extends AbstractNonCacheableEnforcerRule
+{
+
+    private static final String RELEASE = "RELEASE";
+
+    private static final String LATEST = "LATEST";
+
+    private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT";
+
+    /**
+     * {@code true} if versions ending with {@code -SNAPSHOT} should be allowed
+     */
+    private boolean allowSnapshots;
+
+    /**
+     * {@code true} if versions using {@code LATEST} should be allowed
+     */
+    private boolean allowLatest;
+
+    /**
+     * {@code true} if versions using {@code RELEASE} should be allowed
+     */
+    private boolean allowRelease;
+
+    /**
+     * {@code true} if version ranges should be allowed
+     */
+    private boolean allowRanges;
+
+    /**
+     * {@code true} if also optional dependencies should be checked
+     */
+    private boolean checkOptionals;
+
+    /**
+     * the scopes of dependencies which should be excluded from this rule
+     */
+    private String[] excludedScopes;
+
+    /**
+     * Specify the ignored dependencies. This can be a list of artifacts in the format
+     * <code>groupId[:artifactId][:version]</code>. Any of the sections can be a wildcard by using '*' (ie group:*:1.0)
+     * <br>
+     * The rule will fail if any dependency matches any exclude, unless it also matches an include rule.
+     * 
+     * @see {@link #setIgnores(List)}
+     */
+    private List<String> ignores = null;
+
+    public void setIgnores( List<String> ignores )
+    {
+        this.ignores = ignores;
+    }
+
+    public void setAllowSnapshots( boolean allowSnapshots )
+    {
+        this.allowSnapshots = allowSnapshots;
+    }
+
+    public void setAllowLatest( boolean allowLatest )
+    {
+        this.allowLatest = allowLatest;
+    }
+
+    public void setAllowRelease( boolean allowRelease )
+    {
+        this.allowRelease = allowRelease;
+    }
+
+    public void setAllowRanges( boolean allowRanges )
+    {
+        this.allowRanges = allowRanges;
+    }
+
+    public void setCheckOptionals( boolean checkOptionals )
+    {
+        this.checkOptionals = checkOptionals;
+    }
+
+    public void setExcludedScopes( String[] excludedScopes )
+    {
+        this.excludedScopes = excludedScopes;
+    }
+
+    private final class BannedDynamicVersionCollector
+        implements DependencyVisitor
+    {
+
+        private final Log log;
+
+        private final Deque<DependencyNode> nodeStack; // all intermediate nodes (without the root node)
+
+        private boolean isRoot = true;
+
+        private int numViolations;
+
+        private final Predicate<DependencyNode> predicate;
+
+        public int getNumViolations()
+        {
+            return numViolations;
+        }
+
+        BannedDynamicVersionCollector( Log log, Predicate<DependencyNode> predicate )
+        {
+            this.log = log;
+            nodeStack = new ArrayDeque<>();
+            this.predicate = predicate;
+            this.isRoot = true;
+            numViolations = 0;
+        }
+
+        private boolean isBannedDynamicVersion( VersionConstraint versionConstraint )
+        {
+            if ( versionConstraint.getVersion() != null )
+            {
+                if ( versionConstraint.getVersion().toString().equals( LATEST ) )
+                {
+                    return !allowLatest;
+                }
+                else if ( versionConstraint.getVersion().toString().equals( RELEASE ) )
+                {
+                    return !allowRelease;
+                }
+                else if ( versionConstraint.getVersion().toString().endsWith( SNAPSHOT_SUFFIX ) )
+                {
+                    return !allowSnapshots;
+                }

Review Comment:
   Is there? In Resolver API?



-- 
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@maven.apache.org

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