Skip to content

Commit

Permalink
[MENFORCER-455] New Enforcer API
Browse files Browse the repository at this point in the history
- New API based on JSR 330 components
  • Loading branch information
slawekjaranowski committed Jan 1, 2023
1 parent edfd5b1 commit 8784339
Show file tree
Hide file tree
Showing 80 changed files with 1,395 additions and 425 deletions.
23 changes: 23 additions & 0 deletions enforcer-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>custom-rule</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>pre-site</phase>
<configuration>
<outputDirectory>${project.build.directory}/custom-rule-sample</outputDirectory>
<escapeString>\</escapeString>
<resources>
<resource>
<directory>src/custom-rule-sample</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
27 changes: 13 additions & 14 deletions enforcer-api/src/custom-rule-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,45 @@
* under the License.
*
-->
<!-- START SNIPPET: project-pom -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>custom-rule</groupId>
<artifactId>custom-rule-sample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

<name>My Custom Rule</name>
<description>This is my custom rule.</description>

<properties>
<api.version>${project.version}</api.version>
<maven.version>3.0</maven.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer-api</artifactId>
<version>${api.version}</version>
<version>\${api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
<version>\${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${maven.version}</version>
<version>\${maven.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>

</project>
<!-- END SNIPPET: project-pom -->
19 changes: 3 additions & 16 deletions enforcer-api/src/main/assembly/custom-rule-sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,18 @@
* under the License.
*
-->
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 https://maven.apache.org/xsd/assembly-2.1.0.xsd">
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>sample</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/custom-rule-sample</directory>
<directory>target/custom-rule-sample</directory>
<outputDirectory>/custom-rule-sample</outputDirectory>
</fileSet>
</fileSets>
<files>
<!-- filters custom-rule's pom.xml to replace 'api.version' property with the current project version -->
<file>
<source>src/custom-rule-sample/pom.xml</source>
<outputDirectory>/custom-rule-sample</outputDirectory>
<filtered>true</filtered>
</file>
<file>
<source>src/custom-rule-sample/usage-pom.xml</source>
<outputDirectory>/custom-rule-sample</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.apache.maven.enforcer.rule.api;

/**
* Entry point for custom {@code Enforcer Rule}.
*
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public abstract class AbstractEnforcerRule implements EnforcerRuleBase {

/**
* EnforcerLogger instance
*/
private EnforcerLogger log;

/**
* Enforcer Rule execution level
*/
private EnforcerLevel level = EnforcerLevel.ERROR;

/**
* Used by {@code EnforcerMojo} to inject logger instance
*
* @param log an {@link EnforcerLogger} instance
*/
public void setLog(EnforcerLogger log) {
this.log = log;
}

/**
* Provide an {@link EnforcerLogger} instance for Rule
*
* @return an {@link EnforcerLogger} instance
*/
public EnforcerLogger getLog() {
return log;
}

/**
* Current Enforcer execution level
*
* @return an Enforcer execution level
*/
public EnforcerLevel getLevel() {
return level;
}

/**
* If the rule is to be cached during session scope, whole executing of Maven build,
* this id is used as part of the key.
* <p>
* Rule of the same class and the same cache id will be executed once.
*
* @return id to be used by the Enforcer to determine uniqueness of cache results.
* Return {@code null} disable cache of rule executing.
*/
public String getCacheId() {
return null;
}

/**
* This is the interface into the rule. This method should throw an exception
* containing a reason message if the rule fails the check. The plugin will
* then decide based on the fail flag and rule level if it should stop or just log the
* message as a warning.
*
* @throws EnforcerRuleException the enforcer rule exception
* @throws EnforcerRuleError in order to brake a build immediately
*/
public abstract void execute() throws EnforcerRuleException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.apache.maven.enforcer.rule.api;

import java.util.function.Supplier;

/**
* Logger used by enforcer rule.
*
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public interface EnforcerLogger {

/**
* Log message in {@code warn} or {@code error} level according to current rule {@link EnforcerLevel}.
*
* @param message a massage to log
*/
void warnOrError(CharSequence message);

/**
* Log message in {@code warn} or {@code error} level according to current rule {@link EnforcerLevel}.
* <p>
* {@code messageSupplier} will be evaluate only when corresponding log level is enabled.
*
* @param messageSupplier a supplier for message to log
*/
void warnOrError(Supplier<CharSequence> messageSupplier);

/**
* Log message in {@code debug} level.
*
* @param message a massage to log
*/
void debug(CharSequence message);

/**
* Log message in {@code debug} level.
* <p>
* {@code messageSupplier} will be evaluate only when corresponding log level is enabled.
*
* @param messageSupplier a supplier for message to log
*/
void debug(Supplier<CharSequence> messageSupplier);

/**
* Log message in {@code info} level.
*
* @param message a massage to log
*/
void info(CharSequence message);

/**
* Log message in {@code info} level.
* <p>
* {@code messageSupplier} will be evaluate only when corresponding log level is enabled.
*
* @param messageSupplier a supplier for message to log
*/
void info(Supplier<CharSequence> messageSupplier);

/**
* Log message in {@code warn} level.
*
* @param message a massage to log
*/
void warn(CharSequence message);

/**
* Log message in {@code warn} level.
* <p>
* {@code messageSupplier} will be evaluate only when corresponding log level is enabled.
*
* @param messageSupplier a supplier for message to log
*/
void warn(Supplier<CharSequence> messageSupplier);

/**
* Log message in {@code error} level.
*
* @param message a massage to log
*/
void error(CharSequence message);

/**
* Log message in {@code error} level.
* <p>
* {@code messageSupplier} will be evaluate only when corresponding log level is enabled.
*
* @param messageSupplier a supplier for message to log
*/
void error(Supplier<CharSequence> messageSupplier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author <a href="mailto:brianf@apache.org">Brian Fox</a>
*/
public interface EnforcerRule {
public interface EnforcerRule extends EnforcerRuleBase {

/**
* This is the interface into the rule. This method should throw an exception
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.apache.maven.enforcer.rule.api;

/**
* Base interface for old and new API.
* <p>
* Used for internal purpose.
*
* @author Slawomir Jaranowski
* @since 3.2.0
*/
public interface EnforcerRuleBase {}

0 comments on commit 8784339

Please sign in to comment.