Skip to content

Commit

Permalink
Enable war file support.
Browse files Browse the repository at this point in the history
  • Loading branch information
alingenhag committed Dec 26, 2019
1 parent 3416b9b commit 3f254db
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
27 changes: 15 additions & 12 deletions src/main/kotlin/rule/DockerImageRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package rule

import org.gradle.api.Task
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.bundling.War
import org.gradle.jvm.tasks.Jar
import org.gradle.kotlin.dsl.get
import org.gradle.model.*
import task.DockerBuildImageTask
import task.DockerPushImageTask
Expand All @@ -17,22 +19,23 @@ class DockerImageRule : RuleSource() {

@Defaults
fun setDefaults(docker: Docker, @Path("tasks.jar") jarTask: Jar?) {
if (jarTask != null) {
// used by copy task
docker.sourceContextPath = "docker"
docker.contextDirectory = File(jarTask.project.buildDir, "docker")
docker.artifactPath = jarTask.project.relativePath(jarTask.archiveFile.get().asFile)
val project = jarTask?.project

// used by build task
docker.relativeDockerfilePath = "."
docker.artifactName = jarTask.archiveFileName.get()
docker.sourceContextPath = "docker"
docker.contextDirectory = File(project?.buildDir, "docker")
docker.relativeDockerfilePath = "."

docker.tag = jarTask.project.version.toString()
docker.host = "docker.pkg.github.com/f-u-z-z-l-e"
docker.imageId = File(docker.contextDirectory, "imageid.txt")
docker.tag = project?.version.toString()
docker.host = "docker.pkg.github.com/f-u-z-z-l-e"
docker.imageId = File(docker.contextDirectory, "imageid.txt")

if (project?.pluginManager!!.hasPlugin("war")) {
val warTask: War = project.tasks["war"] as War
docker.artifactPath = warTask.project.relativePath(warTask.archiveFile.get().asFile)
docker.artifactName = warTask.archiveFileName.get()
} else {
println("Jar task not found! Unable to set default values for dockerBuildImage task!")
docker.artifactPath = jarTask.project.relativePath(jarTask.archiveFile.get().asFile)
docker.artifactName = jarTask.archiveFileName.get()
}
}

Expand Down
35 changes: 32 additions & 3 deletions src/test/kotlin/DockerPluginTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class DockerPluginTest : AbstractPluginTest() {
@Throws(Exception::class)
fun `Apply plugin to project`() {
// given
val eol = System.getProperty("line.separator")
val buildFileContent = """
|plugins {
| id ("ch.fuzzle.gradle.docker-plugin")
Expand Down Expand Up @@ -52,9 +51,8 @@ class DockerPluginTest : AbstractPluginTest() {

@Test
@Throws(Exception::class)
fun `Build docker image and remove it afterwards`() {
fun `Build docker image and remove it afterwards with jar`() {
// given
val eol = System.getProperty("line.separator")
val buildFileContent = """
|plugins {
| id ("ch.fuzzle.gradle.docker-plugin")
Expand All @@ -81,4 +79,35 @@ class DockerPluginTest : AbstractPluginTest() {
MatcherAssert.assertThat(result.output, Matchers.containsString("BUILD SUCCESSFUL"))
}

@Test
@Throws(Exception::class)
fun `Build docker image and remove it afterwards with war`() {
// given
val buildFileContent = """
|plugins {
| id ("war")
| id ("ch.fuzzle.gradle.docker-plugin")
|}
|
|group = "ch.fuzzle"
|
|version = "1.0.0"
|""".trimMargin()

writeFile(buildFile, buildFileContent)

// when
val result = GradleRunner.create()
.withProjectDir(projectDir)
.withPluginClasspath()
.withArguments("build", "dockerBuildImage", "dockerRemoveImage")
.forwardOutput()
.withJaCoCo()
.build()

// then
MatcherAssert.assertThat(result.output, Matchers.containsString("5 actionable tasks: 5 executed"))
MatcherAssert.assertThat(result.output, Matchers.containsString("BUILD SUCCESSFUL"))
}

}

0 comments on commit 3f254db

Please sign in to comment.