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-454] Deprecate 'rules' property and introduce 'enforcer.rules' as a replacement #211

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
5 changes: 5 additions & 0 deletions maven-enforcer-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
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 -Drules=AlwaysFail
invoker.buildResult = failure
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<AlwaysPass/> <!-- Should be replaced by 'AlwaysFail' via command line property -->
</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('[INFO] Always pass!')
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:enforce -Drules=alwaysFail
invoker.goals = validate -Denforcer.rules=AlwaysFail
invoker.buildResult = failure
14 changes: 14 additions & 0 deletions maven-enforcer-plugin/src/it/projects/cli-always-fail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ under the License.
<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>
<configuration>
<rules>
<AlwaysPass/> <!-- Should be replaced by 'AlwaysFail' via command line property -->
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
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
@@ -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 -Drules=AlwaysPass
invoker.buildResult = success
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<AlwaysFail/> <!-- Should be replaced by 'AlwaysPass' via command line property -->
</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('[INFO] Always pass!')
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:enforce -Drules=alwaysPass
invoker.goals = validate -Denforcer.rules=AlwaysPass
invoker.buildResult = success
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ under the License.
</goals>
<configuration>
<rules>
<AlwaysPass/>
<AlwaysFail/> <!-- Should be replaced by 'AlwaysPass' via command line property -->
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
Expand Down
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 @@ -118,20 +118,14 @@ public class EnforceMojo extends AbstractMojo {
* &lt;message&gt;message for rule&lt;/message&gt;
* &lt;/alwaysPass&gt;
* &lt;myRule implementation="org.example.MyRule"/&gt;
* &lt;/rules>
* &lt;/rules&gt;
* </pre>
*
* @since 1.0.0
*/
@Parameter
private PlexusConfiguration rules;

/**
* List of strings that matches the EnforcerRules to execute.
*/
@Parameter(required = false, property = "rules")
private List<String> commandLineRules;

/**
* List of strings that matches the EnforcerRules to skip.
*
Expand All @@ -153,6 +147,36 @@ public class EnforceMojo extends AbstractMojo {
@Component
private EnforcerRuleManager enforcerRuleManager;

private List<String> rulesToExecute;

/**
* List of strings that matches the EnforcerRules to execute. Replacement for the <code>rules</code> property.
*
* @since 3.2.0
*/
@Parameter(required = false, property = "enforcer.rules")
public void setRulesToExecute(List<String> rulesToExecute) throws MojoExecutionException {
if (rulesToExecute != null && !rulesToExecute.isEmpty()) {
if (this.rulesToExecute != null && !this.rulesToExecute.isEmpty()) {
throw new MojoExecutionException("Detected the usage of both '-Drules' (which is deprecated) "
+ "and '-Denforcer.rules'. Please use only one of them, preferably '-Denforcer.rules'.");
}
this.rulesToExecute = rulesToExecute;
}
}

/**
* List of strings that matches the EnforcerRules to execute.
*
* @deprecated Use <code>enforcer.rules</code> property instead
*/
@Parameter(required = false, property = "rules")
@Deprecated
public void setCommandLineRules(List<String> rulesToExecute) throws MojoExecutionException {
getLog().warn("Detected the usage of property '-Drules' which is deprecated. Use '-Denforcer.rules' instead.");
Copy link
Member

Choose a reason for hiding this comment

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

Maven 3.9 will take care about it and warn about deprecated items ... but can be now

setRulesToExecute(rulesToExecute);
}
psiroky marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void execute() throws MojoExecutionException {
Log log = this.getLog();
Expand Down Expand Up @@ -274,21 +298,19 @@ public void execute() throws MojoExecutionException {
*/
private Optional<PlexusConfiguration> createRulesFromCommandLineOptions() {

if (commandLineRules == null || commandLineRules.isEmpty()) {
if (rulesToExecute == null || rulesToExecute.isEmpty()) {
return Optional.empty();
}

PlexusConfiguration configuration = new DefaultPlexusConfiguration("rules");

for (String rule : commandLineRules) {
for (String rule : rulesToExecute) {
configuration.addChild(new DefaultPlexusConfiguration(rule));
}

return Optional.of(configuration);
}

/**
* Filter our (remove) rules that have been specifically skipped via additional configuration.
* Filter out (remove) rules that have been specifically skipped via additional configuration.
*
* @param allRules list of enforcer rules to go through and filter
*
Expand Down
Loading