Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven warn or fail goal when resource filtering cannot find a property

Tags:

maven

I am starting to work on a poorly documented Maven project. I have set up my profile for the project but I may or may not be missing property definitions that used during resource filtering. By default, if a property is not defined in a filtered file, the variable name is left in the copied resource and Maven continues silently.

Is there a way to force Maven to fast-fail in this case?

As a minimal example, take this pom,

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
 <build>
  <resources>
   <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
   </resource>
  </resources>
 </build>
 <profiles>
  <profile>
   <id>production</id>
   <properties>
    <foo>Craig</foo>
   </properties>
  </profile>
 </profiles>
</project>

And have a resource to filter:

 echo 'Hello, my name is ${foo}!' > src/main/resources/test

Then running mvn clean install -Pproduction produces a file that says Hello, my name is Craig! while running mvn clean install produces a file that says Hello, my name is ${foo}!.

So my question is; how do I force Maven to fail in the second case?

like image 656
Craig Pemberton Avatar asked Jul 18 '13 19:07

Craig Pemberton


People also ask

What is resource filtering in Maven?

You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.

Which plugin is used to copy filter include and exclude non Java files into your final project?

Apache Maven Resources Plugin – Including and excluding files and directories.

What is Maven resources plugin?

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources.

Which option rightly gives the project's source directory?

Source Directories The Maven property ${project. basedir} defaults to the top level directory of the project, so the build directory defaults to the target directory in project base dir. It also sets the property ${project.


2 Answers

JIRA issues exist for this feature in The Codehause database:

  • MRESOURCES-163 Filtering: check if there are any placeholders that were not substituted
  • MRESOURCES-162 Fail build if resource not fully filtered

You can log in and vote for them. Unfortunately the demand for this important safety net feature seems to be low ... strange.

like image 61
gerrie Avatar answered Sep 23 '22 15:09

gerrie


Of course, the Apache Maven Enforcer Plugin is your friend, especially the RequireProperty Rule

like image 28
Robert Scholte Avatar answered Sep 21 '22 15:09

Robert Scholte