yesamer commented on code in PR #6633: URL: https://github.com/apache/incubator-kie-drools/pull/6633#discussion_r3279950930
########## kie-no-dependency-management-enforcer-rule/src/main/java/org/kie/nodependencymanagementrule/NoDependencyManagementRule.java: ########## @@ -0,0 +1,112 @@ +/* + * 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. + */ +package org.kie.nodependencymanagementrule; + +import java.util.Objects; + +import javax.inject.Inject; +import javax.inject.Named; + +import java.util.Set; +import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule; +import org.apache.maven.enforcer.rule.api.EnforcerRuleException; +import org.apache.maven.model.DependencyManagement; +import org.apache.maven.model.Profile; +import org.apache.maven.project.MavenProject; + +/** + * No DependencyManagement Enforcer Rule + * This rule is meant to forbid dependencyManagement tag in pom + */ +@Named("noDependencyManagementRule") +public class NoDependencyManagementRule extends AbstractEnforcerRule { + + // Inject needed Maven components + private final MavenProject project; + + /** + * Set of allowed poms. + */ + private Set<String> allowedPoms; + + @Inject + public NoDependencyManagementRule(MavenProject project) { + this.project = Objects.requireNonNull(project); + } + + public void execute() throws EnforcerRuleException { + if (!isAllowed(project.getGroupId(), project.getArtifactId())) { + checkForDependencyManagement(); + } + } + + void checkForDependencyManagement() throws EnforcerRuleException { + checkDependencyManagementInProject(); + checkDependencyManagementInProfiles(); + } + + void checkDependencyManagementInProject() throws EnforcerRuleException { + if (invalidDependencyManagement(project.getDependencyManagement())) { // The getLocation("") retrieve the position, in the pom, of that specific dependencyManagement element; it is null when such an element is inherited + throw new EnforcerRuleException(String.format("The current pom %s:%s:%s has dependencyManagement tag!", project.getGroupId(), project.getArtifactId(), project.getVersion())); + } + } + + void checkDependencyManagementInProfiles() throws EnforcerRuleException { + if (project.getModel().getProfiles() != null) { + for (Profile profile : project.getModel().getProfiles()) { + checkDependencyManagementInProfile(profile); + } + } + } + + void checkDependencyManagementInProfile(Profile profile) throws EnforcerRuleException { Review Comment: Minor: a tab should be removed for better formatting. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
