Maven allows us to define as well as use properties. Properties allow us to avoid hardcoding values in multiple places such as versions of dependencies. They also provide flexibility to the build tool by allowing values to be passed at runtime.
Let's define and use Maven properties by performing the following steps:
<properties> <junit.version>3.8.1</junit.version> </properties>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency>
There are different types of properties. They are as follows:
env.
will return the value of the shell's environment variable. For example, ${env.PATH}
will return the value of the PATH
variable.project.
will return the value of that element in the pom file. For example, ${project.version}
will return the value in the <version>
tag of the pom file.settings
variable: Prefixing a variable with settings.
will return the value of that element in the settings file. For example, ${settings.offline}
will return the value <offline>
in the settings file.System.getProperties()
method in Java is available. For example, ${java.home}
.<properties>
tag, which is shown in the following example:<properties> <java.version>1.7</java.version> </properties>
Here, the ${java.version}
command will return 1.7
Do remember that properties and profiles can break the portability of the project. Two specific practices for looking up in problem areas are as follows:
3.145.17.140