Allow Docker to cache app libraries dir in DockerJavaApplication plugin
MODIFY
README.asciidoc
README.asciidoc
@@ -501,7 +501,7 @@ The plugin provides a set of tasks for your project and preconfigures them with
[options="header"]
|=======
|Task name |Depends On |Type |Description
-|`dockerCopyDistResources` |`distTar` |link:http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/Copy.html[Copy] |Copies the resource files (like the Java application's TAR file) to a temporary directory for image creation.
+|`dockerCopyDistResources` |`distTar` |link:http://www.gradle.org/docs/current/javadoc/org/gradle/api/tasks/Sync.html[Sync] |Copies the resource files (like the Java application's TAR file) to a temporary directory for image creation.
|`dockerDistTar` |`dockerCopyDistResources` |Dockerfile |Creates the Docker image for the Java application.
|`dockerBuildImage` |`dockerDistTar` |DockerBuildImage |Builds the Docker image for the Java application.
|`dockerPushImage` |`dockerBuildImage` |DockerPushImage |Pushes created Docker image to the repository.

MODIFY
src/functTest/groovy/com/bmuschko/gradle/docker/DockerJavaApplicationPluginFunctionalTest.groovy
src/functTest/groovy/com/bmuschko/gradle/docker/DockerJavaApplicationPluginFunctionalTest.groovy
@@ -21,8 +21,9 @@ class DockerJavaApplicationPluginFunctionalTest extends AbstractFunctionalTest {
dockerfile.text ==
"""FROM java
MAINTAINER ${System.getProperty('user.name')}
-ADD ${projectName}-1.0.tar /
-ENTRYPOINT ["/${projectName}-1.0/bin/${projectName}"]
+ADD ${projectName} /${projectName}
+ADD app-lib/${projectName}-1.0.jar /$projectName/lib/$projectName-1.0.jar
+ENTRYPOINT ["/${projectName}/bin/${projectName}"]
EXPOSE 8080
"""
result.output.contains('Author : ')
@@ -54,8 +55,9 @@ EXPOSE 8080
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER Benjamin Muschko "benjamin.muschko@gmail.com"
-ADD ${projectName}-1.0.tar /
-ENTRYPOINT ["/${projectName}-1.0/bin/${projectName}"]
+ADD ${projectName} /${projectName}
+ADD app-lib/${projectName}-1.0.jar /$projectName/lib/$projectName-1.0.jar
+ENTRYPOINT ["/${projectName}/bin/${projectName}"]
EXPOSE 9090
"""
result.output.contains('Author : Benjamin Muschko "benjamin.muschko@gmail.com"')
@@ -87,8 +89,9 @@ EXPOSE 9090
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER Benjamin Muschko "benjamin.muschko@gmail.com"
-ADD ${projectName}-1.0.tar /
-ENTRYPOINT ["/${projectName}-1.0/bin/${projectName}"]
+ADD ${projectName} /${projectName}
+ADD app-lib/${projectName}-1.0.jar /$projectName/lib/$projectName-1.0.jar
+ENTRYPOINT ["/${projectName}/bin/${projectName}"]
EXPOSE 9090 8080
"""
result.output.contains('Author : Benjamin Muschko "benjamin.muschko@gmail.com"')
@@ -124,7 +127,8 @@ EXPOSE 9090 8080
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER Benjamin Muschko "benjamin.muschko@gmail.com"
-ADD ${projectName}-1.0.tar /
+ADD ${projectName} /${projectName}
+ADD app-lib/${projectName}-1.0.jar /$projectName/lib/$projectName-1.0.jar
CMD ["arg1"]
ENTRYPOINT ["bin/run.sh"]
EXPOSE 9090
@@ -158,7 +162,8 @@ EXPOSE 9090
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER Benjamin Muschko "benjamin.muschko@gmail.com"
-ADD ${projectName}-1.0.tar /
+ADD ${projectName} /${projectName}
+ADD app-lib/${projectName}-1.0.jar /$projectName/lib/$projectName-1.0.jar
EXPOSE 9090
"""
@@ -206,8 +211,9 @@ EXPOSE 9090
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER Benjamin Muschko "benjamin.muschko@gmail.com"
-ADD ${projectName}-1.0.tar /
-ENTRYPOINT ["/${projectName}-1.0/bin/${projectName}"]
+ADD ${projectName} /${projectName}
+ADD app-lib/${projectName}-1.0.jar /$projectName/lib/$projectName-1.0.jar
+ENTRYPOINT ["/${projectName}/bin/${projectName}"]
EXPOSE 9090
ADD file1.txt /some/dir/file1.txt
ADD file2.txt /other/dir/file2.txt
@@ -219,6 +225,7 @@ ADD file2.txt /other/dir/file2.txt
@Requires({ TestPrecondition.DOCKERHUB_CREDENTIALS_AVAILABLE })
def "Can create image for Java application and push to DockerHub"() {
+ String projectName = temporaryFolder.root.name
createJettyMainClass()
writeBasicSetupToBuildFile()
Properties gradleProperties = TestPrecondition.readDockerHubCredentials()
@@ -253,13 +260,15 @@ ADD file2.txt /other/dir/file2.txt
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER ${System.getProperty('user.name')}
-ADD javaapp-1.0.tar /
-ENTRYPOINT ["/javaapp-1.0/bin/javaapp"]
+ADD javaapp /javaapp
+ADD app-lib/${projectName}-1.0.jar /javaapp/lib/$projectName-1.0.jar
+ENTRYPOINT ["/javaapp/bin/javaapp"]
EXPOSE 8080
"""
}
def "Can create image for Java application and push to private registry"() {
+ String projectName = temporaryFolder.root.name
createJettyMainClass()
writeBasicSetupToBuildFile()
buildFile << """
@@ -282,8 +291,9 @@ EXPOSE 8080
dockerfile.text ==
"""FROM $CUSTOM_BASE_IMAGE
MAINTAINER ${System.getProperty('user.name')}
-ADD javaapp-1.0.tar /
-ENTRYPOINT ["/javaapp-1.0/bin/javaapp"]
+ADD javaapp /javaapp
+ADD app-lib/${projectName}-1.0.jar /javaapp/lib/$projectName-1.0.jar
+ENTRYPOINT ["/javaapp/bin/javaapp"]
EXPOSE 8080
"""
noExceptionThrown()

MODIFY
src/main/groovy/com/bmuschko/gradle/docker/DockerJavaApplicationPlugin.groovy
src/main/groovy/com/bmuschko/gradle/docker/DockerJavaApplicationPlugin.groovy
@@ -20,10 +20,11 @@ import com.bmuschko.gradle.docker.tasks.image.DockerPushImage
import com.bmuschko.gradle.docker.tasks.image.Dockerfile
import org.gradle.api.Plugin
import org.gradle.api.Project
+import org.gradle.api.distribution.plugins.DistributionPlugin
import org.gradle.api.plugins.ApplicationPlugin
-import org.gradle.api.tasks.Copy
-import org.gradle.api.tasks.bundling.Tar
-import org.gradle.util.ConfigureUtil
+import org.gradle.api.plugins.JavaPlugin
+import org.gradle.api.tasks.Sync
+import org.gradle.jvm.tasks.Jar
/**
* Opinionated Gradle plugin for creating and pushing a Docker image for a Java application.
@@ -43,12 +44,13 @@ class DockerJavaApplicationPlugin implements Plugin<Project> {
DockerJavaApplication dockerJavaApplication = configureExtension(dockerExtension)
project.plugins.withType(ApplicationPlugin) {
- Tar tarTask = project.tasks.getByName(ApplicationPlugin.TASK_DIST_TAR_NAME)
+ Sync installTask = project.tasks.getByName(DistributionPlugin.TASK_INSTALL_NAME)
+ Jar jarTask = project.tasks.getByName(JavaPlugin.JAR_TASK_NAME)
dockerJavaApplication.exec {
- entryPoint { determineEntryPoint(project, tarTask) }
+ entryPoint { determineEntryPoint(project, installTask) }
}
- Dockerfile createDockerfileTask = createDockerfileTask(project, tarTask, dockerJavaApplication)
- Copy copyTarTask = createDistCopyResourcesTask(project, tarTask, createDockerfileTask)
+ Dockerfile createDockerfileTask = createDockerfileTask(project, installTask, jarTask, dockerJavaApplication)
+ Sync copyTarTask = createDistCopyResourcesTask(project, installTask, jarTask, createDockerfileTask)
createDockerfileTask.dependsOn copyTarTask
DockerBuildImage dockerBuildImageTask = createBuildImageTask(project, createDockerfileTask, dockerJavaApplication)
createPushImageTask(project, dockerBuildImageTask)
@@ -66,31 +68,35 @@ class DockerJavaApplicationPlugin implements Plugin<Project> {
return dockerExtension.javaApplication
}
- private Dockerfile createDockerfileTask(Project project, Tar tarTask, DockerJavaApplication dockerJavaApplication) {
+ private Dockerfile createDockerfileTask(Project project, Sync installTask, Jar jarTask, DockerJavaApplication dockerJavaApplication) {
project.task(DOCKERFILE_TASK_NAME, type: Dockerfile) {
description = 'Creates the Docker image for the Java application.'
- dependsOn tarTask
+ dependsOn jarTask
from { dockerJavaApplication.baseImage }
maintainer { dockerJavaApplication.maintainer }
- addFile({ tarTask.archivePath.name }, { '/' })
+ addFile({ installTask.destinationDir.name }, { "/${installTask.destinationDir.name}" })
+ addFile({ "app-lib/${jarTask.archiveName}" }, { "/${installTask.destinationDir.name}/lib/${jarTask.archiveName}" })
instructions << dockerJavaApplication.exec
exposePort { dockerJavaApplication.getPorts() }
- }
+ } as Dockerfile
}
- private Copy createDistCopyResourcesTask(Project project, Tar tarTask, Dockerfile createDockerfileTask) {
- project.task(COPY_DIST_RESOURCES_TASK_NAME, type: Copy) {
+ private Sync createDistCopyResourcesTask(Project project, Sync installTask, Jar jarTask, Dockerfile createDockerfileTask) {
+ project.task(COPY_DIST_RESOURCES_TASK_NAME, type: Sync) {
group = DockerRemoteApiPlugin.DEFAULT_TASK_GROUP
description = "Copies the distribution resources to a temporary directory for image creation."
- dependsOn tarTask
- from { tarTask.archivePath }
+ dependsOn installTask
+ from { installTask.destinationDir.parentFile }
into { createDockerfileTask.destFile.parentFile }
+ exclude "**/lib/${jarTask.archiveName}"
+ into("app-lib") {
+ from jarTask
}
+ } as Sync
}
- private String determineEntryPoint(Project project, Tar tarTask) {
- String installDir = tarTask.archiveName - ".${tarTask.extension}"
- "/$installDir/bin/$project.applicationName".toString()
+ private String determineEntryPoint(Project project, Sync installTask) {
+ "/${installTask.destinationDir.name}/bin/${project.applicationName}".toString()
}
private DockerBuildImage createBuildImageTask(Project project, Dockerfile createDockerfileTask, DockerJavaApplication dockerJavaApplication) {
@@ -99,7 +105,7 @@ class DockerJavaApplicationPlugin implements Plugin<Project> {
dependsOn createDockerfileTask
conventionMapping.inputDir = { createDockerfileTask.destFile.parentFile }
conventionMapping.tag = { determineImageTag(project, dockerJavaApplication) }
- }
+ } as DockerBuildImage
}
private String determineImageTag(Project project, DockerJavaApplication dockerJavaApplication) {

Allow Docker to cache app libraries dir in DockerJavaApplication plugin (#459)