You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2007/07/06 04:53:06 UTC

svn commit: r553707 - in /maven/plugins/trunk/maven-enforcer-plugin/src: main/java/org/apache/maven/plugin/enforcer/ test/java/org/apache/maven/plugin/enforcer/

Author: brianf
Date: Thu Jul  5 19:53:06 2007
New Revision: 553707

URL: http://svn.apache.org/viewvc?view=rev&rev=553707
Log:
added new rule to ban snapshots

Added:
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BanSnapshots.java
    maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBanSnapshots.java
Modified:
    maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java
    maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBannedDependencies.java

Added: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java?view=auto&rev=553707
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java (added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/AbstractBanDependencies.java Thu Jul  5 19:53:06 2007
@@ -0,0 +1,160 @@
+package org.apache.maven.plugin.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.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.plugin.enforcer.util.EnforcerUtils;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * Abstract Rule for banning dependencies
+ * 
+ * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
+ * @version $Id$
+ * 
+ */
+public abstract class AbstractBanDependencies
+    implements EnforcerRule
+{
+
+    
+    /**
+     * Specify if transitive dependencies should be searched
+     * (default) or only look at direct dependencies
+     * 
+     * @parameter
+     */
+    public boolean searchTransitive = true;
+
+    /**
+     * Specify a friendly message if the rule fails.
+     * 
+     * @parameter
+     */
+    public String message = null;
+
+    /**
+     * Execute the rule.
+     */
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+
+        // get the project
+        MavenProject project = null;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
+        }
+
+        // get the correct list of dependencies
+        Set dependencies = null;
+        if ( searchTransitive )
+        {
+            dependencies = project.getArtifacts();
+        }
+        else
+        {
+            dependencies = project.getDependencyArtifacts();
+        }
+
+        // look for banned dependencies
+        Set foundExcludes = checkDependencies( dependencies );
+
+        // if any are found, fail the check but list all of
+        // them
+        if ( !foundExcludes.isEmpty() )
+        {
+            if ( message == null )
+            {
+                StringBuffer buf = new StringBuffer();
+                Iterator iter = foundExcludes.iterator();
+                while ( iter.hasNext() )
+                {
+                    buf.append( "Found Banned Dependency: " + ( (Artifact) iter.next() ).getId() + "\n" );
+                }
+                message = buf.toString();
+            }
+            throw new EnforcerRuleException( message );
+        }
+
+    }
+
+    /**
+     * Checks the set of dependencies against the list of
+     * excludes
+     * 
+     * @param dependencies
+     * @return
+     * @throws EnforcerRuleException
+     */
+    abstract protected Set checkDependencies( Set dependencies )
+        throws EnforcerRuleException;
+    
+    /**
+     * @return the message
+     */
+    public String getMessage()
+    {
+        return this.message;
+    }
+
+    /**
+     * @param theMessage the message to set
+     */
+    public void setMessage( String theMessage )
+    {
+        this.message = theMessage;
+    }
+
+    /**
+     * @return the searchTransitive
+     */
+    public boolean isSearchTransitive()
+    {
+        return this.searchTransitive;
+    }
+
+    /**
+     * @param theSearchTransitive the searchTransitive to set
+     */
+    public void setSearchTransitive( boolean theSearchTransitive )
+    {
+        this.searchTransitive = theSearchTransitive;
+    }
+}

Added: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BanSnapshots.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BanSnapshots.java?view=auto&rev=553707
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BanSnapshots.java (added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BanSnapshots.java Thu Jul  5 19:53:06 2007
@@ -0,0 +1,77 @@
+package org.apache.maven.plugin.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.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
+import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.plugin.enforcer.util.EnforcerUtils;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.util.StringUtils;
+
+/**
+ * This rule checks that no snapshots are included.
+ * 
+ * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
+ * @version $Id$
+ * 
+ */
+public class BanSnapshots
+    extends AbstractBanDependencies
+{
+
+    /**
+     * Checks the set of dependencies to see if any
+     * snapshots are included
+     * 
+     * @param dependencies
+     * @return
+     * @throws EnforcerRuleException
+     */
+    protected Set checkDependencies( Set dependencies )
+        throws EnforcerRuleException
+    {
+        Set foundExcludes = new HashSet();
+
+        Iterator DependencyIter = dependencies.iterator();
+        while ( DependencyIter.hasNext() )
+        {
+            Artifact artifact = (Artifact) DependencyIter.next();
+
+            if ( artifact.isSnapshot() )
+            {
+                foundExcludes.add( artifact );
+            }
+        }
+
+        return foundExcludes;
+    }
+}

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java?view=diff&rev=553707&r1=553706&r2=553707
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/main/java/org/apache/maven/plugin/enforcer/BannedDependencies.java Thu Jul  5 19:53:06 2007
@@ -46,84 +46,29 @@
  * 
  */
 public class BannedDependencies
-    implements EnforcerRule
+    extends AbstractBanDependencies
 {
 
     /**
      * Specify the banned dependencies. This can be a list
      * of artifacts in the format
-     * groupId[:artifactId][:version]
-     * Any of the sections can be a wildcard by using '*' (ie group:*:1.0)
+     * groupId[:artifactId][:version] Any of the sections
+     * can be a wildcard by using '*' (ie group:*:1.0)
      * 
      * @parameter
      * @required
      */
     public ArrayList excludes = null;
 
-    /**
-     * Specify if transitive dependencies should be searched
-     * (default) or only look at direct dependencies
+    /*
+     * (non-Javadoc)
      * 
-     * @parameter
-     */
-    public boolean searchTransitive = true;
-
-    /**
-     * Specify a friendly message if the rule fails.
-     * 
-     * @parameter
+     * @see org.apache.maven.plugin.enforcer.AbstractBanDependencies#checkDependencies(java.util.Set)
      */
-    public String message = null;
-
-    /**
-     * Execute the rule.
-     */
-    public void execute( EnforcerRuleHelper helper )
+    protected Set checkDependencies( Set theDependencies )
         throws EnforcerRuleException
     {
-
-        // get the project
-        MavenProject project = null;
-        try
-        {
-            project = (MavenProject) helper.evaluate( "${project}" );
-        }
-        catch ( ExpressionEvaluationException eee )
-        {
-            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee );
-        }
-
-        // get the correct list of dependencies
-        Set dependencies = null;
-        if ( searchTransitive )
-        {
-            dependencies = project.getArtifacts();
-        }
-        else
-        {
-            dependencies = project.getDependencyArtifacts();
-        }
-
-        // look for banned dependencies
-        Set foundExcludes = checkDependencies( dependencies, excludes );
-
-        // if any are found, fail the check but list all of
-        // them
-        if ( !foundExcludes.isEmpty() )
-        {
-            if ( message == null )
-            {
-                StringBuffer buf = new StringBuffer();
-                Iterator iter = foundExcludes.iterator();
-                while ( iter.hasNext() )
-                {
-                    buf.append( "Found Banned Dependency: " + ( (Artifact) iter.next() ).getId() + "\n" );
-                }
-                message = buf.toString();
-            }
-            throw new EnforcerRuleException( message );
-        }
-
+        return checkDependencies( theDependencies, excludes );
     }
 
     /**
@@ -134,7 +79,7 @@
      * @return
      * @throws EnforcerRuleException
      */
-    protected Set checkDependencies( Set dependencies, List theExcludes )
+    private Set checkDependencies( Set dependencies, List theExcludes )
         throws EnforcerRuleException
     {
         Set foundExcludes = new HashSet();
@@ -145,8 +90,8 @@
             String exclude = (String) iter.next();
 
             String[] subStrings = exclude.split( ":" );
-            subStrings = StringUtils.stripAll(subStrings);
-            
+            subStrings = StringUtils.stripAll( subStrings );
+
             Iterator DependencyIter = dependencies.iterator();
             while ( DependencyIter.hasNext() )
             {
@@ -227,35 +172,4 @@
         this.excludes = theExcludes;
     }
 
-    /**
-     * @return the message
-     */
-    public String getMessage()
-    {
-        return this.message;
-    }
-
-    /**
-     * @param theMessage the message to set
-     */
-    public void setMessage( String theMessage )
-    {
-        this.message = theMessage;
-    }
-
-    /**
-     * @return the searchTransitive
-     */
-    public boolean isSearchTransitive()
-    {
-        return this.searchTransitive;
-    }
-
-    /**
-     * @param theSearchTransitive the searchTransitive to set
-     */
-    public void setSearchTransitive( boolean theSearchTransitive )
-    {
-        this.searchTransitive = theSearchTransitive;
-    }
 }

Added: maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBanSnapshots.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBanSnapshots.java?view=auto&rev=553707
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBanSnapshots.java (added)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBanSnapshots.java Thu Jul  5 19:53:06 2007
@@ -0,0 +1,87 @@
+package org.apache.maven.plugin.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.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.plugin.testing.ArtifactStubFactory;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
+
+/**
+ * 
+ * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
+ * 
+ */
+public class TestBanSnapshots
+    extends TestCase
+{
+
+    public void testRule()
+        throws IOException
+    {
+        ArtifactStubFactory factory = new ArtifactStubFactory();
+        MockProject project = new MockProject();
+        EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
+        project.setArtifacts( factory.getMixedArtifacts() );
+        project.setDependencyArtifacts( factory.getScopedArtifacts() );
+        BanSnapshots rule = new BanSnapshots();
+
+        rule.setSearchTransitive( false );
+
+        execute( rule, helper, false );
+
+        rule.setSearchTransitive( true );
+
+        execute( rule, helper, true );
+
+    }
+
+    /**
+     * Simpler wrapper to execute and deal with the expected
+     * result.
+     * 
+     * @param rule
+     * @param helper
+     * @param shouldFail
+     */
+    private void execute( BanSnapshots rule, EnforcerRuleHelper helper, boolean shouldFail )
+    {
+        try
+        {
+            rule.message = null;
+            rule.execute( helper );
+            if ( shouldFail )
+            {
+                fail( "Exception expected." );
+            }
+        }
+        catch ( EnforcerRuleException e )
+        {
+            if ( !shouldFail )
+            {
+                fail( "No Exception expected:" + e.getLocalizedMessage() );
+            }
+            // helper.getLog().debug(e.getMessage());
+        }
+    }
+}

Modified: maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBannedDependencies.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBannedDependencies.java?view=diff&rev=553707&r1=553706&r2=553707
==============================================================================
--- maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBannedDependencies.java (original)
+++ maven/plugins/trunk/maven-enforcer-plugin/src/test/java/org/apache/maven/plugin/enforcer/TestBannedDependencies.java Thu Jul  5 19:53:06 2007
@@ -25,7 +25,6 @@
 import junit.framework.TestCase;
 
 import org.apache.maven.plugin.testing.ArtifactStubFactory;
-import org.apache.maven.shared.enforcer.rule.api.EnforcerRule;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleException;
 import org.apache.maven.shared.enforcer.rule.api.EnforcerRuleHelper;
 
@@ -41,7 +40,7 @@
     public void testRule()
         throws IOException
     {
-        ArtifactStubFactory factory = new ArtifactStubFactory( null, false );
+        ArtifactStubFactory factory = new ArtifactStubFactory();
         MockProject project = new MockProject();
         EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
         project.setArtifacts( factory.getMixedArtifacts() );