【问题标题】:Is there a way in maven to assure that a property is set有没有办法在 Maven 中确保设置属性
【发布时间】:2016-07-13 05:44:53
【问题描述】:

我刚刚找到了一个由错误的属性值引起的困难的 maven 问题。

该属性是测试运行时使用的备用 JVM 的路径。 我想通过检测路径是否有效来使 maven 尽早失败。 有什么方法可以做到这一点?

我打算深入研究 antrun 看看是否有办法让它先运行以便它可以检查,但这似乎有点过头了。

问题:我怎样才能干净而简单地做到这一点?

【问题讨论】:

  • 明确一点:你要检查属性是否设置或文件是否存在?似乎标题和您的问题要求不同的东西(因此有两个不同的答案:D)

标签: maven maven-3 pom.xml


【解决方案1】:

您可以使用Enforcer Maven Plugin 及其Require Property 规则,您可以在其中强制存在某个属性,可选择具有某个值(匹配的正则表达式),否则构建失败。

此规则可以强制设置已声明的属性,并可选择根据正则表达式对其进行评估。

一个简单的 sn-p 将是:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
        <execution>
            <id>enforce-property</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <requireProperty>
                        <property>basedir</property>
                        <message>You must set a basedir property!</message>
                        <regex>.*\d.*</regex>
                        <regexMessage>The basedir property must contain at least one digit.</regexMessage>
                    </requireProperty>
                </rules>
                <fail>true</fail>
            </configuration>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 啊有趣,我们建议相同的插件我们不一样的规则:D。
  • @Tunaki ahaha 很好,好的,那么由 OP 来检查哪一个最适合他/她的需求。
  • 很高兴这可以将事物与正则表达式进行比较,但另一个更简单一些,如果我们曾经对代码进行过工作,可能会使实习生的困惑更少。不过还是谢谢。
【解决方案2】:

是的,您可以使用maven-enforcer-plugin 执行此任务。此插件用于在构建期间强制执行规则,它具有内置的requireFilesExist 规则:

此规则检查指定的文件列表是否存在。

以下配置将强制文件${project.build.outputDirectory}/foo.txt 存在,如果不存在则构建失败。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>1.4.1</version>
  <executions>
    <execution>
      <id>enforce-files-exist</id>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireFilesExist>
            <files>
             <file>${project.build.outputDirectory}/foo.txt</file>
            </files>
          </requireFilesExist>
        </rules>
        <fail>true</fail>
      </configuration>
    </execution>
  </executions>
</plugin>

【讨论】:

    【解决方案3】:

    使用 Maven Enforcer 插件的Require Files Exist 规则。

      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.4.1</version>
            <executions>
              <execution>
                <id>enforce-files-exist</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireFilesExist>
                      <files>
                       <file>${property.to.check}</file>
                      </files>
                    </requireFilesExist>
                  </rules>
                  <fail>true</fail>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2020-03-22
      • 2021-02-18
      • 1970-01-01
      • 2013-10-29
      相关资源
      最近更新 更多