- Properties File — Maven
- How to read properties in Python with examples?
- How to write properties in maven build
- Java maven properties file
- Filtering Properties Files
- What has project.build.sourceEncoding got to do with resources?
- Properties files handled by the Properties class
- Properties files used as ResourceBundle
- What do I need to do?
Properties File — Maven
This tutorial explains step by step to read and write a properties file in maven java with example You learned to read and also write version numbers and build timestamps into to properties file using maven code..
In this tutorial, You will learn, How to read external properties files in maven java application Sometimes, We want to read values from a properties file during 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
In pom.xml, create a property under the properties tag for the property file location.
src/main/resources/prod.properties
Configure properties-maven-plugin in pom.xm
org.codehaus.mojo properties-maven-plugin 1.0.0 initialize read-project-properties prod.properties org.apache.maven.plugins maven-antrun-plugin 3.0 validate run Print Database properties Database: "$" hostname: "$" username: "$" password: "$"
On running the mvn validate command.
It prints the following things in the command line.
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.
catch (FileNotFoundException fie) < fie.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >System.out.println(properties.getProperty("hostname")); Set keys = properties.stringPropertyNames(); for (String key : keys) < System.out.println(key + " - " + properties.getProperty(key)); >> >
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`.
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.
src/main/resources true **/version.properties
This will write the version and date into the version.properties in src/main/resources during the 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.
Java maven properties file
- Apache /
- Maven /
- Plugins /
- Apache Maven Resources Plugin /
- Filtering Properties Files
- | Last Published: 2023-03-21
- Version: 3.3.1
Filtering Properties Files
When filtering resources, special care has to be taken if you are filtering properties files. If your filtered properties files include non-ascii characters and your project.build.sourceEncoding is set to anything other than ISO-8859-1 you might be affected and should continue reading.
What has project.build.sourceEncoding got to do with resources?
Maven Resources Plugin has, up until version 3.2.0, defaulted to use project.build.sourceEncoding as the encoding when filtering resources, unless you configure the encoding parameter of the plugin explicitly. So unless you have configured the encoding parameter in Maven Resources Plugin explicitly this is what you get.
Properties files handled by the Properties class
When the Properties class is used to read and write properties files they require that the properties files use ISO-8859-1 encoding. This is still the case for Java 11, as can be seen in the API documentation for the Properties class. So, properties files that are used in this way needs to use ISO-8859-1 encoding.
Properties files used as ResourceBundle
When properties files are used as ResourceBundle s the encoding required differs between versions of Java. Up to and including Java 8 these files are required to use ISO-8859-1 encoding.
Starting with Java 9 the preferred encoding is UTF-8 for property resource bundles. It might work with ISO-8859-1, but as you can see in the Internationalization Enhancements in JDK 9 documentation you should consider converting your property resource bundles into UTF-8 encoding.
What do I need to do?
- Decide which encoding to use for properties files, based on how you use them in your project.
- Explicitly configure Maven Resource Plugin accordingly using the propertiesEncoding configuration parameter, that was introduced in version 3.2.0. In most cases it would look like this:
. . . org.apache.maven.plugins maven-resources-plugin 3.3.1 . ISO-8859-1 .