Adam
Adam

Reputation: 10016

Managing version numbers in a custom Gradle Plugin

I'm developing a custom gradle plugin. At the top of the build.gradle file of the plugin I specify the plugin's version number:

version '0.1.3'

I have a number of Gradle projects that I use during my custom plugin's unit and integration testing phase. For each of these projects, I have to declare my custom plugin's jar as a dependency in the project's build.gradle file . For example:

classpath files('libs/UmpleGradlePlugin-0.1.3.jar')

Ideally, I'd have the version number written in one place rather than explictly writing it in every test project I use. To that end, I've created a constants file, Constants.java, that includes static final string VERSION_NUMBER = "0.1.3". I want to use VERSION_NUMBER in the build files of my projects rather than rather than writing the version number explicitly.

I've compiled Constants.java and added Constants.class to the classpath. However, when I try to use Constants.VERSION_NUMBER in a build.gradle file, Gradle thinks I'm trying to use a property that doesn't exist:

FAILURE: Build failed with an exception. Could not find property 'Constants' on root project 'UmpleGradlePlugin'.

How can I use VERSION_NUMBER in a build file?

Upvotes: 0

Views: 196

Answers (1)

Kright
Kright

Reputation: 91

If you want to use the class from build.gradle, you have to add it as a buildscript dependency:

buildscript {
    dependencies {
        classpath files('libs/UmpleGradlePlugin-0.1.3.jar')
    }
}

And after you can use it directly from build.gradle. For example:

println("pi = ${Math.PI}") // works and prints static variable.
println("2 * pi = ${java.lang.Math.PI}") // works fine too

I think line "import com.package.Constants" isn't required, but I'm not sure about this.

Upvotes: 1

Related Questions