Reputation: 1835
One of my build scripts imports that nebula plugin:
plugins {
id 'nebula.ospackage' version '3.5.0'
}
I've been moving all of my version info into a separate file that all projects have access to and am wondering what is the correct syntax to convert to something like:
plugins {
id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
}
When I try running the above with "gradle clean build", I get the following error:
build file 'build.gradle': 2: argument list must be exactly 1 literal non empty string
See https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block for information on the plugins {} block
@ line 2, column 33. id 'nebula.ospackage' version "$versions.nebula_gradle_ospackage_plugin"
The linked article shows how I could use the "buildscript" block, which works, but it seems like there must be a way to make this work in a single line?
Upvotes: 96
Views: 44309
Reputation: 1822
From the official Gradle documentation:
Plugin Version Management
A
plugins {}
block insidepluginManagement {}
allows all plugin versions for the build to be defined in a single location. Plugins can then be applied by id to any build script via theplugins {}
block.One benefit of setting plugin versions this way is that the
pluginManagement.plugins {}
does not have the same constrained syntax as the build scriptplugins {}
block. This allows plugin versions to be taken fromgradle.properties
, or loaded via another mechanism.
// gradle.properties
springBootVersion=2.6.7
// settings.gradle.kts
pluginManagement {
val springBootVersion: String by settings
plugins {
id("org.springframework.boot") version springBootVersion
}
}
// build.gradle.kts
plugins {
id("org.springframework.boot")
}
Upvotes: 6
Reputation: 7695
To add yet another approach, you can use plugin resolution rules. This allows you to skip declaring the version in the build.gradle.kts
files and instead declare them in the settings.gradle.kts
file.
This approach allows you to use the "real" name of the plugin and makes sure all of them are the same version. If you need different versions in different projects this may not be a good approach for you.
// build.gradle.kts
plugins {
id("com.autonomousapps.dependency-analysis")
id("com.dorongold.task-tree")
id("com.github.ben-manes.versions")
}
// settings.gradle.kts
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.autonomousapps.dependency-analysis") { useVersion("1.19.0") }
if (requested.id.id == "com.dorongold.task-tree") { useVersion("2.1.1") }
if (requested.id.id == "com.github.ben-manes.versions") { useVersion("0.46.0") }
}
}
}
requested.id.id
is not a typo.
Upvotes: 0
Reputation: 27852
I hit this issue.
when I ran
gradle clean test build
instead of
./gradlew clean test build
and my "gradle" was at version 4.x
but my ./gradlew resolved to 6.9 or higher.
aka, "oops, I forgot my build.gradle files were coded to use later versions of gradle(w), but I snapfu-ed and executed the older 4.x version of regular "gradle"
Upvotes: 0
Reputation: 466
Gradle 7 introduced version catalogs for declaring dependencies and sharing them between projects/modules.
Plugins can be declared in the catalog:
// settings.gradle
dependencyResolutionManagement {
versionCatalogs {
libs {
version('android-plugins', '7.3.0')
plugin('android-application', 'com.android.application').versionRef('android-plugins')
// A library declaration using the same version.
library('some-library', 'com.group', 'artifact').versionRef('android-plugins')
}
}
}
The declaration can be referred to in the plugins
block:
// build.gradle
plugins {
alias(libs.plugins.android.application) apply false
}
The version can also be referred to in the plugins
block:
// build.gradle
plugins {
id 'com.android.application' version libs.versions.android.plugins apply false
}
See the version catalogs documentation for more information.
Note: In the declaration, -
is used as separator in the dependency name. On the consuming side .
is used instead, as -
is the sign for the minus operator of the groovy language and can't be used in property names.
So the separators are different on the declaring and consuming side - which threw me off at first.
Upvotes: 11
Reputation: 17474
This is an old post, but the bug is still open, and I found this post looking for workarounds.
It seems you were aware of this already, and actually have BoygeniusDexter's answer, but I think this may help others finding this post like I did. The following workaround is based on the Gradle docs and solved the problem for me:
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
// and other plugins
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
// but the one with the variable version is applied the old way:
apply plugin: 'org.springframework.boot'
// We can use the variable in dependencies, too:
dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
// ...
}
Upvotes: 15
Reputation: 5820
In addition to @zhouji comment, you can not only specify the versions in gradle.properties
, but you can also define it programmatically like:
buildscript {
ext {
kotlinVersion = "${JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16) ? '1.5.32' : '1.4.32'}"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}"
}
Upvotes: 16
Reputation: 17895
plugin
blockbuildscript.dependencies
block, insteadconst
variableIn short, plugin
blocks require string literals or properties whereas dependency
blocks permit varables. The original question asks for doing this in a "single line" and with this approach, your plugin block is shorter and all your dependencies live in one place for all modules. Depending on your goals, that might be better than doing everything in "one line."
For Android, I was able to do this by omitting the version from the plugin
block and then specifying it as a const
in the buildscript dependencies, instead. That block allows variables, whereas the plugin block only allows string literals. From there, I use an object in buildSrc
because that provides the most flexibility, while keeping all dependency info in one file for all modules. So my setup looks like this:
├── project/
│ ├── build.gradle
| └── app/
| └── build.gradle
| └── buildSrc/
| └── build.gradle.kts
| └── src/main/java/com/example/package/Deps.kt
From there, to specify any plugin version as a variable in one place, the app/build.gradle
file omits the version (using kotlin as an example but the same approach works for any plugin):
plugins {
id 'kotlin-android' // omit version property here but provide it in buildscript dependencies
...
}
...
dependencies {
...
// Dagger
implementation Deps.Dagger.ANDROID_SUPPORT
kapt Deps.Dagger.ANDROID_PROCESSOR
kapt Deps.Dagger.COMPILER
}
Then the buildscript dependency section (usually in the parent build.gradle
file but can also be in the same file) provides the version USING A VARIABLE!
import com.example.package.Deps // object in the buildSrc folder with all version info
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Deps.kotlinVersion}"
}
}
Lastly, all the version information is managed in buildSrc, with an object in the Deps.kt file (there are many blog posts out there on using buildSrc with Android projects):
Deps.ktobject Deps {
// For use in the top-level buildscript dependency section which only works with a constant rather than `Deps.Kotlin.version`
const val kotlinVersion = "1.3.72"
object Kotlin : Version(kotlinVersion) {
val STDLIB = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$version"
}
object Dagger : Version("2.25.2") {
val ANDROID_SUPPORT = "com.google.dagger:dagger-android-support:$version"
val ANDROID_PROCESSOR = "com.google.dagger:dagger-android-processor:$version"
val COMPILER = "com.google.dagger:dagger-compiler:$version"
}
}
open class Version(@JvmField val version: String)
Overall, I really like this setup. It's flexible, all dependency info is in one place, even across multiple modules and, best of all, it allows for code completion in the gradle files, when typing dependencies!
Upvotes: 4
Reputation: 5336
As of Gradle 5.6, you can declare your plugin versions in the gradle.properties
file, and reference these properties in plugins
block.
For example, the gradle.properties
file:
springBootVersion=2.2.0.RELEASE
the plugins
block in build.gradle
:
plugins {
id "org.springframework.boot" version "${springBootVersion}"
}
See: https://github.com/gradle/gradle/issues/1697#issuecomment-506910915.
See also:
Upvotes: 69
Reputation: 2252
You cannot use variable here:
Where «plugin version» and «plugin id» must be constant, literal, strings. No other statements are allowed; their presence will cause a compilation error.
Upvotes: 50