How can I apply the Gradle Versions Plugin from an initscript? Ask Question
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 Based on this answer to a related question, I tried creating a file
build.gradle
~/.gradle/init.d/50-ben-manes-versions.gradle
files.
:
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: manes.versions' ). With that I get:
apply plugin: 'com.github.ben-
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. gradle edited Jan 19 at 17:58
asked Jan 18 at 23:54
Laurence Gonsalves 95.2k
24
169
229
First thing is com.github.ben-manes.versions is not surrounded by quotes. When you apply a plugin like that, you are applying it by the id. – mkobit Jan 19 at 3:42
@mkobit As mentioned in the question, I tried both with and without quotes. – Laurence Gonsalves Jan 19 at 3:53
Sorry, totally my fault, I'm laying in bed and missed that. I'll take a look at this tomorrow. – mkobit Jan 19 at 3:58
@mkobit no problem! I thought the no-quotes version looked word too, but that other answer seemed to imply that the lack of quotes was important. Anyway, if you're able to take another look tomorrow I'd appreciate it. Thanks! – Laurence Gonsalves Jan 19 at 4:20
1 Answer
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/gradleplugins/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 } edited Mar 22 at 19:15
answered Jan 19 at 12:56
Laurence Gonsalves
mkobit
95.2k
16k
24
169
229
6
69
79
Thank you for the detailed answer that actually explains what's going on here! – Laurence Gonsalves Jan 19 at 17:57
Doing so results in Could not get unknown property 'com' for root project 'edited' of type org.gradle.api.Project. – Brice Mar 20 at 13:41
@Brice is that from an init script? Might be easier with a new question and full example. – mkobit Mar 20 at 14:11
@mkobit I don't reqally understand what was wrong. However my project sricpt is apply external script that have other buildscript seciton. So moving the apply plugin elsewhere in those scripts worked. – Brice Mar 20 at 15:59
Join Stack Overflow to learn, share knowledge, and build your career. Email Sign Up OR SIGN IN WITH
Google Facebook