How can I apply the Gradle Versions Plugin from an initscript?
gradle
Solution
Plugins can be applied in a few different ways. In the referenced answer from the question, they are being applied by type. You can also apply by plugin Id, which is a `String`.
In your second attempt when you are applying by Id you are doing the right thing, but the build errors out with:
Plugin with id 'com.github.ben-manes.versions' not found.
The issue here is that you currently cannot apply plugins by Id from init scripts (#gradle/1322).
The solution is to instead, apply the plugin by type.
Luckily, the plugin is open source so it relatively simple to discover the type of the plugin. The plugin Id is `com.github.ben-manes.versions`, which leads us to the `META-INF/gradle-plugins/com.github.ben-manes.versions.properties` file. This file contains the line `implementation-class=com.github.benmanes.gradle.versions.VersionsPlugin`, which tells us that the type of the plugin is `com.github.benmanes.gradle.versions.VersionsPlugin`. This could have also be determined by applying the plugin to the build (instead of through an init script) and inspecting the `plugins` or `pluginManager` from the project to list out the plugin types.
To make the fix change this line:
apply plugin: 'com.github.ben-manes.versions'
to:
apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin
So the full, working initscript is:
initscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
}
}
allprojects {
apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin
}
Problem
I'm trying to set things up so that I can use the Gradle Versions Plugin without having to add it to all of my `build.gradle` files. Based on this answer to a related question, I tried creating a file `~/.gradle/init.d/50-ben-manes-versions.gradle`: ``` initscript { repositories { jcenter() } dependencies { classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0' } } allprojects { apply plugin: com.github.ben-manes.versions } ``` If I then try to invoke `./gradlew dependencyUpdates` in my repo, I get: ``` FAILURE: Build failed with an exception. * Where: Initialization script '~/.gradle/init.d/50-ben-manes-versions.gradle' line: 13 * What went wrong: Could not get unknown property 'com' for root project 'project-name' of type org.gradle.api.Project. ``` That answer said to not use quotes around the plugin name, but since that didn't work I tried adding quotes (ie: `apply plugin: 'com.github.ben-manes.versions'`). With that I get: ``` FAILURE: Build failed with an exception. * Where: Initialization script '~/.gradle/init.d/50-ben-manes-versions.gradle' line: 13 * What went wrong: Plugin with id 'com.github.ben-manes.versions' not found. ``` Is there any way to apply the Gradle Versions Plugin from an initscript? I am using Gradle 4.3.1, by the way.