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 1 commit
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.rules=bannedPlugins
invoker.buildResult = failure
72 changes: 72 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,72 @@
<?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.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
<executions>
<execution>
<id>local-ip</id>
<goals>
<goal>local-ip</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>enforce</goal>
</goals>
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
<configuration>
<rules>
<bannedPlugins>
<excludes>
<exclude>org.codehaus.mojo:build-helper-maven-plugin</exclude>
</excludes>
</bannedPlugins>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</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.codehaus.mojo:build-helper-maven-plugin:maven-plugin:3.6.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
Loading