简体   繁体   English

Maven Enforcer插件:通过命令行指定规则

[英]Maven Enforcer Plugin: Specify rules via command line

I want to execute the Maven Enforcer plugin via the command line. 我想通过命令行执行Maven Enforcer插件

I've tried: 我试过了:

mvn enforcer:enforce -Drules=[requireReleaseDeps]
mvn enforcer:enforce -Drules=requireReleaseDeps

I am always getting this error: 我总是收到这个错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (default-cli) on project lkww-util-app-wurm-admin-rs-api: The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce are missing or invalid -> [He
lp 1]

How do I have to specify the rules parameter? 如何指定rules参数?

The enforcer plugin does not allow rules to be chosen/engaged via command line parameters. enforcer插件不允许通过命令行参数选择/使用规则。

There is an open issue against the plugin for this so you could vote for that. 针对此插件存在一个未解决的问题 ,因此您可以对此进行投票。

In the meantime, if your choice of rules can be categorised into a small number of choices then you could perhaps create profiles and associate rules with profiles thereby allowing a build to be run for a selected subset of rules by specifying a profile. 同时,如果您可以将选择的规则分类为少量选项,那么您可以创建配置文件并将规则与配置文件相关联,从而允许通过指定配置文件为选定的规则子集运行构建。 In the example below there are two profiles, each of which has a different enforcer rule: 在下面的示例中,有两个配置文件,每个配置文件都有不同的强制规则:

<profiles>
    <profile>
        <id>EnforceBannedPlugins</id>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>enforce-banned-plugins</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <bannedPlugins>
                                    ...
                                </bannedPlugins>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </profile>
    <profile>
        <id>EnforceMavenVersion</id>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>enforce-maven-version</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    ...
                                </requireMavenVersion>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </profile>
</profiles>

Of course, this is only a runner if your requirement to specify enforcer rules at runtime can be satisfied by a few canned configurations. 当然,如果您在运行时指定实施者规则的要求可以通过一些预制配置来满足,那么这只是一个运行者。 If, however, the requirement is to support any possible enforcer rule then you're out of luck because the plugin does not support that. 但是,如果要求是支持任何可能的强制执行规则,那么你运气不好,因为插件不支持。

Rather than using profiles, as recommended in another answer, you can also pre-configure your <executions> in the main section of your POM and then use the <execution> 's <id> to invoke them from the command line (see the Guide to Configuring Plug-ins for more info on this syntax): 您可以在POM的主要部分中预先配置<executions> ,然后使用<execution><id>从命令行调用它们,而不是使用另一个答案中建议的配置文件(请参阅有关此语法的详细信息,请参阅配置插件指南 ):

mvn enforcer:enforcer@my-execution-id

As any <execution> of the enforce goal by defaults binds the goal to the validate phase, however, the my-execution-id execution also runs on a normal mvn clean install . 由于默认情况下enforce目标的任何<execution>目标绑定到validate阶段,因此my-execution-id执行也会在正常的mvn clean install上运行。 If that is not desired, configure the execution with <skip>true</true> and override this on the command-line: 如果不需要,请使用<skip>true</true>配置执行,并在命令行上覆盖它:

mvn enforcer:enforcer@my-execution-id -Denforcer.skip=false

Whether this is clearer than spreading the maven-enforcer-plugin configuration across the POM's main section and <profiles> is a matter of personal preference. 这是否比在POM的主要部分和<profiles>传播maven-enforcer-plugin配置更清楚是一个个人偏好的问题。

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

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