Is there a way in maven to ensure that the property is set

I just discovered a complex problem with maven that was caused by a bad property value.

A property is the path to an alternative JVM that is used by the test at runtime. I would like to make maven earlier, finding that the path is valid or not. What could be for this?

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

Question : How can I do this cleanly and simply?

+5
source share
3 answers

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

This rule checks for the presence of a specified list of files.

The following configuration will ensure that the file ${project.build.outputDirectory}/foo.txt exists and will fail if it is 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> 
+4
source

You can use the Enforcer Maven Plugin and Require a property , where you can ensure the existence of a specific property, optionally with a specific value (the corresponding regular expression), and not build otherwise.

This rule can forcibly establish that a declared property is set, and possibly evaluates it by 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> 
+4
source

Use the Require Files Exist rule for 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> 
+1
source

Source: https://habr.com/ru/post/1245769/


All Articles