You are previewing Maven: The Definitive Guide.

Maven: The Definitive Guide

Cover of Maven: The Definitive Guide by Sonatype Company Published by O'Reilly Media, Inc.
O'Reilly logo

Resource Filtering

You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for references to Maven property references surrounded by ${ and }. When it finds these references, it will replace them with the appropriate value in much the same way that the properties defined in the previous section can be referenced from a POM. This feature is especially helpful when you need to parameterize a build with different configuration values depending on the target deployment platform.

Often a .properties file or an XML document in src/main/resources will contain a reference to an external resource, such as a database or a network location that needs to be configured differently depending on the target deployment environment. For example, a system that reads data from a database has an XML document that contains the JDBC URL along with credentials for the database. If you need to use a different database in development and a different database in production, you can use a technology such as Java Naming and Directory Interface (JNDI) to externalize the configuration from the application in an application server, or you can create a build that knows how to replace variables with different values depending on the target platform.

Using Maven resource filtering, you can reference Maven properties and then use Maven profiles to define different configuration values for different target deployment environments. To illustrate ...

The best content for your career. Discover unlimited learning on demand for around $1/day.