~~ 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. ------ Writing a custom rule ------ Brian Fox ------ 2007-09-01 ------ Writing a custom rule Custom rules are easy to make with the <<>>. These rules can then be invoked with the {{{http://maven.apache.org/plugins/maven-enforcer-plugin/}maven-enforcer-plugin}}. Note: The files shown below may be downloaded here: {{{./custom-rule.zip}custom-rule.zip}} * Project with custom Enforcer Rule First make a new jar project starting with the sample pom below: %{snippet|id=project-pom|file=enforcer-api/target/custom-rule-sample/pom.xml} Target bytecode version of rule must be <<<1.8>>> or <<<11>>> - due to {{{https://issues.apache.org/jira/browse/MENFORCER-473}MENFORCER-473}}. Note that the classloader is shared with the embedding <<>> (a regular {{{https://maven.apache.org/guides/mini/guide-maven-classloading.html#Plugin_Classloaders}plugin classloader}}) and therefore the artifacts <<>> are always loaded in the same version as the embedding <<>>. Custom rule artifacts should therefore only depend on <<>> and core Maven artifacts with <<>> scope (for details refer to {{{https://issues.apache.org/jira/browse/MNG-7097}MNG-7097}}). The classes available from <<>> must not be used from custom rules, as those are not considered API and may change in backwards-incompatible ways with every <<>> version (even minor ones). Other dependencies used by custom rule at run time should have <<>> scope. * Implementation of custom Enforcer Rule The rule must extend {{{./apidocs/org/apache/maven/enforcer/rule/api/AbstractEnforcerRule.html}AbstractEnforcerRule}} (available since API version 3.2.1) and implement its <<>> method. Add annotation <<<@Named("yourRuleName")>>> to your Rule class. Your Rule name must start with lowercase character. In addition, the rule can provide a setter method or simply field for each parameter allowed to be configured in the pom.xml file (like the parameter <<>> shown example). Maven component can be injected into Rule by annotation <<<@Inject>>> on field or constructor. Entry point for Rule executing is <<>> method, tf the rule succeeds, it should just simply return. If the rule fails, it should throw an {{{./apidocs/org/apache/maven/enforcer/rule/api/EnforcerRuleException.html}EnforcerRuleException}} with a descriptive message telling the user why the rule failed. Enforcer plugin takes decision based on configuration and Enforcer Rule level whether build should pass or fail. In case when you want to brake build immediately, <<>> method can throw an {{{./apidocs/index.html}EnforcerRuleError}}. Here's a sample class: %{snippet|id=rule-implementation|file=enforcer-api/target/custom-rule-sample/src/main/java/org/example/custom/rule/MyCustomRule.java} * Using custom Rule * Build and Install or Deploy your custom rule. * Add your custom-rule artifact as a dependency of the <<>> in your build. * Add your rule to the configuration section of the <<>>, the name used in <<<@Named>>> annotation of your Rule will be the name of the rule. [] That's it. The full plugin config may look like this: %{snippet|id=usage-pom|file=enforcer-api/target/custom-rule-sample/usage-pom.xml}