ConstGen Maven Plugin

Simple constant util file generation based on compile time maven properties (e.g. project version)

License

License

Categories

Categories

Maven Build Tools
GroupId

GroupId

com.mktiti
ArtifactId

ArtifactId

const-generator-maven-plugin
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

maven-plugin
Description

Description

ConstGen Maven Plugin
Simple constant util file generation based on compile time maven properties (e.g. project version)
Project URL

Project URL

https://github.com/mktiti/ConstGeneratorMavenPlugin
Project Organization

Project Organization

hu.mktiti
Source Code Management

Source Code Management

https://github.com/mktiti/ConstGeneratorMavenPlugin

Download const-generator-maven-plugin

How to add to project

<plugin>
    <groupId>com.mktiti</groupId>
    <artifactId>const-generator-maven-plugin</artifactId>
    <version>1.1</version>
</plugin>

Dependencies

compile (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-compiler-embeddable jar 1.3.41

provided (4)

Group / Artifact Type Version
org.apache.maven : maven-plugin-api jar 3.3.9
org.apache.maven : maven-core jar 3.3.9
org.apache.maven : maven-artifact jar 3.3.9
org.apache.maven.plugin-tools : maven-plugin-annotations jar 3.6.0

test (4)

Group / Artifact Type Version
com.itranswarp : compiler jar 1.0
org.apache.maven : maven-compat jar 3.3.9
junit : junit jar 4.12
org.apache.maven.plugin-testing : maven-plugin-testing-harness jar 3.3.0

Project Modules

There are no modules declared in this project.

Const Generator Maven Plugin

License: GPL v3 Build Status Maven Central

A simple maven plugin to generate util classes or objects containing compile time values for Java, Kotlin, and other JVM languages. Useful for dynamically including artifact version, build time or other build dependent values in your source for easy access.

Adding to your maven project

To enable constant generation, add the following plugin execution to your build config (build > plugins):

<plugin>
     <groupId>com.mktiti</groupId>
     <artifactId>const-generator-maven-plugin</artifactId>
     <version>1.1</version>
    
     <configuration>
         <packageName>my.project.generated</packageName>
     </configuration>
    
     <executions>
         <execution>
             <id>generate-const</id>
             <phase>generate-sources</phase>
             <goals>
                 <goal>generate</goal>
             </goals>
         </execution>
     </executions>
</plugin>

Code generation happens on the generate goal, invoke it with mvn const-generator:generate.

To generate automatically, add the goal execution to the generate-sources phase (as seen above).

Also add the output directory (${project.build.directory}/src/main/{java/kotlin} by default, usually target/src/main/{java/kotlin}) to your sources, and optionally mark the generated directory as 'Generated sources' or similar in your IDE.

Java compiler sample config (build > plugins):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    
    <configuration>
        <generatedSourcesDirectory>${project.build.directory}/src/main/java</generatedSourcesDirectory>
	<!-- Other configs -->
    </configuration>
    
    <!-- Executions, etc. -->
</plugin>

Kotlin compiler sample config (build > plugins):

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    
    <configuration>
        <sourceDirs>
            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
	    <sourceDir>${project.build.directory}/src/main/kotlin</sourceDir>
        </sourceDirs>
	<!-- Other configs -->
    </configuration>
    
    <!-- Executions, etc. -->
</plugin>

Now you can access the generated properties from your code. (Version is added by default)

// Java
System.out.println("Running version: " + ProjectInfo.version); // Running version: 1.0-SNAPSHOT

// Kotlin
println("Running version: ${ProjectInfo.version}") // Running version: 1.0-SNAPSHOT

Multiple executions are supported for multiple file generation (each with their own config in the execution tag).

Configuration

Settings

Config key Usage Default
outputDirectory Path to generate the sources in ${project.build.directory}/src/main/
(e.g. target/src/main)
type Type of source to generate (java/kotlin) java
packageName Package of the generated class/object No default
className Name of the generated class/object ProjectInfo
visibility Visibility modifier of the class/object and members
One of PUBLIC, PACKAGE_PRIVATE, DEFAULT
PUBLIC
useGetters Whether to use static getters (without backing fields) or static fields false
addVersion Sets whether the project version should be added true
values Key-Value map of the values to be added (e.g. <foo>bar</foo>) empty

Examples

Basic example with defaults only

By default the project version is added (as it is the most often used option), this can be disabled through the addVersion option.

The only required option is packageName.

Config:

<configuration>
    <packageName>my.project.generated</packageName>
</configuration>

Output (target/src/main/java/my/project/generated/ProjectInfo.java):

package my.project.generated;

// Generated by const-generator-maven-plugin at 2019-07-17T23:47:46.924

public final class ProjectInfo {

	private ProjectInfo() {
		throw new AssertionError("Generated static utility class, should not be instantiated");
	}

	public static final String version = "1.0-SNAPSHOT";

}

Custom values

You can add your own custom values (constants or maven properties) using the values map.

Config:

<configuration>
    <packageName>my.project.generated</packageName>
    <values>
        <foo>bar</foo>
        <empty></empty>
        <builtAt>${maven.build.timestamp}</builtAt>
        <builtBy>${user.name}</builtBy>
    </values>
</configuration>

Output (target/src/main/java/my/project/generated/ProjectInfo.java):

package my.project.generated;

// Generated by const-generator-maven-plugin at 2019-07-17T23:52:54.796

public final class ProjectInfo {

	private ProjectInfo() {
		throw new AssertionError("Generated static utility class, should not be instantiated");
	}

	public static final String builtBy = "titi";

	public static final String builtAt = "2019-07-17T21:52:54Z";

	public static final String foo = "bar";

	public static final String version = "1.0-SNAPSHOT";

	public static final String empty = "";

}

Getters instead of fields

Change the generation type to getters with useGetters.

Config:

<configuration>
    <packageName>my.project.generated</packageName>
    <values>
        <foo>bar</foo>
    </values>
    <useGetters>true</useGetters>
</configuration>

Output (target/src/main/java/my/project/generated/ProjectInfo.java):

package my.project.generated;

// Generated by const-generator-maven-plugin at 2019-07-17T23:56:03.046

public final class ProjectInfo {

	private ProjectInfo() {
		throw new AssertionError("Generated static utility class, should not be instantiated");
	}

	public static String getFoo() {
		return "bar";
	}

	public static String getVersion() {
		return "1.0-SNAPSHOT";
	}

}

Kotlin generation

Beside java, kotlin source generation is also supported, this is to make integration for kotlin-only projects easier.

Kotlin generation is designed as to allow easy access from java code.

Use the type config option to set the generation type (kotlin/java).

Config:

<configuration>
    <packageName>my.project.generated</packageName>
    <type>kotlin</type>
    <values>
        <foo>bar</foo>
    </values>
</configuration>

Output (target/src/main/kotlin/generated/ProjectInfo.kt):

@file:JvmName("ProjectInfo")

package my.project.generated

// Generated by const-generator-maven-plugin at 2019-07-17T23:58:25.138

public object ProjectInfo {

	@JvmField public val foo: String = "bar"

	@JvmField public val version: String = "1.0-SNAPSHOT"

}

Kotlin getters

Config:

<configuration>
    <packageName>my.project.generated</packageName>
    <type>kotlin</type>
    <values>
        <foo>bar</foo>
    </values>
    <useGetters>true</useGetters>
</configuration>

Output (target/src/main/kotlin/generated/ProjectInfo.kt):

@file:JvmName("ProjectInfo")

package my.project.generated

// Generated by const-generator-maven-plugin at 2019-07-18T00:16:31.070

public object ProjectInfo {

	@JvmStatic @get:JvmName("getFoo") public val foo: String = "bar"

	@JvmStatic @get:JvmName("getVersion") public val version: String = "1.0-SNAPSHOT"

}

Other settings

There are various other config settings to use.

Config:

<configuration>
    <packageName>my.project.generated</packageName>
    <type>java</type>
    <className>MetaInfo</className>
    <addVersion>false</addVersion>
    <visibility>PUBLIC</visibility>
    <outputDirectory>${project.basedir}/gen</outputDirectory>
    <values>
        <foo>bar</foo>
    </values>
</configuration>

Output (gen/java/my/project/generated/MetaInfo.java):

package my.project.generated;

// Generated by const-generator-maven-plugin at 2019-07-18T00:02:08.427

public final class MetaInfo {

	private MetaInfo() {
		throw new AssertionError("Generated static utility class, should not be instantiated");
	}

	public static final String foo = "bar";

}

Versions

Version
1.1