From 3f254dbb767bf3ff5cbe1b9a9f63a963a6122f81 Mon Sep 17 00:00:00 2001 From: alingenhag <11538311+alingenhag@users.noreply.github.com> Date: Thu, 26 Dec 2019 15:09:30 +0100 Subject: [PATCH] Enable war file support. --- src/main/kotlin/rule/DockerImageRule.kt | 27 ++++++++++--------- src/test/kotlin/DockerPluginTest.kt | 35 ++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/rule/DockerImageRule.kt b/src/main/kotlin/rule/DockerImageRule.kt index c60c0eb..43f93da 100644 --- a/src/main/kotlin/rule/DockerImageRule.kt +++ b/src/main/kotlin/rule/DockerImageRule.kt @@ -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 @@ -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() } } diff --git a/src/test/kotlin/DockerPluginTest.kt b/src/test/kotlin/DockerPluginTest.kt index dd09d74..235f919 100644 --- a/src/test/kotlin/DockerPluginTest.kt +++ b/src/test/kotlin/DockerPluginTest.kt @@ -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") @@ -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") @@ -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")) + } + } \ No newline at end of file