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/11 17:51:38 UTC

[GitHub] [maven-enforcer] kwin opened a new pull request, #187: [MENFORCER-427] New rule to ban dynamic versions

kwin opened a new pull request, #187:
URL: https://github.com/apache/maven-enforcer/pull/187

   Following this checklist to help us incorporate your 
   contribution quickly and easily:
   
    - [x] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MENFORCER) filed 
          for the change (usually before you start working on it).  Trivial changes like typos do not 
          require a JIRA issue.  Your pull request should address just this issue, without 
          pulling in other changes.
    - [x] Each commit in the pull request should have a meaningful subject line and body.
    - [x] Format the pull request title like `[MENFORCER-XXX] - Fixes bug in ApproximateQuantiles`,
          where you replace `MENFORCER-XXX` with the appropriate JIRA issue. Best practice
          is to use the JIRA issue title in the pull request title and in the first line of the 
          commit message.
    - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [x] Run `mvn clean verify` to make sure basic checks pass. A more thorough check will 
          be performed on your pull request automatically.
    - [x] You have run the integration tests successfully (`mvn -Prun-its clean verify`).
   
   If your pull request is about ~20 lines of code you don't need to sign an
   [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
   please ask on the developers list.
   
   To make clear that you license your contribution under 
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
    - [ ] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [x] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   


-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994571730


##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );
+        }
+        catch ( ComponentLookupException cle )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve component RepositorySystem", cle );

Review Comment:
   The rest is injected via expressions!



-- 
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


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

Posted by GitBox <gi...@apache.org>.
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1275663253

   - `[1.0.0,1.0.0]` is currently invalid and leads to `org.apache.maven.artifact.versioning.InvalidVersionSpecificationException: Range cannot have identical boundaries: [1.0,1.0]`.
   - `[1.0]` leads to version resolving (although redundant) when used in a dependency and therefore always means overhead during build and dependency on metadata. I would rather ban this as well. This should be replaced by `1.0` to speed up the build!


-- 
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


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

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994635932


##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -99,9 +100,15 @@
     private boolean allowRanges;
 
     /**
-     * {@code true} if also optional dependencies should be checked
+     * {@code true} if ranges having the same upper and lower bound like {@code [1.0]} should be allowed.
+     * Only applicable if {@link #allowRanges} is not set to {@code true}.
      */
-    private boolean checkOptionals;
+    private boolean allowRangesWithSameUpperAndLowerBound;

Review Comment:
   That is huge. What about `allowRangesWithIdenticalBounds?



##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -99,9 +100,15 @@
     private boolean allowRanges;
 
     /**
-     * {@code true} if also optional dependencies should be checked
+     * {@code true} if ranges having the same upper and lower bound like {@code [1.0]} should be allowed.
+     * Only applicable if {@link #allowRanges} is not set to {@code true}.
      */
-    private boolean checkOptionals;
+    private boolean allowRangesWithSameUpperAndLowerBound;

Review Comment:
   That is huge. What about `allowRangesWithIdenticalBounds`?



-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994613535


##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }

Review Comment:
   Done in https://github.com/apache/maven-enforcer/pull/187/commits/4cfee6af3c02787dd254cf7346228b7edaa8ce9f.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994577941


##########
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:
   OK, accepted.



##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );
+        }
+        catch ( ComponentLookupException cle )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve component RepositorySystem", cle );

Review Comment:
   ah 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-unsubscribe@maven.apache.org

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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1275973098

   I opened https://issues.apache.org/jira/browse/MNG-7561 for same lower and upper bounds but it seems using a hard restriction always require resolving against metadata from local/remote repo, therefore at least optionally this pattern should be banned as well in this rule.


-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1277425972

   > This is strange because 1.0 is resolved to this...I wonder why this range is logically invalid.
   
   I proposed a fix for this attached to https://issues.apache.org/jira/browse/MNG-7106


-- 
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


[GitHub] [maven-enforcer] michael-o commented on pull request #187: [MENFORCER-427] New rule to ban dynamic versions

Posted by GitBox <gi...@apache.org>.
michael-o commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1277474671

   > > I think it is parsed to [1.0,1.0] then of course it incurs resolution.
   > 
   > This is totally unexpected because `1.0` (soft requirement) is not resolved, see also my comment at [apache/maven#823 (comment)](https://github.com/apache/maven/pull/823#discussion_r993263931). The only difference between `[1.0]` and `1.0` should be that one is a hard requirement the other one a soft one (https://maven.apache.org/pom.html#dependency-version-requirement-specification). The documentation doesn't say anything that hard requirements are resolved while soft ones aren't.
   
   My bad, I meant `[1.0]`


-- 
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


[GitHub] [maven-enforcer] kwin merged pull request #187: [MENFORCER-427] New rule to ban dynamic versions

Posted by GitBox <gi...@apache.org>.
kwin merged PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187


-- 
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


[GitHub] [maven-enforcer] michael-o commented on pull request #187: [MENFORCER-427] New rule to ban dynamic versions

Posted by GitBox <gi...@apache.org>.
michael-o commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1277424886

   > * `[1.0.0,1.0.0]` is currently invalid and leads to `org.apache.maven.artifact.versioning.InvalidVersionSpecificationException: Range cannot have identical boundaries: [1.0,1.0]`.
   
   This is strange because 1.0 is resolved to this...I wonder why this range is logically invalid.
   
   >     * `[1.0]` leads to version resolving (although redundant) when used in a dependency and therefore always means overhead during build and dependency on metadata. I would rather ban this as well. This should be replaced by soft requirement `1.0` to speed up the build! Is there already an issue related to this? According to https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification the fixed hard requirement may make sense for some edge cases, but as long as this leads to version resolving I would recommend not to rely on it.
   
   Well, `[1.0]` exists for a reason. I think it is parsed to `[1.0,1.0]` then of course it incurs resolution. Hard for me to tell whether this should be blocked or not. If you want to block this, fine. Add a comment about the situation.


-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994654016


##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -99,9 +100,15 @@
     private boolean allowRanges;
 
     /**
-     * {@code true} if also optional dependencies should be checked
+     * {@code true} if ranges having the same upper and lower bound like {@code [1.0]} should be allowed.
+     * Only applicable if {@link #allowRanges} is not set to {@code true}.
      */
-    private boolean checkOptionals;
+    private boolean allowRangesWithSameUpperAndLowerBound;

Review Comment:
   only slighly shorter, but I changed to that in https://github.com/apache/maven-enforcer/pull/187/commits/2d82f485ff5c73a6052b189fa9488d02fa4b3cbb



##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -99,9 +100,15 @@
     private boolean allowRanges;
 
     /**
-     * {@code true} if also optional dependencies should be checked
+     * {@code true} if ranges having the same upper and lower bound like {@code [1.0]} should be allowed.
+     * Only applicable if {@link #allowRanges} is not set to {@code true}.
      */
-    private boolean checkOptionals;
+    private boolean allowRangesWithSameUpperAndLowerBound;

Review Comment:
   only slightly shorter, but I changed to that in https://github.com/apache/maven-enforcer/pull/187/commits/2d82f485ff5c73a6052b189fa9488d02fa4b3cbb



-- 
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


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

Posted by GitBox <gi...@apache.org>.
gnodet commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994513017


##########
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;

Review Comment:
   Why a default of `false` here ? In what sense are optional dependencies different wrt to version resolving ? If they aren't, I'd think the default should be also check those.
   or maybe `excludeOptionals` if the above is true.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1277429206

   >  I think it is parsed to [1.0,1.0] then of course it incurs resolution.
   
   This is totally unexpected because `1.0` (soft requirement) is not resolved, see also my comment at https://github.com/apache/maven/pull/823#discussion_r993263931.
   


-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994613781


##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );

Review Comment:
   Done in https://github.com/apache/maven-enforcer/pull/187/commits/182722b981b2257ec56bd8b34c3d21e813380cd5.



##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );
+        }
+        catch ( ComponentLookupException cle )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve component RepositorySystem", cle );
+        }
+        Log log = helper.getLog();
+
+        Collection<DependencySelector> depSelectors = new ArrayList<>();
+        depSelectors.add( new ScopeDependencySelector( excludedScopes ) );
+        if ( !checkOptionals )
+        {
+            depSelectors.add( new OptionalDependencySelector() );
+        }
+        newRepoSession.setDependencySelector( new AndDependencySelector( depSelectors ) );
+
+        Dependency rootDependency = RepositoryUtils.toDependency( project.getArtifact(), null );
+        try
+        {
+            // use root dependency with unresolved direct dependencies
+            int numViolations = emitDependenciesWithBannedDynamicVersions( rootDependency, repoSystem, newRepoSession,
+                                                                           remoteRepositories, log );
+            if ( numViolations > 0 )
+            {
+                ChoiceFormat dependenciesFormat = new ChoiceFormat( "1#dependency|1<dependencies" );
+                throw new EnforcerRuleException( "Found " + numViolations + " "
+                    + dependenciesFormat.format( numViolations )
+                    + " with dynamic versions. Look at the warnings emitted above for the details." );
+            }
+        }
+        catch ( DependencyCollectionException e )
+        {
+            throw new EnforcerRuleException( "Could not retrieve dependency metadata for project  : " + e.getMessage(),
+                                             e );

Review Comment:
   Done in https://github.com/apache/maven-enforcer/pull/187/commits/182722b981b2257ec56bd8b34c3d21e813380cd5.



##########
enforcer-rules/src/site/apt/banDynamicVersions.apt.vm:
##########
@@ -0,0 +1,108 @@
+~~ 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.    
+ 
+  ------
+  Ban Dynamic Versions
+  ------
+  Konrad Windszus
+  ------
+  October 2022

Review Comment:
   Done in https://github.com/apache/maven-enforcer/pull/187/commits/182722b981b2257ec56bd8b34c3d21e813380cd5.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994654305


##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -110,7 +117,8 @@
 
     /**
      * 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)
+     * <code>groupId[:artifactId[:version[:type[:scope:[classifier]]]]]</code>. 
+     * Any of the sections can be a wildcard by using '*' (ie group:*:1.0).

Review Comment:
   fixed in https://github.com/apache/maven-enforcer/pull/187/commits/2d82f485ff5c73a6052b189fa9488d02fa4b3cbb.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994491972


##########
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:
   Isn't there a better way tthan relying on `#toString()`?



##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );

Review Comment:
   This duplicates the message. I think ` throw new EnforcerRuleException( "Cannot resolve expression", eee )` is enough.



##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );
+        }
+        catch ( ComponentLookupException cle )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve component RepositorySystem", cle );
+        }
+        Log log = helper.getLog();
+
+        Collection<DependencySelector> depSelectors = new ArrayList<>();
+        depSelectors.add( new ScopeDependencySelector( excludedScopes ) );
+        if ( !checkOptionals )
+        {
+            depSelectors.add( new OptionalDependencySelector() );
+        }
+        newRepoSession.setDependencySelector( new AndDependencySelector( depSelectors ) );
+
+        Dependency rootDependency = RepositoryUtils.toDependency( project.getArtifact(), null );
+        try
+        {
+            // use root dependency with unresolved direct dependencies
+            int numViolations = emitDependenciesWithBannedDynamicVersions( rootDependency, repoSystem, newRepoSession,
+                                                                           remoteRepositories, log );
+            if ( numViolations > 0 )
+            {
+                ChoiceFormat dependenciesFormat = new ChoiceFormat( "1#dependency|1<dependencies" );
+                throw new EnforcerRuleException( "Found " + numViolations + " "
+                    + dependenciesFormat.format( numViolations )
+                    + " with dynamic versions. Look at the warnings emitted above for the details." );
+            }
+        }
+        catch ( DependencyCollectionException e )
+        {
+            throw new EnforcerRuleException( "Could not retrieve dependency metadata for project  : " + e.getMessage(),
+                                             e );

Review Comment:
   Same here, don't duplicate.



##########
enforcer-rules/src/site/apt/banDynamicVersions.apt.vm:
##########
@@ -0,0 +1,108 @@
+~~ 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.    
+ 
+  ------
+  Ban Dynamic Versions
+  ------
+  Konrad Windszus
+  ------
+  October 2022

Review Comment:
   Extended ISO date please.



##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }

Review Comment:
   Please use `Objects` for these null checks.



##########
enforcer-rules/src/site/apt/banDynamicVersions.apt.vm:
##########
@@ -0,0 +1,108 @@
+~~ 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.    
+ 
+  ------
+  Ban Dynamic Versions
+  ------
+  Konrad Windszus
+  ------
+  October 2022
+  ------
+
+Ban Dynamic Versions
+
+  This rule bans dependencies having versions which require resolution (i.e. dynamic versions which might change with each build). Dynamic versions are either 
+  
+  * version ranges,
+  
+  * the special placeholders <<<LATEST>>>/<<<RELEASE>>> or
+  
+  * versions ending with <<<-SNAPSHOT>>>.
+
+  []
+
+  The following parameters are supported by this rule:
+  
+  * <<allowSnapshots>> - if <<<true>>> dependencies with versions ending with <<<-SNAPSHOT>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<allowRelease>> - if <<<true>>> dependencies with version placeholder <<<RELEASE>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<allowLatest>> - if <<<true>>> dependencies with versions placeholder <<<LATEST>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<allowRanges>> - if <<<true>>> versions ending with <<<-SNAPSHOT>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<checkOptionals>> - if <<<true>>> also optional dependencies will be checked.  Default is <<<false>>>.
+  
+  * <<excludedScopes>> - the list of scopes to exclude;
+  
+  * <<ignores>> - a list of dependencies to ignore. The format is <<<groupId[:artifactId][:version][:type][:scope][:classifier]>>> where <<<artifactId>>>, <<<version>>>, <<<type>>>, <<<scope>>> and <<<classifier>>> are optional. Wildcards may be used to replace an entire or just parts of a section.

Review Comment:
   This does not correspond to https://github.com/apache/maven-enforcer/pull/187/files#diff-9dd3c382c00a8c5fa752798d8f5e7bae9b72fe5d7454bebf08dc77d1244fe5b4R113.



##########
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;
+                }
+            }
+            else if ( versionConstraint.getRange() != null )
+            {
+                return !allowRanges;
+            }
+            else
+            {
+                log.warn( "Unexpected version constraint found: " + versionConstraint );
+            }
+            return false;
+
+        }
+
+        @Override
+        public boolean visitEnter( DependencyNode node )
+        {
+            if ( isRoot )
+            {
+                isRoot = false;
+            }
+            else
+            {
+                log.debug( "Found node " + node + " with version constraint " + node.getVersionConstraint() );
+                if ( predicate.test( node ) && isBannedDynamicVersion( node.getVersionConstraint() ) )
+                {
+                    MessageBuilder msgBuilder = MessageUtils.buffer();
+                    log.warn( msgBuilder.a( "Dependency " )
+                              .strong( node.getDependency() )
+                              .mojo( dumpIntermediatePath( nodeStack ) )
+                              .a( " is referenced with a banned dynamic version " + node.getVersionConstraint() )
+                              .toString() );
+                    numViolations++;
+                    return false;
+                }
+                nodeStack.addLast( node );
+            }
+            return true;
+        }
+
+        @Override
+        public boolean visitLeave( DependencyNode node )
+        {
+            if ( !nodeStack.isEmpty() )
+            {
+                nodeStack.removeLast();
+            }
+            return true;
+        }
+    }
+
+    @SuppressWarnings( "unchecked" )
+    @Override
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        MavenProject project;
+        DefaultRepositorySystemSession newRepoSession;
+        RepositorySystem repoSystem;
+        List<RemoteRepository> remoteRepositories;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+            if ( project == null )
+            {
+                throw new ExpressionEvaluationException( "${project} is null" );
+            }
+            RepositorySystemSession repoSession =
+                (RepositorySystemSession) helper.evaluate( "${repositorySystemSession}" );
+            if ( repoSession == null )
+            {
+                throw new ExpressionEvaluationException( "${repositorySystemSession} is null" );
+            }
+            // get a new session to be able to tweak the dependency selector
+            newRepoSession = new DefaultRepositorySystemSession( repoSession );
+            remoteRepositories = (List<RemoteRepository>) helper.evaluate( "${project.remoteProjectRepositories}" );
+            repoSystem = helper.getComponent( RepositorySystem.class );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Cannot resolve expression: " + eee.getCause(), eee );
+        }
+        catch ( ComponentLookupException cle )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve component RepositorySystem", cle );

Review Comment:
   Only RS, not the rest?



-- 
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


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

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994532503


##########
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:
   Honestly, I don't know. I am bit surprised. If there isn't one, this is bad. Just place a comment that there is no better solution then this is fine.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994614655


##########
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;

Review Comment:
   Makes sense, fixed in https://github.com/apache/maven-enforcer/pull/187/commits/182722b981b2257ec56bd8b34c3d21e813380cd5.



-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994614282


##########
enforcer-rules/src/site/apt/banDynamicVersions.apt.vm:
##########
@@ -0,0 +1,108 @@
+~~ 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.    
+ 
+  ------
+  Ban Dynamic Versions
+  ------
+  Konrad Windszus
+  ------
+  October 2022
+  ------
+
+Ban Dynamic Versions
+
+  This rule bans dependencies having versions which require resolution (i.e. dynamic versions which might change with each build). Dynamic versions are either 
+  
+  * version ranges,
+  
+  * the special placeholders <<<LATEST>>>/<<<RELEASE>>> or
+  
+  * versions ending with <<<-SNAPSHOT>>>.
+
+  []
+
+  The following parameters are supported by this rule:
+  
+  * <<allowSnapshots>> - if <<<true>>> dependencies with versions ending with <<<-SNAPSHOT>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<allowRelease>> - if <<<true>>> dependencies with version placeholder <<<RELEASE>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<allowLatest>> - if <<<true>>> dependencies with versions placeholder <<<LATEST>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<allowRanges>> - if <<<true>>> versions ending with <<<-SNAPSHOT>>> will not be banned.  Default is <<<false>>>.
+  
+  * <<checkOptionals>> - if <<<true>>> also optional dependencies will be checked.  Default is <<<false>>>.
+  
+  * <<excludedScopes>> - the list of scopes to exclude;
+  
+  * <<ignores>> - a list of dependencies to ignore. The format is <<<groupId[:artifactId][:version][:type][:scope][:classifier]>>> where <<<artifactId>>>, <<<version>>>, <<<type>>>, <<<scope>>> and <<<classifier>>> are optional. Wildcards may be used to replace an entire or just parts of a section.

Review Comment:
   This is actually the supported format, fixed the javadoc in https://github.com/apache/maven-enforcer/pull/187/commits/182722b981b2257ec56bd8b34c3d21e813380cd5.



-- 
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


[GitHub] [maven-enforcer] michael-o commented on pull request #187: [MENFORCER-427] New rule to ban dynamic versions

Posted by GitBox <gi...@apache.org>.
michael-o commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1277417680

   > * `[1.0.0,1.0.0]` is currently invalid and leads to `org.apache.maven.artifact.versioning.InvalidVersionSpecificationException: Range cannot have identical boundaries: [1.0,1.0]`.
   > 
   >     * `[1.0]` leads to version resolving (although redundant) when used in a dependency and therefore always means overhead during build and dependency on metadata. I would rather ban this as well. This should be replaced by soft requirement `1.0` to speed up the build! Is there already an issue related to this? According to https://maven.apache.org/pom.html#Dependency_Version_Requirement_Specification the fixed hard requirement may make sense for some edge cases, but as long as this leads to version resolving I would recommend not to rely on it.
   
   @cstamas @gnodet What is your opinion on this.


-- 
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


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

Posted by GitBox <gi...@apache.org>.
michael-o commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994636938


##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDynamicVersions.java:
##########
@@ -110,7 +117,8 @@
 
     /**
      * 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)
+     * <code>groupId[:artifactId[:version[:type[:scope:[classifier]]]]]</code>. 
+     * Any of the sections can be a wildcard by using '*' (ie group:*:1.0).

Review Comment:
   `.e.g {@code group:*:1.0}`



-- 
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


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

Posted by GitBox <gi...@apache.org>.
kwin commented on code in PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#discussion_r994571477


##########
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:
   There is only https://github.com/apache/maven-resolver/blob/6e74e9ee279f827140553846b6421fe9b75d3ad9/maven-resolver-api/src/main/java/org/eclipse/aether/artifact/AbstractArtifact.java#L46 which is private. The only public API is when you deal with an artifact.



-- 
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


[GitHub] [maven-enforcer] michael-o commented on pull request #187: [MENFORCER-427] New rule to ban dynamic versions

Posted by GitBox <gi...@apache.org>.
michael-o commented on PR #187:
URL: https://github.com/apache/maven-enforcer/pull/187#issuecomment-1275121196

   Here are edge cases which I expect to work:
   * [1.0.0] (hard 1.0.0)
   * [1.0.0,1.0.0] (same as above), see https://issues.apache.org/jira/browse/MNG-7106


-- 
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