Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2012 Wisen Tanasa
   *
   * Permission is hereby granted, free of charge, to any person obtaining
   * a copy of this software and associated documentation files (the
   * "Software"), to deal in the Software without restriction, including
   * without limitation the rights to use, copy, modify, merge, publish,
   * distribute, sublicense, and/or sell copies of the Software, and to
   * permit persons to whom the Software is furnished to do so, subject to
  * the following conditions:
  *
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
 package org.apache.maven.plugins.enforcer;
 
 
 import java.util.*;

This rule verifies if dependency management is used.

Author(s):
ceilfors
 
 @SuppressWarnings("UnusedDeclaration"// Plugin
 public class RequireDepMgt extends AbstractStandardEnforcerRule {

    
If true, this rule will fail if the version managed by dependency management is being overridden.
 
     public boolean checkVersion;
    
The list of artifact to be ignored in groupId:artifactId:type format.
 
     public List<StringignoreArtifacts = new ArrayList<String>();
    
Specify the scope to be ignored. By default ignoring test scope.
 
     public List<StringignoreScopes = Arrays.asList("test");
 
     @Override
     @SuppressWarnings("unchecked")
     public void execute(final EnforcerRuleHelper helperthrows EnforcerRuleException {
         final String newLine = System.getProperty("line.separator");
 
         try {
             MavenProject project = (MavenProjecthelper.evaluate("${project}");
             DependencyManagement depMgt = project.getDependencyManagement();
             Map<StringDependencydepMgtMap =
                     depMgt == null ? new HashMap<StringDependency>() : getDependencyMap(depMgt.getDependencies());
 
             StringBuilder sb = new StringBuilder();
             List<Dependencydependencies = project.getDependencies();
             for (Dependency dependency : dependencies) {
                 if (.contains(dependency.getScope())
                         || .contains(dependency.getManagementKey())) {
                     continue;
                 }
 
                 Dependency depMgtDependency = depMgtMap.get(dependency.getManagementKey());
                 if (depMgtDependency == null) {
                     sb.append(String.format("%s is not managed by dependency management",
                             dependency.getManagementKey()));
                     sb.append(newLine);
                 } else {
                     if ( && depMgtDependency.getVersion() != null &&
                             !depMgtDependency.getVersion().equals(dependency.getVersion())) {
                         sb.append(String.format("%s version is with %s. Managed version is %s",
                                 dependency.getManagementKey(), dependency.getVersion(), depMgtDependency.getVersion()));
                         sb.append(newLine);
                     }
                 }
             }
 
             if (sb.length() != 0) {
                 throw new EnforcerRuleException(sb.toString() +
                         ( == null ? "Please update the dependency management." : ));
             }
 
         } catch (ExpressionEvaluationException e) {
             throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e);
         }
     }

    
Converts a list of dependencies to a map.

Parameters:
dependencies list of dependencies
Returns:
dependency map with the dependency's management key as its key
    private Map<StringDependencygetDependencyMap(final List<Dependencydependencies) {
        Map<StringDependencydependencyMap = new HashMap<StringDependency>();
        for (Dependency dependency : dependencies) {
            dependencyMap.put(dependency.getManagementKey(), dependency);
        }
        return dependencyMap;
    }
    @Override
    public boolean isCacheable() {
        return false;
    }
    @Override
    public boolean isResultValid(EnforcerRule enforcerRule) {
        return false;
    }
    @Override
    public String getCacheId() {
        return String.valueOf( ? 0 : 1);
    }
New to GrepCode? Check out our FAQ X