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-455] New Enforcer API #216

Merged
merged 1 commit into from
Jan 4, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
51 changes: 35 additions & 16 deletions enforcer-api/src/custom-rule-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,65 @@
* 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 https://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>
<maven.version>${maven.version}</maven.version>
<!-- use JDK 1.8 at least -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</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>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<!-- generate index of project components -->
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.9.0.M1</version>
<executions>
<execution>
<goals>
<goal>main-index</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

</project>
<!-- END SNIPPET: project-pom -->

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.
*/

// START SNIPPET: rule-implementation
package org.example.custom.rule;

import javax.inject.Inject;
import javax.inject.Named;

import java.util.List;

import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.rtinfo.RuntimeInformation;

/**
* Custom Enforcer Rule - example
*/
@Named("myCustomRule") // rule name - must start from lowercase character
public class MyCustomRule extends AbstractEnforcerRule {

/**
* Simple param. This rule fails if the value is true.
*/
private boolean shouldIfail = false;

/**
* Rule parameter as list of items.
*/
private List<String> listParameters;

// Inject needed Maven components

@Inject
private MavenProject project;

@Inject
private MavenSession session;

@Inject
private RuntimeInformation runtimeInformation;

public void execute() throws EnforcerRuleException {

getLog().info("Retrieved Target Folder: " + project.getBuild().getDirectory());
getLog().info("Retrieved ArtifactId: " + project.getArtifactId());
getLog().info("Retrieved Project: " + project);
getLog().info("Retrieved Maven version: " + runtimeInformation.getMavenVersion());
getLog().info("Retrieved Session: " + session);
getLog().warnOrError("Parameter shouldIfail: " + shouldIfail);
getLog().info(() -> "Parameter listParameters: " + listParameters);

if (this.shouldIfail) {
throw new EnforcerRuleException("Failing because my param said so.");
}
}

/**
* If your rule is cacheable, you must return a unique id when parameters or conditions
* change that would cause the result to be different. Multiple cached results are stored
* based on their id.
* <p>
* The easiest way to do this is to return a hash computed from the values of your parameters.
* <p>
* If your rule is not cacheable, then you don't need to override this method or return null
*/
@Override
public String getCacheId() {
//no hash on boolean...only parameter so no hash is needed.
return Boolean.toString(shouldIfail);
}

/**
* A good practice is provided toString method for Enforcer Rule.
* <p>
* Output is used in verbose Maven logs, can help during investigate problems.
*
* @return rule description
*/
@Override
public String toString() {
return String.format("MyCustomRule[level=%s, shouldIfail=%b]", getLevel(), shouldIfail);
}
}
// END SNIPPET: rule-implementation