我可以强制Maven 2要求在命令行上指定属性吗?

Hob*_*obo 19 maven-2

我正在设置maven构建,并且需要在命令行上将目标服务器指定为属性(然后用于选择适当的配置文件),例如

mvn -Denv=test
Run Code Online (Sandbox Code Playgroud)

如果财产未设置,我希望构建失败 - 这可能吗?

是的,我是Maven的新手.

编辑:我已经看到这个链接,这似乎暗示它是不可能的,但我不知道它是如何最新的.

Bri*_*Fox 25

每个人都很接近,但是在执法者中有一条规则来专门检查属性,不需要蚂蚁或时髦的配置文件:http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html

该规则还可以检查属性的值.


小智 5

也许您可以使用这样的解决方法:在Maven中,如果未设置某些属性,您可以激活配置文件:

<project>
...
    <profiles>
        <profile>
            <id>failure_profile</id>
            <activation>
                <property>
                    <name>!env</name>
                </property>
            </activation>
        </profile>
    </profiles>
</project>
Run Code Online (Sandbox Code Playgroud)

然后你应该强制这个配置文件总是失败,例如使用maven-enforcer-plugin:

<profile>
    <id>failure_profile</id>
    ...
    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <executions>
              <execution>
                <id>enforce</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <AlwaysFail/>
                  </rules>
                  <fail>true</fail>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
</profile>
Run Code Online (Sandbox Code Playgroud)

如果您不提供-Denv构建将失败:

[INFO] [enforcer:enforce {execution: enforce}]
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.AlwaysFail failed with message:
Always fails!
[INFO] ---------------------------------------------------------
[ERROR] BUILD ERROR
Run Code Online (Sandbox Code Playgroud)

好吧,它比Ant更冗长,但纯粹的Maven :)