Interpolation and Variables

This lesson explains how variable substitution works in Maven and the variables that come default with Maven.

Maven allows using predefined and user-defined variables in a POM file. This avoids repeating values across child POMs and also makes modifications easy as any change only needs to be applied at one place.

Any elements with a single value present in the project model (the XSD that defines the structure of a POM file) can be accessed using the prefix project.. For instance, we can access the version of the project using project.version. Similarly, if we want to access the default value of the source directory defined in the super POM, we can do so using the expression project.build.sourceDirectory. Some of the variables are special, i.e., not defined in the POM or super POM, such as project.basedir and maven.build.timestamp.

Users can define their own variables as properties under the properties XML element and can then use the variable as ${X} where X is the name of the property. Properties defined in a parent POM can also be used in a child POM. With this backdrop let’s dive into the exercise below which will give us hands-on exposure to using properties in POM.

Get hands-on with 1200+ tech skills courses.