如何在 Maven Assembly 插件中使参数成为必需的

How make parameters necessary in Maven Assembly Plugin

我正在使用 Maven 程序集插件。我从 mvn 命令到程序集描述中得到一个名为 env 的参数。基于它,程序集决定使用哪些配置进行分发。有什么方法可以使这个参数成为强制性的,这样如果用户不传递这个参数那么构建就会失败?

使用 Enforcer plugin's required property rule 怎么样?

您可以添加:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0-M1</version>
        <executions>
            <execution>
                <id>enforce-property</id>
                <goals>
                    <goal>enforce</goal>
                </goals>
                <configuration>
                    <rules>
                        <requireProperty>
                            <property>env</property>
                            <message>You must set an env property!</message>
                            <regex>^(?!\s*$).+</regex>
                            <regexMessage>Env property validation failed</regexMessage>
                        </requireProperty>
                    </rules>
                    <fail>true</fail>
                </configuration>
            </execution>
        </executions>
    </plugin>

这需要设置 -Denv,并且等于至少一个非 space 字符:

mvn clean verify
...
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireProperty failed with message:
You must set an env property!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.557 s
[INFO] Finished at: 2017-11-27T19:30:06-05:00
[INFO] Final Memory: 10M/309M
[INFO] ------------------------------------------------------------------------


mvn clean verify -Denv=test
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.965 s
[INFO] Finished at: 2017-11-27T19:30:59-05:00
[INFO] Final Memory: 27M/309M
[INFO] ------------------------------------------------------------------------