Skip to content

Plugin that processes compiled `*.class` files and inserts compile-time constants

License

Notifications You must be signed in to change notification settings

remal-gradle-plugins/build-time-constants

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tested on Java LTS versions from 8 to 21.

Tested on Gradle versions from 6.7 to 8.11.1.

name.remal.build-time-constants.jvm plugin

configuration cache: supported

The plugin automatically adds name.remal.gradle-plugins.build-time-constants:build-time-constants-api dependency to compileOnly configuration of every SourceSet. The version of the added dependency will be the same as the plugin version.

You can use name.remal.gradle_plugins.build_time_constants.api.BuildTimeConstants class in any JVM-language statically compiled code. The plugin processes *.class files and injects build-time constants instead of BuildTimeConstants methods invocation.

Using build-time properties

Let's say, a developer would like to use the current version of the project in the project's code.

Common ways to do so are code generation are sources preprocessing. It's quite complex.

This plugin provides a simpler solution.

First, set property as a build-time property for replacement:

buildTimeConstants.property('version', project.version)

Second, use BuildTimeConstants.getStringProperty("version") in the code.

Third, the plugin will replace getStringProperty invocation with the value of project.version.

See the Javadoc documentation for other BuildTimeConstants.get*Property() and BuildTimeConstants.get*Properties() methods.

Decoupling from class literals

When working with optional dependencies, class names are usually used instead of class literals (java.lang.Object instead of Object.class). The issue with class names as strings is that they are not checked at build-time and can be missed during refactoring.

This plugin can convert class literals to string values. For example, BuildTimeConstants.getClassName(Object.class) will be converted to java.lang.Object.

See the Javadoc documentation for other BuildTimeConstants.getClass*() methods.