Configuring com.palantir.docker tag at Execution Phase
Internal
Overview
This is an example of a custom task that configures the Docker image tag at execution phase. Normally, DockerExtension configuration elements can be configured in build.gradle only at configuration phase. If any of the configuration elements, such as the tag, must be computed dynamically at execution phase, as result of another task execution, it could be done by pulling the DockerExtension instance and invoking the corresponding setter, as shown below.
Identify the Plugin Library
The plugin is declared as follows in a regular sub-project build.gradle:
plugins {
// some 'com.palantir.docker' plugin classes are used by this project's Gradle extensions,
// defined in buildSrc, so plugin libraries are declared as buildSrc dependencies, and the
// plugin version is defined there. See buildSrc/build.gradle.
id "com.palantir.docker"
}
Note that the version cannot be declared here if the backing library is to be configured as a buildSrc dependency. It will be declared in buildSrc build.gradle.
Decide on a version, identify the location in the plugin repository: https://plugins.gradle.org/m2/. Go to https://plugins.gradle.org/m2/com/palantir/docker/, then to https://plugins.gradle.org/m2/com/palantir/docker/com.palantir.docker.gradle.plugin/0.25.0/ and inspect the plugin's POM:
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.palantir.docker</groupId>
<artifactId>com.palantir.docker.gradle.plugin</artifactId>
<version>0.25.0</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.palantir.gradle.docker</groupId>
<artifactId>gradle-docker</artifactId>
<version>0.25.0</version>
</dependency>
</dependencies>
</project>
The group ID and artifact ID of the library are available as <dependency>.
Configure buildSrc build.gradle
Note that the plugin/library version is specified here and it will also dictate the plugin version used in sub-projects:
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
implementation "com.palantir.gradle.docker:gradle-docker:0.25.0"
}
Define a Task that Sets the Tag Version at Execution Phase
package ...;
import com.palantir.gradle.docker.DockerExtension;
import org.gradle.api.DefaultTask;
...
public class UpdateTagAtExecutionPhase extends DefaultTask {
@TaskAction
void action() {
Project project = getProject();
ExtensionContainer ec = project.getExtensions();
DockerExtension de = (DockerExtension)ec.getByName("docker");
String name = de.getName();
// the name includes the tag, trailing ":"
name += "-TEST";
de.setName(name);
}
}