maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rfscho...@apache.org
Subject svn commit: r1488785 - in /maven/enforcer/trunk: enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/ maven-enforcer-plugin/src/it/ban-pom-dependency-version/
Date Sun, 02 Jun 2013 20:55:44 GMT
Author: rfscholte
Date: Sun Jun  2 20:55:44 2013
New Revision: 1488785

URL: http://svn.apache.org/r1488785
Log:
[MENFORCER-152] Add Rule: BanDuplicatePomDependencyVersions
See also MNG-5091: Add option to fail build if WARNING's appear in POM

Added:
    maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicatePomDependencyVersions.java
    maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/
    maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/invoker.properties
    maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/pom.xml
    maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/verify.groovy

Added: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicatePomDependencyVersions.java
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicatePomDependencyVersions.java?rev=1488785&view=auto
==============================================================================
--- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicatePomDependencyVersions.java
(added)
+++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicatePomDependencyVersions.java
Sun Jun  2 20:55:44 2013
@@ -0,0 +1,153 @@
+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.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
+import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
+import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Model;
+import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+/**
+ * Since Maven 3 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique.
+ * Early versions of Maven 3 already warn, this rule can force to break a build for this
reason. 
+ * 
+ * @author Robert Scholte
+ * @since 1.3
+ *
+ */
+public class BanDuplicatePomDependencyVersions
+    extends AbstractNonCacheableEnforcerRule
+{
+
+    public void execute( EnforcerRuleHelper helper )
+        throws EnforcerRuleException
+    {
+        // get the project
+        MavenProject project;
+        try
+        {
+            project = (MavenProject) helper.evaluate( "${project}" );
+        }
+        catch ( ExpressionEvaluationException eee )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", eee
);
+        }
+        
+        
+        // re-read model, because M3 uses optimized model
+        MavenXpp3Reader modelReader = new MavenXpp3Reader();
+        FileReader pomReader = null;
+        Model model;
+        try
+        {
+            pomReader = new FileReader( project.getFile() );
+
+            model = modelReader.read( pomReader );
+        }
+        catch ( FileNotFoundException e )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", e );
+        }
+        catch ( IOException e )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", e );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new EnforcerRuleException( "Unable to retrieve the MavenProject: ", e );
+        }
+        finally
+        {
+            IOUtil.close( pomReader );
+        }
+
+        // @todo reuse ModelValidator when possible
+        
+//        Object modelValidator = null;
+//        try
+//        {
+//            modelValidator = helper.getComponent( "org.apache.maven.model.validation.ModelValidator"
);
+//        }
+//        catch ( ComponentLookupException e1 )
+//        {
+//            // noop
+//        }
+
+
+//        if( modelValidator == null )
+//        {
+            maven2Validation( helper, model );            
+//        }
+//        else
+//        {
+//        }
+    }
+
+    private void maven2Validation( EnforcerRuleHelper helper, Model model )
+        throws EnforcerRuleException
+    {
+        Map<String, Integer> depMap = new HashMap<String, Integer>();
+        Set<String> duplicateDeps = new HashSet<String>();
+
+        @SuppressWarnings( "unchecked" )
+        List<Dependency> dependencies = model.getDependencies();
+        for ( Dependency dependency : dependencies )
+        {
+            String key = dependency.getManagementKey();
+
+            helper.getLog().debug( "verify " + key );
+
+            int times = 0;
+            if ( depMap.containsKey( key ) )
+            {
+                times = depMap.get( key );
+                duplicateDeps.add( key );
+            }
+            depMap.put( key, times + 1 );
+        }
+
+        if ( !duplicateDeps.isEmpty() )
+        {
+            StringBuilder message = new StringBuilder();
+            message.append( "Found " ).append( duplicateDeps.size() ).append( " duplicate
" );
+            message.append( duplicateDeps.size() == 1 ? "dependency" : "dependencies" ).append(
" in this project:\n" );
+            for ( String key : duplicateDeps )
+            {
+                message.append( " - " ).append( key ).append( " ( " ).append( depMap.get(
key ) ).append( " times )\n" );
+            }
+            throw new EnforcerRuleException( message.toString() );
+        }
+    }
+
+}

Added: maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/invoker.properties
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/invoker.properties?rev=1488785&view=auto
==============================================================================
--- maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/invoker.properties
(added)
+++ maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/invoker.properties
Sun Jun  2 20:55:44 2013
@@ -0,0 +1 @@
+invoker.buildResult=failure
\ No newline at end of file

Added: maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/pom.xml
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/pom.xml?rev=1488785&view=auto
==============================================================================
--- maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/pom.xml (added)
+++ maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/pom.xml Sun
Jun  2 20:55:44 2013
@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.enforcer</groupId>
+  <artifactId>ban-pom-dependency-version</artifactId>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <banDuplicatePomDependencyVersions/>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>3.0</version>
+    </dependency>  
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>3.0</version>
+    </dependency>  
+  </dependencies>
+
+</project>

Added: maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/verify.groovy
URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/verify.groovy?rev=1488785&view=auto
==============================================================================
--- maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/verify.groovy
(added)
+++ maven/enforcer/trunk/maven-enforcer-plugin/src/it/ban-pom-dependency-version/verify.groovy
Sun Jun  2 20:55:44 2013
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+File buildLog = new File( basedir, 'build.log' )
+assert buildLog.text.contains( '[WARNING] Rule 0: org.apache.maven.plugins.enforcer.BanDuplicatePomDependencyVersions
failed with message:' )
+assert buildLog.text.contains( 'Found 1 duplicate dependency in this project:' )
+assert buildLog.text.contains( '- org.codehaus.plexus:plexus-utils:jar ( 2 times )' )
+
+
+
+ 
\ No newline at end of file



Mime
View raw message