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

<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 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(); 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:
```markup
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 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 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 to replace variables from a project, 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 version.properties in src/main/resources during 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 version number and build timestamp into to properties file using maven code.