Created
October 8, 2018 17:37
-
-
Save michael-pratt/cb0448903e34ba2bde6938333cb5457c to your computer and use it in GitHub Desktop.
Extending Spring Boot bootJar Task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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