Pass arguments through command line to a gradle project without a main file

51 Views Asked by At

I am developing a plugin for intellij and I have a gradle.build.kts file. I build the binary using "./gradlew build". I want to pass arguments while building so for example something like "./gradlew build -DbuildType="prod"". I am trying to access this value using System.getProperty("buildType") but this returns null. The project does not have a main file.

1

There are 1 best solutions below

0
Simon Jacobs On

One simple way of sharing values between a Gradle build file and the target program is to use the Gradle BuildConfig plugin

Here you can write the following in your build.gradle.kts:

plugins {
    id("com.github.gmazzo.buildconfig") version "5.3.5"
}

buildConfig {
    buildConfigField("APP_NAME", project.name)
    buildConfigField("THE_ANSWER", 42)
}

And it will generate an object of constants in your project for you to access:

internal object BuildConfig {
    internal const val APP_NAME: String = "<project name>"
    internal const val THE_ANSWER: Int = 42
}