Sometimes
you may need to use properties to contain a value that can only be
supplied at build time. Maven provides a valid mechanism to handle this
type of property. Indeed it is possible to put a reference to the
property that will contain the value into your resource file using the
syntax ${<property name>}. The property can be one of the values defined in your pom.xml, a value defined in the user's settings.xml (in $HOME/.m2), a property defined in an external properties file, or a system property.
Suppose we have this pom.xml in our project:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.blogspot.informaticaamodomio</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
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. Some properties are:
- ${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;
- ${project.groupId} refers to group id of the project;
- ${parent.version} refers to the version of the parent pom.
At this point we can proceed with an example: let's add some properties to the application.properties file (in the src/main/resources directory, according pom configuration) whose values will be supplied when the resource is filtered:
you can execute the following command:application.name=${project.name}-${project.version}
application.package.name=${project.build.finalName}
$ mvn process-resources
application.name=myapp-1.0
application.package.name=my-app
<build>
<filters>
<filter>src/main/filters/filter.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
devMode=true
application.name=${project.name}-${project.version} application.package.name=${project.build.finalName}
application.devMode=${devMode}
As an alternative to the external file (filter.properties) you can create the properties into pom.xml into <properties> xml tag.
<properties>
<devMode>true</devMode>
</properties>
Finally you can also get values from system properties for example, java version, java home, user home. And if all this is not enough, you can define the properties via 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.name=${project.name}-${project.version}
application.package.name=${project.build.finalName}
java.version=${java.version}
server.ip=${server.ip}
$ mvn process-resources "-Dserver.ip=localhost"
No comments:
Post a Comment