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/


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


Configure properties-maven-plugin in pom.xml

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

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 method

  • Create a InputStream using url.openStream method

  • Load the properties content into the java properties object using the 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 the loop

  • First, get all keys using stringPropertyNames

  • using for loop print the key and values.

import java.util.Properties;
import java.util.Set;

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 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 foldersrc/main/resources`.


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



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.


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



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.