java - 如何使 Maven Assembly Plugin 中的参数成为必需

标签 java maven 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,并且至少等于一个非空格字符:

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] ------------------------------------------------------------------------

关于java - 如何使 Maven Assembly Plugin 中的参数成为必需,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47521506/

相关文章:

java - jQuery 模板和 Backbone.js

java - 如何解决maven EnforcedBytecodeVersion失败?

java - 是否有任何与 JPAv2 兼容的供应商不可知持久性 api 公开可用

maven - 类似于 intellij 的 nbactions.xml 的文件

java - 如何使用 Maven 程序集插件在 tar 中创建和包含一个 zip 文件

java - jsp中过滤记录的sql查询

java - 基于谁是被调用者的逻辑

java - 在获取 Graphics 实例之前获取 FontMetrics

maven-2 - 如何在全新安装时将多个 jar 文件发布到 maven

maven - 使用 maven-assembly-plugin 生成 OSGi 包分发