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.
 package org.apache.maven.plugins.enforcer;
 import java.util.*;

This rule verifies if dependency management is used.

 @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");
     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())) {
                 Dependency depMgtDependency = depMgtMap.get(dependency.getManagementKey());
                 if (depMgtDependency == null) {
                     sb.append(String.format("%s is not managed by dependency management",
                 } 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()));
             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.

dependencies list of dependencies
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;
    public boolean isCacheable() {
        return false;
    public boolean isResultValid(EnforcerRule enforcerRule) {
        return false;
    public String getCacheId() {
        return String.valueOf( ? 0 : 1);
New to GrepCode? Check out our FAQ X