Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-431] Introduce option to skip specific rules via custom property #205

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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.

invoker.goals = validate -Denforcer.skipRules=alwaysFail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • please add verify script yo be sure that rule alwaysPass was executed

53 changes: 53 additions & 0 deletions maven-enforcer-plugin/src/it/projects/cli-skip-rules/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
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.
-->

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.its.enforcer</groupId>
<artifactId>minimal-skip-rules-test</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<description>Minimal test for skipping specific rules via command line</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>always-pass-and-fail</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<alwaysPass/>
<alwaysFail/>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
20 changes: 20 additions & 0 deletions maven-enforcer-plugin/src/it/projects/cli-skip-rules/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/
def buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('[INFO] Always pass!')
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.maven.enforcer.rule.api.EnforcerLevel;
import org.apache.maven.enforcer.rule.api.EnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRule2;
Expand Down Expand Up @@ -131,6 +133,14 @@ public class EnforceMojo extends AbstractMojo {
@Parameter(required = false, property = "rules")
private String[] commandLineRules;
psiroky marked this conversation as resolved.
Show resolved Hide resolved

/**
* Array of Strings that matches the EnforcerRules to skip.
*
* @since 3.2.0
*/
@Parameter(required = false, property = "enforcer.skipRules")
private String[] rulesToSkip;

/**
* Use this flag to disable rule result caching. This will cause all rules to execute on each project even if the
* rule indicates it can safely be cached.
Expand All @@ -154,11 +164,11 @@ public void execute() throws MojoExecutionException {
}

Optional<PlexusConfiguration> rulesFromCommandLine = createRulesFromCommandLineOptions();

List<EnforcerRuleDesc> rulesList;
try {
// current behavior - rules from command line override all other configured rules.
rulesList = enforcerRuleManager.createRules(rulesFromCommandLine.orElse(rules));
List<EnforcerRuleDesc> allRules = enforcerRuleManager.createRules(rulesFromCommandLine.orElse(rules));
rulesList = filterOutSkippedRules(allRules);
} catch (EnforcerRuleManagerException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
Expand All @@ -176,8 +186,6 @@ public void execute() throws MojoExecutionException {
// messages with warn/error flag
Map<String, Boolean> messages = new LinkedHashMap<>();

String currentRule = "Unknown";

// create my helper
PluginParameterExpressionEvaluator evaluator = new PluginParameterExpressionEvaluator(session, mojoExecution);
EnforcerRuleHelper helper = new DefaultEnforcementRuleHelper(session, evaluator, log, container);
Expand All @@ -200,7 +208,7 @@ public void execute() throws MojoExecutionException {
EnforcerLevel level = getLevel(rule);
// store the current rule for
// logging purposes
currentRule = rule.getClass().getName();
String currentRule = rule.getClass().getName();
try {
if (ignoreCache || shouldExecute(rule)) {
// execute the rule
Expand Down Expand Up @@ -254,12 +262,10 @@ public void execute() throws MojoExecutionException {
}
});

// CHECKSTYLE_OFF: LineLength
if (fail && hasErrors) {
throw new MojoExecutionException(
"Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed.");
}
// CHECKSTYLE_ON: LineLength
}

/**
Expand All @@ -282,6 +288,22 @@ private Optional<PlexusConfiguration> createRulesFromCommandLineOptions() {
return Optional.of(configuration);
}

/**
* Filter our (remove) rules that have been specifically skipped via additional configuration.
*
* @param allRules list of enforcer rules to go through and filter
*
* @return list of filtered rules
*/
private List<EnforcerRuleDesc> filterOutSkippedRules(List<EnforcerRuleDesc> allRules) {
if (rulesToSkip == null || rulesToSkip.length == 0) {
return allRules;
}
return allRules.stream()
.filter(ruleDesc -> !ArrayUtils.contains(rulesToSkip, ruleDesc.getName()))
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());
}

/**
* This method determines if a rule should execute based on the cache
*
Expand Down