Pass arguments through CLI while building plugin for intellij. I am using gradle

40 views Asked by At

I am developing a plugin for intellij which I build using the command "./gradlew clean build" I want to pass the build type in the command and use it in the code. There is no main file in the code.I want to use something like "./gradlew clean build --buildType=prod". I am not able to get the value of buildType in the project. It returns null.

1

There are 1 answers

2
Hangrybear666 On

Use the P flag like so ./gradlew clean build -PbuildType=prod to pass Grade Project Properties.

You can access it in build.gradle via Groovy DSL

build {
    String buildTypeStr = project.getProperty("buildType");
    println 'buildType from -P: ' + project.property('buildType')
    println 'buildType from local variable: ' + buildTypeStr
}

You can access it in build.gradle.kts via Kotlin DSL

build {
    val buildTypeStr: String = project.getProperty("buildType");
    println "buildType from -P = " + project.property("buildType")
    println "buildType from local variable: " + buildTypeStr
}

If you need to access it in your JVM (or in Java Classes) you can use the -D flag to pass System Properties -DbuildType=prod and access it in Java via System.getProperty("buildType")

Further reading