Using JitPack to Install Gradle Plugins from Git Sources
Today, I was trying to release the Jenkins Job DSL plugin when I found that I needed to upgrade a couple of plugins to resolve an issue with a Gradle upgrade.
As mentioned in upstream issues for the affected plugins, the fix had been made on the primary branch but the plugin had not yet been released.
One option, as mentioned by folks on the issues, is to publish them yourself or vendor them into the project, which can be messy and frustrating.
An alternative is something I found super useful when working with the Spotless project, called out in their contributing docs, which is the JitPack service.
JitPack is a service that makes it possible to build JVM libraries from a number of Git hosting tools. This allows us to point Gradle to the JitPack repository for specific dependencies, where JitPack will build an artefact for us for the given Git commit and publish it so we can consume it.
Let's say that your build.gradle
currently looks like this:
plugins {
// in my opinion, removing `version` makes it more obvious that we're using JitPack, but it can stay too
id 'com.eriwen.gradle.css' version '2.14.0'
id 'com.eriwen.gradle.js' version '2.14.1'
}
In our settings.gradle
, we need to add the Maven repository for JitPack, and then specify a resolutionStrategy
for each of the plugins that we want to pull, ensuring that we specify a version that is either a Git SHA for the commit we want to build, or specify a branch name:
pluginManagement {
repositories {
gradlePluginPortal()
maven {
url 'https://jitpack.io'
}
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'com.eriwen.gradle.css') {
useModule('com.github.eriwen:gradle-css-plugin:9fe88d7') // can also be a branch, i.e. `master-SNAPSHOT`
}
if (requested.id.id == 'com.eriwen.gradle.js') {
useModule('com.github.eriwen:gradle-js-plugin:d15f4ae') // can also be a branch, i.e. `master-SNAPSHOT`
}
}
}
}
And just like that, you don't need to worry (as much) about plugins not yet being released!