Properties Constants Creator Maven Plugin
This plug-in reads properties files and generates Java classes holding
constants for the property keys or values. It scans a given directory (default
src/main/resources
) for properties files and creates constants classes for
all of them.
By default a generated class containing constants for the keys to access the properties. By configuration the class could be generated to contain constants holding the values from the properties file. In this case the properties file itself is not needed during runtime.
In Eclipse the plugin goal generate
is used as a build participant and runs on
incremental and clean builds.
Example
The file messages.properties
contains two entries:
welcome=Welcome to Maven
goodby=See you next time
With the default template keys
the following Java class will be generated.
/**
* Constants for messages.properties
* <p>
* The constant values are the keys to access the properties.
*
* @author properties-constants-maven-plugin
*/
public final class Messages {
/**
* Properties file used to generate this class: "messages.properties".
*/
public static final String PROPERTIES_FILE_NAME = "messages.properties";
/**
* Key of welcome=Welcome to Maven
*/
public static final String WELCOME = "welcome";
/**
* Key of goodby=See you next time
*/
public static final String GOODBY = "goodby";
/** Hidden constructor. */
private Messages() {
// nothing to instantiate
}
}
With the template values
the following Java class will be generated.
/**
* Constants for messages.properties
* <p>
* The constant values are the values of the properties.
*
* @author properties-constants-maven-plugin
*/
public final class Messages {
/**
* Value of welcome=Welcome to Maven
*/
public static final String WELCOME = "Welcome to Maven";
/**
* Value of goodby=See you next time
*/
public static final String GOODBY = "See you next time";
/** Hidden constructor. */
private Messages() {
// nothing to instantiate
}
}
Properties Source Files
The plugin supports traditional *.properties
files and XML
properties. If the file extension contains xml
(case insensitive) it
is read as XML file, all other extensions are handled like a *.properties
.
Property Key to Java Constant Name
The plugin creates names for the Java constants from the names of the keys in the properties file.
- Trim leading and trailing whitespaces.
- Trim leading and trailing non Java indentifier char
- Split into parts on non Java indentifier chars and underscores
- Concatenate all parts with ‘_’
- Make entire String upper case.
Examples
Property Key | Java Constant |
---|---|
testCase |
TEST_CASE |
test.case |
TEST_CASE |
test..case |
TEST_CASE |
test_case |
TEST_CASE |
test%case |
TEST_CASE |
test$case |
TEST$CASE |
0test |
_0TEST |
%test |
TEST |
. |
_ |
.. |
__ |
%%% |
___ |
Also this plugin should be able to handle any properties keys, it is not guaranteed to create unique constant names.
It works best if the keys …
- only contain ASCII characters, digits and the separators dot, underscore and dash (aka minus-sign).
- start with a ASCII character.
- end with a ASCII character or a digit.
- never contain two or more consecutive separators.
It is also recommended to only use lower case ASCII characters.
The plugin requires Maven 3.8.2 or later and Java 8 or later.