Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 503
Solx Avatar asked Mar 25 '16 13:03

Solx


People also ask

What is properties tag in Maven?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

What is a groupId and artifactId in Maven?

groupId – a unique base name of the company or group that created the project. artifactId – a unique name of the project. version – a version of the project.

What is the use of properties in Maven?

Properties can be defined in a POM or in a Profile. The properties set in a POM or in a Maven Profile can be referenced just like any other property available throughout Maven. User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.


2 Answers

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>
like image 191
A_Di-Matteo Avatar answered Oct 11 '22 16:10

A_Di-Matteo


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>
like image 22
Tunaki Avatar answered Oct 11 '22 18:10

Tunaki