Maven Properties Generator

Java Processor that creates a class with static fields valued with Maven properties

License

License

Categories

Categories

Maven Build Tools
GroupId

GroupId

io.github.luiinge
ArtifactId

ArtifactId

maven-properties-gen
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Maven Properties Generator
Java Processor that creates a class with static fields valued with Maven properties
Project Organization

Project Organization

luiinge

Download maven-properties-gen

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.luiinge/maven-properties-gen/ -->
<dependency>
    <groupId>io.github.luiinge</groupId>
    <artifactId>maven-properties-gen</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.luiinge/maven-properties-gen/
implementation 'io.github.luiinge:maven-properties-gen:1.0.0'
// https://jarcasting.com/artifacts/io.github.luiinge/maven-properties-gen/
implementation ("io.github.luiinge:maven-properties-gen:1.0.0")
'io.github.luiinge:maven-properties-gen:jar:1.0.0'
<dependency org="io.github.luiinge" name="maven-properties-gen" rev="1.0.0">
  <artifact name="maven-properties-gen" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.luiinge', module='maven-properties-gen', version='1.0.0')
)
libraryDependencies += "io.github.luiinge" % "maven-properties-gen" % "1.0.0"
[io.github.luiinge/maven-properties-gen "1.0.0"]

Dependencies

compile (3)

Group / Artifact Type Version
javax.annotation : javax.annotation-api jar 1.3.2
org.apache.maven : maven-model-builder jar 3.6.3
org.slf4j : slf4j-api jar 1.7.30

test (5)

Group / Artifact Type Version
com.google.testing.compile : compile-testing jar 0.19
junit : junit jar 4.13.1
org.hamcrest : hamcrest jar 2.2
org.apache.logging.log4j : log4j-slf4j-impl jar 2.13.0
org.assertj : assertj-core jar 3.16.1

Project Modules

There are no modules declared in this project.

Maven Properties Generator

GitHub JaCoCo GitHub Workflow Status (branch) Maven Central Quality Gate Status Lines of Code Bugs Code Smells Duplicated Lines (%) Technical Debt

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

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.

Versions

Version
1.0.0