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-516] Use rule configuration in POM when executed from CLI #344

Merged
merged 3 commits into from
Jan 15, 2025
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 = enforcer:enforce -Denforcer.rules=bannedPlugins
invoker.buildResult = failure
56 changes: 56 additions & 0 deletions maven-enforcer-plugin/src/it/projects/cli-banned-plugins/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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>test</artifactId>
<version>1.0</version>

<description>
</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins.enforcer.its</groupId>
<artifactId>menforcer126_maven-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<configuration>
<rules>
<bannedPlugins>
<excludes>
<exclude>org.apache.maven.plugins.enforcer.its:menforcer126_maven-plugin</exclude>
</excludes>
</bannedPlugins>
</rules>
<fail>true</fail>
</configuration>
</plugin>
</plugins>
</build>
</project>
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('org.apache.maven.plugins.enforcer.its:menforcer126_maven-plugin:maven-plugin:1.0 <--- banned plugin')
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,23 @@ private Optional<PlexusConfiguration> createRulesFromCommandLineOptions() {

PlexusConfiguration configuration = new DefaultPlexusConfiguration("rules");
for (String rule : rulesToExecute) {
configuration.addChild(new DefaultPlexusConfiguration(rule));
PlexusConfiguration configuredRule = null;
// Check if there's configuration in the project for this rule and use it if so
if (rules != null) {
// rule names haven't been normalized yet, so check both with first character upper and lower
String ruleLower = Character.toLowerCase(rule.charAt(0)) + rule.substring(1);
String ruleUpper = Character.toUpperCase(rule.charAt(0)) + rule.substring(1);
configuredRule = rules.getChild(ruleLower, false);
if (configuredRule == null) {
configuredRule = rules.getChild(ruleUpper, false);
}
}

if (configuredRule != null) {
configuration.addChild(configuredRule);
} else {
configuration.addChild(new DefaultPlexusConfiguration(rule));
}
}
return Optional.of(configuration);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,80 @@ Check specific rule via CLI
mvn enforcer:enforce -Denforcer.rules=alwaysPass,alwaysFail
+---+

Many rules require configuration to be meaningful. If configuration for a rule is defined in the POM, it will be used.

Note that if configuration for a rule is defined within an execution, the execution ID must be specified on the command line.

For example, given the following plugin configuration:

+---+
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${project.version}</version>
<configuration>
<rules>
<requireMavenVersion>
<version>3.0</version>
</requireMavenVersion>
</rules>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
+---+

You can run this rule with the defined configuration:

+---+
mvn enforcer:enforce -Denforcer.rules=requireMavenVersion
+---+


However, if your plugin configuration is within an execution context:

+---+
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>3.0</version>
</requireMavenVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
+---+

You must name the execution ID on the command line for the configuration to apply:

+---+
mvn enforcer:enforce@enforce-maven -Denforcer.rules=requireMavenVersion
+---+


A full list of built-in rules can be found {{{../../../enforcer/enforcer-rules/index.html}here}}.
Loading