Maven Properties Generator
Sometimes it is desirable to access to properties defined in the Maven pom.xml
file of your project from a Java process. The most straightforward solution is copy them as constants in some class, but then you will have the burden of maintaining those values up to date with every change in the pom.xml
.
There are workarounds that allow you to extract Maven properties to a text file that can be read later from a Java process. However, such techniques are not suited for certain scenarios where static values are required (for instance, property values within annotations must be static).
This annotation processor fills that gap, creating a class with static constants and updating it every time you compile your project.
Usage
-
Include the dependency in your
pom.xml
:<dependency> <groupId>io.github.luiinge</groupId> <artifactId>maven-properties-gen</artifactId> <version>1.0.0</version> </dependency>
-
Create a new interface annotated with
@ProjectProperties
. Add as many methods as you want, annotated with@ProjectProperty
and the Maven property identifier. For example:package my.project; @ProjectProperties public interface ProjectInformation { @ProjectProperty("${project.version}") String staticVersion(); @ProjectProperty("${project.description}") String description(); @ProjectProperty("${project.inceptionYear}") int year(); }
-
According your Maven build configuration, you may need to add the processor path in your
pom.xml
:<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> <annotationProcessorPaths> <annotationProcessorPath> <groupId>io.github.luiinge</groupId> <artifactId>maven-properties-gen</artifactId> <version>1.0.0</version> </annotationProcessorPath> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
And that is it. After compiling, a new class would have been generated in the folder target/generated-sources/annotations
. Using the previous example, the output would be something like the following:
package my.project;
public final class StaticProjectInformation implements ProjectInformation {
public static final String STATIC_VERSION = "1.0.0";
public static final String DESCRIPTION = "This is the description of my project";
public static final int YEAR = Integer.parseInt("2020");
@Override
public String staticVersion() {
return STATIC_VERSION;
}
@Override
public String description() {
return DESCRIPTION;
}
@Override
public int year() {
return YEAR;
}
}
Other considerations
Property interpolation
You are not limited to reference properties explicitly declared in your pom.xml
. Since this processor uses the Maven interpolation engine instead of plainly reading the file, you can reference implicit properties o even inherited properties from a parent project.
Accepted types
Although there is no constraints related to the method names, they must return one of the following types:
String
- any of the primitive types (
byte
,short
,int
,long
,float
,double
) - any of the corresponding boxed types (
Byte
,Short
,Integer
,Long
,Float
,Double
)
The value parsing is performed using the proper existing methods of each type (for example, Integer:parseInt
for int
). Notice that the processor is not responsible for the correctness of the values within the pom.xml
file, so it may result in parsing errors at compilation time if they are not valid.
Also, attempting to use any other type will result in an compiler error as well.
Processor options
You can customize the package and name of the generated classes with the following options:
option | description | default value* |
---|---|---|
generatedClass |
name of the generated class | Static%s |
generatedPackage |
enclosing package of the generated class | %s |
*being %s
a placeholder for the original name.
Pre-compile generation
If you need static access to the Maven properties ahead of compiling (for instance, if another annotation processor tries to read the static properties), you might have to define a compile
goal at the process-sources
phase and set the compile parameter proc
to only
. This way, you would generate the output of this processor before attempting the actual compilation:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<executions>
<!-- pre-compile execution -->
<execution>
<id>generate-sources</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<proc>only</proc>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.github.luiinge</groupId>
<artifactId>maven-properties-gen</artifactId>
<version>${maven-properties-gen.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<!-- actual compilation -->
<execution>
<id>compile-default</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<annotationProcessorPaths>
<!-- other annotation processors... -->
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Authors
- Luis Iñesta Gelabert | [email protected]
Contributions
If you want to contribute to this project, visit the Github project. You can open a new issue / feature request, or make a pull request to consider. You will be added as a contributor in this very page.
Issue reporting
If you have found any defect in this software, please report it in Github project Issues. There is no guarantee that it would be fixed in the following version but it would be addressed as soon as possible.
License
MIT License
Copyright (c) 2020 Luis Iñesta Gelabert - [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.