Skip to content

Instantly share code, notes, and snippets.

@ajoberstar
Last active December 9, 2016 03:29
Show Gist options
  • Save ajoberstar/2d480eba473ce17f6400 to your computer and use it in GitHub Desktop.
Save ajoberstar/2d480eba473ce17f6400 to your computer and use it in GitHub Desktop.
Class to work around the coupling between Gradle version of build and Gradle version used as dependency for plugins.
package org.ajoberstar.gradle.hack
import org.gradle.api.Project
import org.gradle.api.file.FileTree
import org.gradle.wrapper.Download
import org.gradle.wrapper.Install
import org.gradle.wrapper.Logger
import org.gradle.wrapper.PathAssembler
import org.gradle.wrapper.WrapperConfiguration
/**
* Include this in your project's buildSrc, then add a dependency to your project:
* compile new GradleDist(project, '2.11').asFileTree
*
* This is a complete hack and uses internal Gradle APIs. This occassionally causes
* it to break when upgrading the Gradle version of your build due to changes in the
* Wrapper APIs. Tends to be pretty easy to update at that point though.
*/
class GradleDist {
private final Project project
private final URI uri
private GradleDist(Project project, URI uri) {
this.project = project
this.uri = uri
}
FileTree getAsFileTree() {
Logger logger = new Logger(true)
Install install = new Install(logger, new Download(logger, 'gradle', ''), new PathAssembler(project.gradle.gradleUserHomeDir))
WrapperConfiguration config = new WrapperConfiguration()
config.distribution = uri
File file = install.createDist(config)
return project.fileTree(dir:file, include:'**/*.jar')
}
static GradleDist create(Project project, String version) {
return new GradleDist(project, new URI("https://services.gradle.org/distributions/gradle-${version}-bin.zip"))
}
static GradleDist createNightly(Project project, String version) {
return new GradleDist(project, new URI("https://services.gradle.org/distributions-snapshots/gradle-${version}-bin.zip"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment