In this tutorial, You will Learn How to read external properties file in maven java application Sometimes, We want to read values from a properties file during maven build.

How to read properties in Python with example?

maven properties plugin is used to read the properties from an external file.

Let’s create properties file in src/main/resources/


In pom.xml, create a property under the properties tag for the property file location.


Configure properties-maven-plugin in pom.xm

              <echo>Print Database properties</echo>
                <echo>Database: "${database}"</echo>
                <echo>hostname: "${hostname}"</echo>
                <echo>username: "${username}"</echo>
             <echo>password: "${password}"</echo>

On running mvn validate command

It prints the following things in the command line

[echo] Print Database properties
[echo] Database: postgress
[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 method
  • Create a InputStream using url.openStream method
  • Load the properties content into java properties object using load method
  • Add try and catch block for IOExceptions and FIleNotFoundException
  • Oneway, Print the value using getProperty with a key of a properties object
  • Another way is to iterate properties object for loop
  • First, get all keys using stringPropertyNames
  • using for loop print the key and values.

public class PropertiesReader { public static void main(String[] args) { Properties properties = new Properties(); url = ClassLoader.getSystemResource(“”);

    try  {
    } catch (FileNotFoundException fie) {
    catch (IOException e) {
    Set<String> keys = properties.stringPropertyNames();
    for (String key : keys) {
        System.out.println(key + " - " + properties.getProperty(key));


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 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.


Define custom properties and initialize the variables with maven system variables. So, Define the properties inside properties tag in pom.xml



In pom.xml, add resources inside the building element and set filtering=true. filtering enables to replace variables from a project, system to the resource files.


This will write the version and date into in src/main/resources during maven build running.



You learned to read a properties file during maven build

and also write version number and build timestamp into to properties file using maven code.