Skip to content

Instantly share code, notes, and snippets.

@michael-pratt
Created October 8, 2018 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-pratt/cb0448903e34ba2bde6938333cb5457c to your computer and use it in GitHub Desktop.
Save michael-pratt/cb0448903e34ba2bde6938333cb5457c to your computer and use it in GitHub Desktop.
Extending Spring Boot bootJar Task
package com.elvtn
import org.gradle.api.Plugin
import org.gradle.api.Project
/**
* Custom plugin that adds a new <code>bootDeploy</code> task for any
* project that depends on the Spring Boot gradle plugin. This new task
* depends on the existing <code>bootJar</code> task, and adds some new
* steps that can be used to copy the generated JAR file into a new
* directory, rebuild containers, etc.
* <br/><br/>
* This is the main plugin code. To configure in your own project you also
* need to to 2 things:
* <ol>
* <li>Make sure your top level project has this dependency: <code>compileClasspath "org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE"</code>.</li>
* <li>Create a <code>/buildSrc/src/main/resources/META-INF/gradle-plugins/com.elvtn.bootdeploy.properties</code>
* file in your main project and set <code>implementation-class=com.elvtn.BootDeployPlugin</code>.</li>
* </ol>
*/
class BootDeployPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.afterEvaluate {
def bootJar = project.tasks.getByName("bootJar")
project.tasks.create("bootDeploy", { task ->
task.doLast {
// Copy the fat JAR from bootJar task into a new directory
project.copy { spec ->
spec.from(bootJar.outputs)
spec.into("/apps/lib/")
}
// Stop, rebuild, and restart the associated Docker container
// using a docker-compose file
project.exec { exec ->
exec.workingDir "/docker"
exec.executable "docker-compose"
exec.args = ["rm", "-s", "-f", ${project.name}]
}.assertNormalExitValue()
project.exec { exec ->
exec.workingDir "/docker"
exec.executable "docker-compose"
exec.args = ["build", "--force-rm", ${project.name}]
}.assertNormalExitValue()
project.exec { exec ->
exec.workingDir "/docker"
exec.executable "docker-compose"
exec.args = ["up", "-d"]
}.assertNormalExitValue()
// Could do any other arbitrary command we want, sky's the limit
}
task.dependsOn(bootJar)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment