简体   繁体   English

如何使用 Maven Enforcer 插件?

[英]How to use of the Maven Enforcer plugin?

I'd like to use the Maven Enforcer plugin to check to see if I have duplicate classes on my path.我想使用 Maven Enforcer 插件来检查我的路径上是否有重复的类。

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 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 丢失或无效

Is there a way to use this correctly?有没有办法正确使用它?

EDIT #1编辑#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.产生相同的错误。

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.仅当您的插件由完整 Maven 构建的特殊阶段触发时才使用执行。

The Maven guide to configuration explains it better: Maven 配置指南更好地解释了它:

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.或者,如果您将配置部分移到执行部分之外,它将全局应用于插件的所有调用。

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.现在,当您执行mvn enforcer:enforce时,它会从您的 pom.xml 中选择规则。

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.您可以使用特殊的默认命令行执行 ID,default-cli 来调用它(请参阅Maven 文档),请参阅下面的示例。 This works at least with 3.1.1 and the article cited says it should work with 2.2.0+这至少适用于 3.1.1,引用的文章说它应该适用于 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);但是,如果您使用的是 Maven 3.1.1以上版本(我可以确认它在 3.3.3 中使用 enforcer v 1.4.1),您可以使用新的 @ 语法指定您希望的执行 ID(请参阅Maven JIRA和上面的答案);

eg for the example below use例如,对于下面的示例,请使用

mvn enforcer:enforce@dependency-convergence

Here's a snippet from my pom;这是我的 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>
      ...

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM