How do I filter resource files?

Our project sometimes need to fill up or filter resource files using maven during the build time. To accomplish this in Maven, put a reference to the property that will contain the value into your resource file using the syntax ${<property name>}.

The property’s value can be defined using following ways,

  1. Using pom.xml
  2. From settings.xml
  3. From an external properties file
  4. Can be from system property/ command line

To have Maven filter resources when copying, simply set filtering to true for the resource directory in your pom.xml

<build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
</build>

We have to add the buildresources, and resource elements which weren’t there before. Also, we have to explicitly state that the resources are located in the src/main/resources directory. All of this information was provided as default values previously, but because the default value for filtering is false, we had to add this to our pom.xml in order to override that default value and set filtering to true.

How to reference a property from POM.xml?

To reference a property defined in your pom.xml, the property name uses the names of the XML elements that define the value, with “pom” being allowed as an alias for the project (root) element.

${project.name} refers to the name of the project,

${project.version} refers to the version of the project,

${project.build.finalName} refers to the final name of the file created when the built project is packaged, etc.

Note that some elements of the POM have default values, so don’t need to be explicitly defined in your pom.xml for the values to be available here.

How to reference a property from settings.xml?

To reference a property defined in your settings.xml, the property name uses the names of the XML elements that define the value. Values in the user’s settings.xml can be referenced using property names beginning with “settings”

${settings.localRepository} refers to the path of the user’s local repository.

To continue our example, let’s add a couple of properties to the application.properties file (which we put in the src/main/resources directory) whose values will be supplied when the resource is filtered:

# application.properties
application.name=${project.name}
application.version=${project.version}

With that in place, you can execute the following command (process-resources is the build lifecycle phase where the resources are copied and filtered):

mvn process-resources

and the application.properties file under target/classes (and will eventually go into the jar) looks like this:

# application.properties
application.name=Maven Quick Start Archetype
application.version=1.0-SNAPSHOT

How to reference a property from external file?

To reference a property defined in an external file, all you need to do is add a reference to this external file in your pom.xml. First, let’s create our external properties file and call it src/main/filters/filter.properties.

# filter.properties
my.filter.value=hello!

Next, we’ll add a reference to this new file in the pom.xml

<build>
    <filters>
      <filter>src/main/filters/filter.properties</filter>
    </filters>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
</build>

Then, if we add a reference to this property in the application.properties file:

# application.properties
application.name=${project.name}
application.version=${project.version}
message=${my.filter.value}

the next execution of the mvn process-resources command will put our new property value into application.properties.

How to reference a property from property section of POM.xml?

As an alternative to defining the my.filter.value property in an external file, you could also have defined it in the properties section of your pom.xml and you’d get the same effect.

<properties>
    <my.filter.value>hello</my.filter.value>
</properties>

How to reference a property from System property/ Command line?

Filtering resources can also get values from system properties; either the system properties built into Java (like java.version or user.home) or properties defined on the command line using the standard Java -D parameter. To continue the example, let’s change our application.properties file to look like this:

# application.properties
java.version=${java.version}
command.line.prop=${command.line.prop}

Now, when you execute the following command (note the definition of the command.line.prop property on the command line), the application.properties file will contain the values from the system properties.

mvn process-resources "-Dcommand.line.prop=hello again"

Conclusion

Hence we saw how to reference property from different sources such as pom.xml, settings.xml, external properties file, from system property/ command line.

Please follow and like us:
error