maven-enforcer-plugin https://www.e-learn.cn/tag/maven-enforcer-plugin zh-hans How to catch exception in a beanshell? https://www.e-learn.cn/topic/3992810 <span>How to catch exception in a beanshell?</span> <span><span lang="" about="/user/176" typeof="schema:Person" property="schema:name" datatype="">浪尽此生</span></span> <span>2021-01-07 01:34:30</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I can use the evaluateBeanshell rule to enforce some convention: no colon's in directories below src.</p> <pre><code>&lt;plugin&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.4.1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-beanshell&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;evaluateBeanshell&gt; &lt;condition&gt;org.codehaus.plexus.util.FileUtils.getDirectoryNames(new File("src"), "**/*:*", null, false).isEmpty()&lt;/condition&gt; &lt;/evaluateBeanshell&gt; &lt;/rules&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>But some projects don't have an src directory, and the rule will fail hard. I tried setting</p> <pre><code>&lt;condition&gt;org.codehaus.plexus.util.FileUtils.getDirectoryNames(new File("."), "src/**/[^A-Z]*", null, false).isEmpty()&lt;/condition&gt; </code></pre> <p>How do I safely catch the non-existence of the src directory?</p> <br /><h3>回答1:</h3><br /><p>If you want to use Beanshell, you just need something that returns a boolean.</p> <pre><code>&lt;evaluateBeanshell&gt; &lt;condition&gt;new java.io.File("src").exists()&lt;/condition&gt; &lt;/evaluateBeanshell&gt; </code></pre> <p>This will be true if the path exists as a file/folder/symlink, and false otherwise which will break the build.</p> <p>If you want to check multiple aspects, such as if it exists AND if it's a directory, add multiple conditions:</p> <pre><code>&lt;evaluateBeanshell&gt; &lt;condition&gt;new java.io.File("path").exists()&lt;/condition&gt; &lt;condition&gt;new java.io.File("path").isDirectory()&lt;/condition&gt; &lt;/evaluateBeanshell&gt; </code></pre> <p>(Arguably there's little point in checking if it exists then if it's a directory, but helps to illustrate the point)</p> <p>There's plenty more that can be done with Beanshell, if you have to use it. Ultimately though, the 'requireFilesExist' rule is probably a better configuration for the enforcer.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>This does what I need</p> <pre><code>&lt;evaluateBeanshell&gt; &lt;condition&gt; String projectSource = "${project.basedir}" + "/src"; if (org.codehaus.plexus.util.FileUtils.fileExists(projectSource)){ List filenames = org.codehaus.plexus.util.FileUtils.getFileNames( new File(projectSource),"**/*.*",null,false); for (Iterator it = filenames.iterator(); it.hasNext();) { String file = it.next(); String extension = org.codehaus.plexus.util.FileUtils.getExtension(file); if (extension.equals(extension.toLowerCase())) { it.remove(); } else { print("Invalid filename extension (no capitals): " + projectSource + "/" + file); }; }; return filenames.isEmpty(); } else return true; &lt;/condition&gt; &lt;/evaluateBeanshell&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/65307107/how-to-catch-exception-in-a-beanshell</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/beanshell" hreflang="zh-hans">beanshell</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Wed, 06 Jan 2021 17:34:30 +0000 浪尽此生 3992810 at https://www.e-learn.cn enforce custom rule in external projects https://www.e-learn.cn/topic/3210264 <span>enforce custom rule in external projects</span> <span><span lang="" about="/user/154" typeof="schema:Person" property="schema:name" datatype="">主宰稳场</span></span> <span>2020-01-14 05:51:07</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have one Parent maven project and 4 modules in that project:</p> <p></p> <p>Now I want to put some custom rule that says there can be no project versions or dependency versions that begin with the string "master-". This rule will be applicable to all the external projects that depends on eBill Software.</p> <p>So I created a new module with name customRule whose parent is again eBill Software. I followed writing-a-custom-rule and my rule class is in new module.</p> <p>Relevant part of CustomRule.java in module customRule:</p> <pre><code> MavenProject project = (MavenProject) helper.evaluate( "${project}" ); Set&lt;Artifact&gt; artifacts = project.getArtifacts(); boolean containsInvalid = artifacts.stream() .filter(item -&gt; item.getVersion() .startsWith("master-")) .anyMatch(item -&gt; true); if(containsInvalid) { this.shouldIfail = true; } </code></pre> <p>Relevant part of customRule module pom.xml:</p> <pre><code>&lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.4&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-no-master-versions&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;myCustomRule implementation="org.apache.customRule.CustomRule"&gt; &lt;shouldIfail&gt;false&lt;/shouldIfail&gt; &lt;/myCustomRule&gt; &lt;/rules&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; </code></pre> <p>Now I want to access this custom rule in parent module eBill Software:</p> <p>So what should I enter in its pom.xml and external project's pom.xml to be able to get the custom rule applicable to all the external projects that depends on eBill Software.</p> <p>来源:<code>https://stackoverflow.com/questions/48315914/enforce-custom-rule-in-external-projects</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-3" hreflang="zh-hans">maven-3</a></div> <div class="field--item"><a href="/tag/pomxml" hreflang="zh-hans">pom.xml</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Mon, 13 Jan 2020 21:51:07 +0000 主宰稳场 3210264 at https://www.e-learn.cn Run Maven Enforcer Plugin rule on command line https://www.e-learn.cn/topic/2801267 <span>Run Maven Enforcer Plugin rule on command line</span> <span><span lang="" about="/user/118" typeof="schema:Person" property="schema:name" datatype="">不打扰是莪最后的温柔</span></span> <span>2019-12-23 07:27:27</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I would like to enforce the requireReleaseDeps rule of the Maven Enforcer Plugin on a Maven project without any POM configuration simply as a command line call.</p> <p>According to the docs I should be able to just pass in the rules parameter like so</p> <pre><code>mvn enforcer:enforce -Drules=requireReleaseDeps </code></pre> <p>or maybe this should work</p> <pre><code>mvn enforcer:enforce -Drules=org.apache.maven.plugins.enforcer.RequireReleaseDeps </code></pre> <p>However both of these calls result in </p> <pre><code>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce (default-cli) on project hel lo-world: The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce are missing or in valid -&gt; [Help 1] </code></pre> <p>Anybody know if this usage scenario actually works somehow or do I have to dive into debugging the plugin at this stage to figure this out? </p> <br /><h3>回答1:</h3><br /><p>Unfortunately there is currently no such functionality but there is already a JIRA Issue for that.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>It is available from 3.0.0. The Jira issue from accepted answer got resolved: https://issues.apache.org/jira/browse/MENFORCER-142</p> <pre><code>mvn enforcer:enforce -Drules=alwaysPass,alwaysFail </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>Worked for me using Atlassian's fork of the plugin: <code>mvn org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-atlassian-m01:enforce -Drules=...</code></p> <p>I had to declare their plugin repository:</p> <pre class="lang-xml prettyprint-override"><code>&lt;pluginRepositories&gt; .... &lt;pluginRepository&gt; &lt;id&gt;atlassian-3rdparty&lt;/id&gt; &lt;url&gt;https://maven.atlassian.com/3rdparty&lt;/url&gt; &lt;/pluginRepository&gt; &lt;/pluginRepositories&gt; </code></pre> <p>Hope this helps.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>It works with the <code>3.0.0-M3</code> version:</p> <pre><code>mvn org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce -Drules=requireReleaseDeps </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/22157668/run-maven-enforcer-plugin-rule-on-command-line</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Sun, 22 Dec 2019 23:27:27 +0000 不打扰是莪最后的温柔 2801267 at https://www.e-learn.cn Run Maven Enforcer Plugin rule on command line https://www.e-learn.cn/topic/2801228 <span>Run Maven Enforcer Plugin rule on command line</span> <span><span lang="" about="/user/227" typeof="schema:Person" property="schema:name" datatype="">烂漫一生</span></span> <span>2019-12-23 07:27:09</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I would like to enforce the requireReleaseDeps rule of the Maven Enforcer Plugin on a Maven project without any POM configuration simply as a command line call.</p> <p>According to the docs I should be able to just pass in the rules parameter like so</p> <pre><code>mvn enforcer:enforce -Drules=requireReleaseDeps </code></pre> <p>or maybe this should work</p> <pre><code>mvn enforcer:enforce -Drules=org.apache.maven.plugins.enforcer.RequireReleaseDeps </code></pre> <p>However both of these calls result in </p> <pre><code>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce (default-cli) on project hel lo-world: The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce are missing or in valid -&gt; [Help 1] </code></pre> <p>Anybody know if this usage scenario actually works somehow or do I have to dive into debugging the plugin at this stage to figure this out? </p> <br /><h3>回答1:</h3><br /><p>Unfortunately there is currently no such functionality but there is already a JIRA Issue for that.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>It is available from 3.0.0. The Jira issue from accepted answer got resolved: https://issues.apache.org/jira/browse/MENFORCER-142</p> <pre><code>mvn enforcer:enforce -Drules=alwaysPass,alwaysFail </code></pre> <br /><br /><br /><h3>回答3:</h3><br /><p>Worked for me using Atlassian's fork of the plugin: <code>mvn org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-atlassian-m01:enforce -Drules=...</code></p> <p>I had to declare their plugin repository:</p> <pre class="lang-xml prettyprint-override"><code>&lt;pluginRepositories&gt; .... &lt;pluginRepository&gt; &lt;id&gt;atlassian-3rdparty&lt;/id&gt; &lt;url&gt;https://maven.atlassian.com/3rdparty&lt;/url&gt; &lt;/pluginRepository&gt; &lt;/pluginRepositories&gt; </code></pre> <p>Hope this helps.</p> <br /><br /><br /><h3>回答4:</h3><br /><p>It works with the <code>3.0.0-M3</code> version:</p> <pre><code>mvn org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce -Drules=requireReleaseDeps </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/22157668/run-maven-enforcer-plugin-rule-on-command-line</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Sun, 22 Dec 2019 23:27:09 +0000 烂漫一生 2801228 at https://www.e-learn.cn maven custom rule failing or passing without considering custom rule https://www.e-learn.cn/topic/2436910 <span>maven custom rule failing or passing without considering custom rule</span> <span><span lang="" about="/user/123" typeof="schema:Person" property="schema:name" datatype="">我与影子孤独终老i</span></span> <span>2019-12-13 04:20:06</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am following writing-a-custom-rule document to create custom rule and applying it.</p> <p>My enforcer plugin execution has the following entry in pom.xml file-</p> <pre><code>&lt;execution&gt; &lt;id&gt;enforce-no-masters&lt;/id&gt; &lt;configuration&gt; &lt;skip&gt;${masterDependenciesAllowed}&lt;/skip&gt; &lt;rules&gt; &lt;myCustomRule implementation="or.apache.customRule.CustomRule"&gt; &lt;excludes&gt; &lt;exclude&gt;*:*:master-*:*:*&lt;/exclude&gt; &lt;/excludes&gt; &lt;shouldIfail&gt;false&lt;/shouldIfail&gt; &lt;/myCustomRule&gt; &lt;/rules&gt; &lt;/configuration&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; </code></pre> <p>The issue is if I keep <code>&lt;shouldIfail&gt;true&lt;/shouldIfail&gt;</code> then I always get failure and if I keep <code>&lt;shouldIfail&gt;false&lt;/shouldIfail&gt;</code> then I always get success.</p> <p>so I don't know that how to put value of java class variable <code>shouldIfail</code> in the tag so that it will be dynamically pass or fail based on logic.</p> <p>来源:<code>https://stackoverflow.com/questions/48338179/maven-custom-rule-failing-or-passing-without-considering-custom-rule</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/pomxml" hreflang="zh-hans">pom.xml</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Thu, 12 Dec 2019 20:20:06 +0000 我与影子孤独终老i 2436910 at https://www.e-learn.cn How to resolve maven EnforcedBytecodeVersion failure? https://www.e-learn.cn/topic/2291447 <span>How to resolve maven EnforcedBytecodeVersion failure?</span> <span><span lang="" about="/user/101" typeof="schema:Person" property="schema:name" datatype="">老子叫甜甜</span></span> <span>2019-12-11 17:54:41</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>When trying to run maven enforcer getting a failure due to some classes conforming to 1.9 where as the entire project is confine dto 1.8. Following is the stack trace of the log. That specific dependency is being pulled by a different jar which can't be excluded as it has compile time dependency.</p> <pre><code>[INFO] Checking unresolved references to org.codehaus.mojo.signature:java18:1.0 [INFO] Restricted to JDK 1.8 yet javax.json.bind:javax.json.bind-api:jar:1.0:compile contains module-info.class targeted to JDK 1.9 [WARNING] Rule 14: org.apache.maven.plugins.enforcer.EnforceBytecodeVersion failed with message: Found Banned Dependency: javax.json.bind:javax.json.bind-api:jar:1.0 </code></pre> <br /><h3>回答1:</h3><br /><p>It seemed to be that you misunderstand the intention of enforceBytecodeversion...It will check all dependencies if they use byte code for a more recent version as stated which means higher than JDK 8 just lifting the maxJdkVersion is not solving the problem. The problem is related to the dependencies you are using ....</p> <pre><code>The dependency: javax.json.bin:javax.json.bind-api contains a `module-info.class` file which is related to JDK 9 ... </code></pre> <p>If you are sure all code in that dependency does not use JDK 9 specific things you have to exclude <code>module-info.class</code> from checking in enforcer rules...</p> <p><strong>Update</strong>: This can be achieved by using the following:</p> <pre><code>&lt;project&gt; [...] &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;3.0.0-M1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-bytecode-version&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;enforceBytecodeVersion&gt; &lt;maxJdkVersion&gt;1.8&lt;/maxJdkVersion&gt; &lt;ignoreClasses&gt; &lt;ignoreClass&gt;module-info&lt;/ignoreClass&gt; &lt;/ignoreClasses&gt; &lt;/enforceBytecodeVersion&gt; &lt;/rules&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt; &lt;artifactId&gt;extra-enforcer-rules&lt;/artifactId&gt; &lt;version&gt;1.0-beta-9&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; [...] &lt;/project&gt; </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>By overriding the target bytecode to 1.9 in the child pom was able to resolve the enforcer issue as follows.</p> <pre><code>&lt;plugin&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;executions&gt; &lt;execution&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;enforceBytecodeVersion&gt; &lt;maxJdkVersion&gt;1.9&lt;/maxJdkVersion&gt; &lt;/enforceBytecodeVersion&gt; &lt;/rules&gt; &lt;fail&gt;true&lt;/fail&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/51850449/how-to-resolve-maven-enforcedbytecodeversion-failure</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-3" hreflang="zh-hans">maven-3</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Wed, 11 Dec 2019 09:54:41 +0000 老子叫甜甜 2291447 at https://www.e-learn.cn Maven enforcer plugin missing or invalid rules https://www.e-learn.cn/topic/1440275 <span>Maven enforcer plugin missing or invalid rules</span> <span><span lang="" about="/user/84" typeof="schema:Person" property="schema:name" datatype="">混江龙づ霸主</span></span> <span>2019-12-03 15:16:22</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am trying to setup the enforcer plugin for maven to enforce a minimum Java version. However, whenever I try to run <code>mvn enforcer:enforce</code>, I get:</p> <blockquote> <p>The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce are missing or invalid</p> </blockquote> <p>Here is the relevant portion of my pom file:</p> <pre><code>&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.3.1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-java&lt;/id&gt; &lt;phase&gt;validate&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;requireJavaVersion&gt; &lt;version&gt;(1.7.0-20,)&lt;/version&gt; &lt;/requireJavaVersion&gt; &lt;/rules&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>I also tried replacing the <code>&lt;requireJavaVersion&gt;</code> block with <code>&lt;alwaysPass/&gt;</code>, in case something was invalid, but it still failed with the same error.</p> <br /><h3>回答1:</h3><br /><p>It may be that you are using invalid rule names. Check out the rules page. The rule names are case sensitive. Though this is not the case here.</p> <p>---- Edit ----</p> <p>Note that the POM configuration has an execution ID of <code>enforce-java</code> and that execution is bound to the <code>validate</code> phase of the lifecycle. The command <code>mvn enforcer:enforce</code> is running a goal, not a phase in the lifecycle. The configuration you provided in the POM doesn't apply to the <code>enforcer:enforce</code> goal. </p> <p>There are two ways to make this work. Which one you choose depends on what you need. </p> <ol><li>If you are just trying to test the enforcer plugin configuration without running the whole build, run <code>mvn validate</code>. </li> <li>If the requirement is that <code>mvn enforcer:enforce</code> works, then change the execution ID to <code>default-cli</code>.</li> </ol><br /><br /><p>来源:<code>https://stackoverflow.com/questions/24827194/maven-enforcer-plugin-missing-or-invalid-rules</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-3" hreflang="zh-hans">maven-3</a></div> <div class="field--item"><a href="/tag/pomxml" hreflang="zh-hans">pom.xml</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Tue, 03 Dec 2019 07:16:22 +0000 混江龙づ霸主 1440275 at https://www.e-learn.cn Maven enforcer plugin missing or invalid rules https://www.e-learn.cn/topic/1348536 <span>Maven enforcer plugin missing or invalid rules</span> <span><span lang="" about="/user/235" typeof="schema:Person" property="schema:name" datatype="">你。</span></span> <span>2019-12-03 05:50:29</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><div class="alert alert-danger" role="alert"> <p>I am trying to setup the enforcer plugin for maven to enforce a minimum Java version. However, whenever I try to run <code>mvn enforcer:enforce</code>, I get:</p> <blockquote> <p>The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce are missing or invalid</p> </blockquote> <p>Here is the relevant portion of my pom file:</p> <pre><code>&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.3.1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-java&lt;/id&gt; &lt;phase&gt;validate&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;requireJavaVersion&gt; &lt;version&gt;(1.7.0-20,)&lt;/version&gt; &lt;/requireJavaVersion&gt; &lt;/rules&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>I also tried replacing the <code>&lt;requireJavaVersion&gt;</code> block with <code>&lt;alwaysPass/&gt;</code>, in case something was invalid, but it still failed with the same error.</p> </div><div class="panel panel-info"><div class="panel-heading">user944849</div><div class="panel-body"> <p>It may be that you are using invalid rule names. Check out the <a href="http://maven.apache.org/enforcer/enforcer-rules/index.html" rel="nofollow">rules</a> page. The rule names are case sensitive. Though this is not the case here.</p> <p>---- Edit ----</p> <p>Note that the POM configuration has an execution ID of <code>enforce-java</code> and that execution is bound to the <code>validate</code> phase of the lifecycle. The command <code>mvn enforcer:enforce</code> is running a goal, not a phase in the lifecycle. The configuration you provided in the POM doesn't apply to the <code>enforcer:enforce</code> goal. </p> <p>There are two ways to make this work. Which one you choose depends on what you need. </p> <ol><li>If you are just trying to test the enforcer plugin configuration without running the whole build, run <code>mvn validate</code>. </li> <li>If the requirement is that <code>mvn enforcer:enforce</code> works, then change the execution ID to <code>default-cli</code>.</li> </ol></div></div><div class="alert alert-warning" role="alert"><p>来源:<code>https://stackoverflow.com/questions/24827194/maven-enforcer-plugin-missing-or-invalid-rules</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-3" hreflang="zh-hans">maven-3</a></div> <div class="field--item"><a href="/tag/pomxml" hreflang="zh-hans">pom.xml</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Mon, 02 Dec 2019 21:50:29 +0000 你。 1348536 at https://www.e-learn.cn usage of maven enforcer plugin https://www.e-learn.cn/topic/381821 <span>usage of maven enforcer plugin</span> <span><span lang="" about="/user/171" typeof="schema:Person" property="schema:name" datatype="">人走茶凉</span></span> <span>2019-11-27 21:13:40</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'd like to use the maven enforcer plugin to check to see if I have duplicate classes on my path. I've tried the example from here.</p> <p>But when I run it like so:</p> <p><code>mvn enforcer:enforce</code></p> <p>I get this:</p> <blockquote> <p>Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.0.1:enforce (default-cli) on project datapopulator: The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.0.1:enforce are missing or invalid</p> </blockquote> <p>Is there a way to use this correctly?</p> <p><strong>EDIT #1</strong></p> <p>If changing my config to this:</p> <pre><code> &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.0.1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-versions&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;AlwaysPass /&gt; &lt;/rules&gt; &lt;fail&gt;true&lt;/fail&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; </code></pre> <p>Produces the same error.</p> <br /><h3>回答1:</h3><br /><p>The reason why your first version did not work is because there is a difference between a plug-in configuration inside the execution tag and a plug-in configuration outside the execution tag. The execution is only used when your plug-in is triggered by a special phase of the complete Maven build. </p> <p>The Maven guide to configuration explains it better:</p> <blockquote> <p>Configurations inside the tag differ from those that are outside in that they cannot be used from a direct command line invocation. Instead they are only applied when the lifecycle phase they are bound to are invoked. Alternatively, if you move a configuration section outside of the executions section, it will apply globally to all invocations of the plugin.</p> </blockquote> <br /><br /><br /><h3>回答2:</h3><br /><p>Try this, moving the configuration outside executions, so it isn't bound to the life cycle phase.</p> <pre><code>&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.0.1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;enforce-versions&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;configuration&gt; &lt;rules&gt; &lt;AlwaysPass /&gt; &lt;/rules&gt; &lt;fail&gt;true&lt;/fail&gt; &lt;/configuration&gt; &lt;/plugin&gt; </code></pre> <p>Now when you do <code>mvn enforcer:enforce</code>, it picks the rules from your pom.xml.</p> <br /><br /><br /><h3>回答3:</h3><br /><p>See these answers</p> <p>You can use the special default command line execution id, default-cli to invoke it (see Maven Docs), see my example below. This works at least with 3.1.1 and the article cited says it should work with 2.2.0+</p> <pre><code>mvn enforcer:enforce </code></pre> <p>However if you are using <strong><em>above</em></strong> Maven 3.1.1 (I can confirm it works in 3.3.3 with enforcer v 1.4.1) you can specify the execution id you wish using the new @ syntax (see Maven JIRA and the answers above);</p> <p>e.g. for the example below use</p> <pre><code>mvn enforcer:enforce@dependency-convergence </code></pre> <p>Here's a snippet from my pom;</p> <pre><code>&lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.4.1&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; &lt;id&gt;dependency-convergence&lt;/id&gt; &lt;phase&gt;install&lt;/phase&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;DependencyConvergence /&gt; &lt;/rules&gt; &lt;fail&gt;true&lt;/fail&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;execution&gt; &lt;id&gt;default-cli&lt;/id&gt; &lt;goals&gt; &lt;goal&gt;enforce&lt;/goal&gt; &lt;/goals&gt; &lt;configuration&gt; &lt;rules&gt; &lt;DependencyConvergence/&gt; &lt;/rules&gt; &lt;fail&gt;true&lt;/fail&gt; &lt;/configuration&gt; &lt;/execution&gt; &lt;/executions&gt; &lt;/plugin&gt; ... </code></pre> <br /><br /><br /><h3>回答4:</h3><br /><p>I don't know why it won't work with the config being in an execution, but this worked for me:</p> <pre><code> &lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-enforcer-plugin&lt;/artifactId&gt; &lt;version&gt;1.0&lt;/version&gt; &lt;configuration&gt; &lt;rules&gt; &lt;banDuplicateClasses&gt; &lt;findAllDuplicates&gt;true&lt;/findAllDuplicates&gt; &lt;/banDuplicateClasses&gt; &lt;/rules&gt; &lt;fail&gt;false&lt;/fail&gt; &lt;/configuration&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt; &lt;artifactId&gt;extra-enforcer-rules&lt;/artifactId&gt; &lt;version&gt;1.0-alpha-1&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;/plugin&gt; </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/6754974/usage-of-maven-enforcer-plugin</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/java" hreflang="zh-hans">java</a></div> <div class="field--item"><a href="/tag/maven" hreflang="zh-hans">maven</a></div> <div class="field--item"><a href="/tag/maven-plugin" hreflang="zh-hans">maven-plugin</a></div> <div class="field--item"><a href="/tag/maven-enforcer-plugin" hreflang="zh-hans">maven-enforcer-plugin</a></div> </div> </div> Wed, 27 Nov 2019 13:13:40 +0000 人走茶凉 381821 at https://www.e-learn.cn