In this tutorial, You will learn, How to read external properties files in the Maven Java application Sometimes, We want to read values from a properties file during the maven build.
How to read properties in Python with examples?
maven properties plugin
is used to read the properties from an external file.
Let’s create a properties file in src/main/resources/prod.properties
database=postgress
hostname=localhost
username=john
password=
In pom.xml, create a property under the properties tag for the property file location.
<properties>
<db.properties>src/main/resources/prod.properties</db.properties>
</properties>
Configure properties-maven-plugin in pom.xml
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<files>
<file>prod.properties</file>
</files>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>Print Database properties</echo>
<echo>Database: "${database}"</echo>
<echo>hostname: "${hostname}"</echo>
<echo>username: "${username}"</echo>
<echo>password: "${password}"</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugin>
On running the mvn validate command.
It prints the following things in the command line.
[echo] Print Database properties
[echo] Database: Postgres
[echo] hostname: localhost
[echo] username: john
[echo] password:
Here is an example and step by sep
Create a properties object, Properties is a data structure to store keys and values in Java
Create URL object using
ClassLoader
.getSystemResource
methodCreate a
InputStream
usingurl.openStream
methodLoad the properties content into the java properties object using the
load
methodAdd
try
andcatch
block forIOExceptions
andFIleNotFoundException
Oneway, Print the value using getProperty with a key of a properties object
Another way is to iterate properties object for the loop
First, get all keys using
stringPropertyNames
using for loop print the key and values.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesReader {
public static void main(String[] args) {
Properties properties = new Properties();
java.net.URL url = ClassLoader.getSystemResource("database.properties");
try {
properties.load(url.openStream());
} catch (FileNotFoundException fie) {
fie.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(properties.getProperty("hostname"));
Set<String> keys = properties.stringPropertyNames();
for (String key : keys) {
System.out.println(key + " - " + properties.getProperty(key));
}
}
}
Output:
localhost
hostname - localhost
password -
database - mysql
username - john
How to write properties in Maven build
Sometimes, We want to write a build number and timestamp to the properties file during maven build time, and Java reads and displays in UI to know the version.
For example, Let’s create a version. properties file in the maven project folder
src/main/resources`.
build.version=${app.version}
build.date=${app.date}
Define custom properties and initialize the variables with maven system variables. So, Define the properties inside the properties tag in pom.xml
<properties>
<app.date>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy-MM-dd:HH:mm:ss</maven.build.timestamp.format>
<app.version>${pom.version}</timestamp>
</properties>
In pom.xml, add resources inside the building element and set filtering=true. filtering enables replacement variables from a project or system to the resource files.
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/version.properties</include>
</includes>
</resource>
</resources>
</build>
This will write the version and date into the version.properties in src/main/resources
during the maven build running.
build.version=1.2.1
build.date=2021-12-12:10:11:09
Conclusion
You learned to read a properties file during maven build.
and also write the version number and build timestamp into to properties file using maven code.