Gradle task for Java playground

I'm still new to Docker and Gradle, but I'm trying to customize the Gradle assembly, which creates a Docker image.

I just finished configuring the Dockerfile , which locally deploys and launches the jar as expected. I have this in my build.gradle :

 buildscript { repositories { mavenCentral() } dependencies { classpath 'se.transmode.gradle:gradle-docker:1.2' } } plugins { id 'com.github.johnrengelman.shadow' version '1.2.3' } apply plugin: 'docker' jar { manifest { attributes 'Main-Class': 'com.myapp.Main' } } task buildDocker(type: Docker, dependsOn: shadowJar) { push = false applicationName = jar.baseName tagVersion = 'latest' dockerfile = file('src/main/docker/Dockerfile') copy { from shadowJar into stageDir } } 

I run ./gradlew build buildDocker to create an image. I am still satisfied with this.

I usually create a throwaway class (e.g. Playground.java ) using the main method, which I can start and ignore. I usually run this in the IDE, but now I would like to be able to connect to other Docker containers, which, as I know, will work.

I know that I can try to change the sourceSets I use by excluding com.myapp.Main , but I thought there might be a more elegant solution similar to this:

 task buildDockerPlayground(type: Docker, dependsOn: shadowJar) { main = 'com.myapp.Playground' push = false applicationName = jar.baseName tagVersion = 'latest' dockerfile = file('src/main/docker/Dockerfile') copy { from shadowJar into stageDir } } 

Another approach might be another task that I use to replace build when calling ./gradlew build buildDocker , for example. ./gradlew playground buildDocker . Is it more practical?

+5
source share
1 answer

I would suggest replacing the hard-coded main class with a gradle project property.

 jar { manifest { attributes 'Main-Class': main } } 

Set this default property in the gradle.properties file.

 main=com.myapp.Main 

Finally, when you need to create a com.myapp.Playground container that uses the jar com.myapp.Playground you can call gradle with:

 ./gradlew buildDocker -Pmain=com.myapp.Playground 

Edit: to achieve the same goal in the task

 project.ext.main = 'com.myapp.Main' task play(){ project.main = 'com.myapp.Playground' finalizedBy buildDocker } jar { manifest { attributes 'Main-Class': project.main } } 
+1
source

All Articles