Is there a way in maven to assure that a property is set

冷暖自知 提交于 2021-01-27 04:48:25

问题


I just tracked down a difficult maven issue that was caused by a bad property value.

The property is a path to an alternate JVM that is used a run-time by a test. I would like to make maven fail early by detecting if the path is valid or not. What might be a way to accomplish this?

I plan to dig into antrun to see if there is a way to make it run first so that it can check, but that seems like overkill.

Question: How can I do this cleanly and simply?


回答1:


Yes, you can use the maven-enforcer-plugin for this task. This plugin is used to enforce rules during the build and it has a built-in requireFilesExist rule:

This rule checks that the specified list of files exist.

The following configuration will enforce that the file ${project.build.outputDirectory}/foo.txt exists and will fail the build if it does not.

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



回答2:


You can use the Enforcer Maven Plugin and its Require Property rule, where you can enforce the existence of a certain property, optionally with a certain value (a matching regex), and fail the build otherwise.

This rule can enforce that a declared property is set and optionally evaluate it against a regular expression.

A simple snippet would be:

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



回答3:


Use the Require Files Exist rule of the Maven Enforcer plugin.

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


来源:https://stackoverflow.com/questions/36220448/is-there-a-way-in-maven-to-assure-that-a-property-is-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!