如何使用 Maven Enforcer 插件?

发布于 2024-11-25 07:02:23 字数 1395 浏览 2 评论 0原文

我想使用 Maven Enforcer 插件来检查我的路径上是否有重复的类。

我已经尝试了此处中的示例。

但是当我像这样运行它时:

mvn enforcer:enforce

我收到此错误:

执行目标失败 org.apache.maven.plugins:maven-enforcer-plugin:1.0.1:enforce 项目 datapopulator 上的(default-cli):参数“规则” 目标 org.apache.maven.plugins:maven-enforcer-plugin:1.0.1:enforce 是 缺失或无效

有没有办法正确使用它?

编辑 #1

如果将我的配置更改为:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0.1</version>
            <executions>
                <execution>
                    <id>enforce-versions</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <AlwaysPass />
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>

会产生相同的错误。

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.

But when I run it like this:

mvn enforcer:enforce

I get this error:

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

Is there a way to use this correctly?

EDIT #1

If changing my config to this:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0.1</version>
            <executions>
                <execution>
                    <id>enforce-versions</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <AlwaysPass />
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Produces the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故乡的云 2024-12-02 07:02:23

您的第一个版本不起作用的原因是执行标记内的插件配置和执行标记外的插件配置之间存在差异。仅当您的插件由完整 Maven 构建的特殊阶段触发时才会使用执行。

Maven 配置指南 对此进行了更好的解释:

标记内部的配置与外部的配置不同,因为它们不能从直接命令行调用中使用。相反,它们仅在调用它们所绑定的生命周期阶段时才应用。或者,如果您将配置部分移到执行部分之外,它将全局应用于插件的所有调用。

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.

The Maven guide to configuration explains it better:

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.

葮薆情 2024-12-02 07:02:23

试试这个,将配置移到执行之外,这样它就不受生命周期阶段的约束。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <id>enforce-versions</id>
            <goals>
                <goal>enforce</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <rules>
            <AlwaysPass />
        </rules>
        <fail>true</fail>
    </configuration>
</plugin>

现在,当您执行 mvn enforcer:enforce 时,它会从您的 pom.xml 中选择规则。

Try this, moving the configuration outside executions, so it isn't bound to the life cycle phase.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.0.1</version>
    <executions>
        <execution>
            <id>enforce-versions</id>
            <goals>
                <goal>enforce</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <rules>
            <AlwaysPass />
        </rules>
        <fail>true</fail>
    </configuration>
</plugin>

Now when you do mvn enforcer:enforce, it picks the rules from your pom.xml.

意中人 2024-12-02 07:02:23

请参阅这些答案

您可以使用特殊的默认命令行执行 id,default-cli 调用它(请参阅 Maven文档),请参阅下面的示例。这至少适用于 3.1.1,引用的文章说它应该适用于 2.2.0+

mvn enforcer:enforce

但是,如果您使用以上 Maven 3.1.1(我可以确认)在 3.3.3 中与执行器 v 1.4.1 配合使用)您可以使用新的 @ 语法指定您希望的执行 id(请参阅 Maven JIRA 以及上面的答案);

例如,对于下面的示例,使用

mvn enforcer:enforce@dependency-convergence

Here's a snippet from my pom;

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.4.1</version>
            <executions>
                <execution>
                    <id>dependency-convergence</id>
                    <phase>install</phase>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <DependencyConvergence />
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
                <execution>
                    <id>default-cli</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <DependencyConvergence/>
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      ...

See these answers

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+

mvn enforcer:enforce

However if you are using above 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);

e.g. for the example below use

mvn enforcer:enforce@dependency-convergence

Here's a snippet from my pom;

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.4.1</version>
            <executions>
                <execution>
                    <id>dependency-convergence</id>
                    <phase>install</phase>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <DependencyConvergence />
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
                <execution>
                    <id>default-cli</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <DependencyConvergence/>
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      ...
时光无声 2024-12-02 07:02:23

我不知道为什么它不适用于执行中的配置,但这对我有用:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <rules>
                    <banDuplicateClasses>
                        <findAllDuplicates>true</findAllDuplicates>
                    </banDuplicateClasses>
                </rules>
                <fail>false</fail>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>extra-enforcer-rules</artifactId>
                    <version>1.0-alpha-1</version>
                </dependency>
            </dependencies>
        </plugin>

I don't know why it won't work with the config being in an execution, but this worked for me:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <rules>
                    <banDuplicateClasses>
                        <findAllDuplicates>true</findAllDuplicates>
                    </banDuplicateClasses>
                </rules>
                <fail>false</fail>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>extra-enforcer-rules</artifactId>
                    <version>1.0-alpha-1</version>
                </dependency>
            </dependencies>
        </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文