Solx
Solx

Reputation: 5121

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

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?

Upvotes: 7

Views: 3188

Answers (3)

Harald Wellmann
Harald Wellmann

Reputation: 12885

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>

Upvotes: 1

Tunaki
Tunaki

Reputation: 137329

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>

Upvotes: 4

A_Di-Matteo
A_Di-Matteo

Reputation: 27862

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>

Upvotes: 6

Related Questions