Defining a task in a subscript


(Peter Streef) #1

To avoid rewriting code I would like to use a subscript. This works fine for simple stuff but gets harder for implementing other plugins.

I’m using a docker plugin (com.bmuschko.gradle.docker) to create images for my projects. Until now I would just copy past all my code in the build.gradle file. I would like to use a seperate .gradle file for this.

How would I go about doing this? I won’t mind having to include stuff in the build.gradle file other than the apply from “subscript.gradle”.

I currently have something like this in my docker subscript:

apply plugin: ‘com.bmuschko.docker-remote-api’

def dockerRegistryHost = “registry:5000”

docker {
url = ‘https://docker-builder:2375
}

task createDockerfile(type: com.bmuschko.gradle.docker.tasks.image.Dockerfile) {
destFile = project.file(‘build/docker/Dockerfile’)

}

however, this crashes on the class name on DockerFile (com.bm…DockerFile)

I have the feeling I’m missing something fundamental about how gradle scripts are build and that “apply from” doesn’t just insert the code into the main gradle file.


(Stefan Oehme) #2

That’s right, it doesn’t. The script is evaluated in isolation, so you’d have to add the docker plugin to its buildscript dependencies. However, then your project build script won’t be able to see the docker plugin types. This is one of the shortcomings of the plugin system that we will address by the middle of this year.


(Peter Streef) #3

Ok, good to know. So is there some other way? Perhaps extending the plugin or something? Or am I forced to keep repeating myself in every subproject that needs this docker plugin?


(Stefan Oehme) #4

Ite depends: Do you need to access the types in both the common script and the project scripts? If not, you can just extract it into the common script. If you need a combination of some reusable logic and some logic that lives in a specific project and both need to see the same type, then you can currently use buildSrc to write a plugin and then apply that plugin in your projects.