View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  
24  import java.text.ChoiceFormat;
25  import java.util.List;
26  import java.util.Objects;
27  
28  import org.apache.maven.enforcer.rule.api.EnforcerLevel;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.model.Dependency;
31  import org.apache.maven.project.MavenProject;
32  
33  /**
34   * Checks that all dependencies have an explicitly declared scope in the non-effective pom (i.e. without taking
35   * inheritance or dependency management into account).
36   */
37  @Named("requireExplicitDependencyScope")
38  public final class RequireExplicitDependencyScope extends AbstractStandardEnforcerRule {
39  
40      private final MavenProject project;
41  
42      @Inject
43      public RequireExplicitDependencyScope(MavenProject project) {
44          this.project = Objects.requireNonNull(project);
45      }
46  
47      @Override
48      public void execute() throws EnforcerRuleException {
49          int numMissingDependencyScopes = 0;
50          List<Dependency> dependencies = project.getOriginalModel().getDependencies(); // this is the non-effective
51          // model but the original one
52          // without inheritance and
53          // interpolation resolved
54          // check scope without considering inheritance
55          for (Dependency dependency : dependencies) {
56              getLog().debug("Found dependency " + dependency);
57              if (dependency.getScope() == null) {
58                  getLog().warnOrError(() -> new StringBuilder()
59                          .append("Dependency ")
60                          .append(dependency.getManagementKey())
61                          .append(" @ ")
62                          .append(formatLocation(project, dependency.getLocation("")))
63                          .append(" does not have an explicit scope defined!"));
64                  numMissingDependencyScopes++;
65              }
66          }
67          if (numMissingDependencyScopes > 0) {
68              ChoiceFormat scopesFormat = new ChoiceFormat("1#scope|1<scopes");
69              String logCategory = getLevel() == EnforcerLevel.ERROR ? "errors" : "warnings";
70              throw new EnforcerRuleException("Found " + numMissingDependencyScopes + " missing dependency "
71                      + scopesFormat.format(numMissingDependencyScopes)
72                      + ". Look at the " + logCategory + " emitted above for the details.");
73          }
74      }
75  }