001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.enforcer.rules;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.text.ChoiceFormat;
025import java.util.List;
026import java.util.Objects;
027
028import org.apache.maven.enforcer.rule.api.EnforcerLevel;
029import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
030import org.apache.maven.model.Dependency;
031import org.apache.maven.project.MavenProject;
032
033/**
034 * Checks that all dependencies have an explicitly declared scope in the non-effective pom (i.e. without taking
035 * inheritance or dependency management into account).
036 */
037@Named("requireExplicitDependencyScope")
038public final class RequireExplicitDependencyScope extends AbstractStandardEnforcerRule {
039
040    private final MavenProject project;
041
042    @Inject
043    public RequireExplicitDependencyScope(MavenProject project) {
044        this.project = Objects.requireNonNull(project);
045    }
046
047    @Override
048    public void execute() throws EnforcerRuleException {
049        int numMissingDependencyScopes = 0;
050        List<Dependency> dependencies = project.getOriginalModel().getDependencies(); // this is the non-effective
051        // model but the original one
052        // without inheritance and
053        // interpolation resolved
054        // check scope without considering inheritance
055        for (Dependency dependency : dependencies) {
056            getLog().debug("Found dependency " + dependency);
057            if (dependency.getScope() == null) {
058                getLog().warnOrError(() -> new StringBuilder()
059                        .append("Dependency ")
060                        .append(dependency.getManagementKey())
061                        .append(" @ ")
062                        .append(formatLocation(project, dependency.getLocation("")))
063                        .append(" does not have an explicit scope defined!"));
064                numMissingDependencyScopes++;
065            }
066        }
067        if (numMissingDependencyScopes > 0) {
068            ChoiceFormat scopesFormat = new ChoiceFormat("1#scope|1<scopes");
069            String logCategory = getLevel() == EnforcerLevel.ERROR ? "errors" : "warnings";
070            throw new EnforcerRuleException("Found " + numMissingDependencyScopes + " missing dependency "
071                    + scopesFormat.format(numMissingDependencyScopes)
072                    + ". Look at the " + logCategory + " emitted above for the details.");
073        }
074    }
075}