Gradle Build – Copying custom file and replace with tockenized string
Objective
The blog is to copy a file from the project path to build path after the project build completes
Example
Use-case : In my case, I have a Dockerfile in the project root directory. I wanted to copy the file to the build directory once project build completes.
Project structure :
smartechie-pro | ----- src | ----- build.gradle | ----- Dockerfile | ----- Jnkinsfile | ----- Makefile
After project build (Expected) :
smartechie-pro | ----- build | >----- classes | >----- libs | >----- resources | >----- tmp | ----- Dockerfile | >----- src | ----- build.gradle | ----- Dockerfile | ----- Jnkinsfile | ----- Makefile
My Docker file content as follows :
FROM openjdk:alpine
MAINTAINER Sudhir Ranjan Pradhan sudhir@smartechie.com
RUN apk –no-cache add ca-certificates
RUN mkdir /@binaryName@
WORKDIR /@binaryName@/
COPY –from=builder /@binaryName@/build .
EXPOSE 16666
CMD java -jar /@binaryName@/libs/@binaryName@-@binaryVersion@.jar
Expected Dockerfile content as follows :
FROM openjdk:alpine MAINTAINER Sudhir Ranjan Pradhan sudhir@smartechie.com RUN apk --no-cache add ca-certificates RUN mkdir /smartechie-pro WORKDIR /smartechie-pro COPY --from=builder /smartechie-pro/build . EXPOSE 16666 CMD java -jar /smartechie-pro/libs/smartechie-pro-1.0.0.jar
STEPS to implement :
- Open the build.gradle and add the following block of code at the end of the file
task ('filterCopyDockerfile', type: Copy) {
println project.buildDir
println project.rootDir
from rootDir
into buildDir
include 'Dockerfile'
filter(ReplaceTokens, tokens: [binaryName: 'revrecon-factory-api', binaryVersion: '1.0.0'])
}
assemble.dependsOn filterCopyDockerfile
- Verify if the above implementation is working
./gradlew build