kwin commented on code in PR #179: URL: https://github.com/apache/maven-enforcer/pull/179#discussion_r951459330
########## enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequireExplicitDependencyScope.java: ########## @@ -0,0 +1,87 @@ +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.text.ChoiceFormat; +import java.util.List; + +import org.apache.maven.enforcer.rule.api.EnforcerRule2; +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.project.MavenProject; +import org.apache.maven.shared.utils.logging.MessageBuilder; +import org.apache.maven.shared.utils.logging.MessageUtils; +import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; + +/** + * Checks that all dependencies have an explicitly declared scope in the non-effective pom (i.e. without taking + * inheritance or dependency management into account). + */ +public class RequireExplicitDependencyScope + extends AbstractNonCacheableEnforcerRule + implements EnforcerRule2 +{ + + @Override + public void execute( EnforcerRuleHelper helper ) + throws EnforcerRuleException + { + try + { + int numMissingDependencyScopes = 0; + MavenProject project = (MavenProject) helper.evaluate( "${project}" ); + if ( project == null ) + { + throw new ExpressionEvaluationException( "${project} is null" ); + } + List<Dependency> dependencies = project.getOriginalModel().getDependencies(); // this is the non-effective + // model but the original one + // without inheritance and + // interpolation resolved + // check scope without considering inheritance + for ( Dependency dependency : dependencies ) + { + helper.getLog().debug( "Found dependency " + dependency ); + if ( dependency.getScope() == null ) + { + MessageBuilder msgBuilder = MessageUtils.buffer(); + helper.getLog().warn( msgBuilder Review Comment: I fixed this in https://github.com/apache/maven-enforcer/pull/179/commits/d21ae7d9f3a462c5570af4c51b11c257a7bf3bbe. I don't want to rely on aggregate messages in the exception as that prevent using coloured output from the message builder. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org