In Maven, you use the <properties> tag within your pom.xml to define variables. These properties act as placeholders, allowing you to centralize reusable values like dependency versions, plugin settings, and configuration parameters. You can reference any variable with the ${variableName} syntax.
Defining user-defined variables
The most common way to create a variable is by defining it inside the <properties> block of your pom.xml. This makes the variable's value accessible anywhere else in that same POM.
Example: Managing dependency versions
This is a best practice for managing versions across multiple dependencies to ensure they stay consistent.
Variables defined in the <properties> block of a pom.xml can be used for managing values like dependency versions and plugin configurations. Maven also provides implicit variables for accessing project, settings, environment, and system properties.
These include project properties such as ${project.version} and ${project.artifactId}, and settings properties like ${settings.localRepository}. Environment variables, accessed with the env. prefix (e.g., ${env.PATH}), and system properties (e.g., ${java.version}) are also available.
Variables can be overridden from the command line using the -D flag. For environment-specific values, profiles can be used to define different property sets.
Maven properties can also be used in non-XML resource files through resource filtering, typically configured with the maven-resources-plugin.