Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

Gradle Build – Copying custom file and replace with tockenized string

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
You can enroll now !We are giving 30% discount on our Internship Program

Don’t miss the chance to participate in the upcoming Internship Program which will be done using Microsoft Dot Net Web Development Full Stack Technology. The new batch will be starting from May 20, 2024.  We will have most experienced trainers for you to successfully complete the internship with live project experience.

Why to choose Our Internship Program?

Industry-Relevant Projects
Tailored Assignments: We offer projects that align with your academic background and career aspirations.
Real-World Challenges: Tackle industry-specific problems and contribute to meaningful projects that make a difference.

Professional Mentorship
Guidance from Experts: Benefit from one-on-one mentorship from seasoned professionals in your field.
Career Development Workshops: Participate in workshops that focus on resume building, interview skills, and career planning.

Networking Opportunities
Connect with Industry Leaders: Build relationships with professionals and expand your professional network.
Peer Interaction: Collaborate with fellow interns and exchange ideas, fostering a supportive and collaborative environment.

Skill Enhancement
Hands-On Experience: Gain practical skills and learn new technologies through project-based learning.
Soft Skills Development: Enhance communication, teamwork, and problem-solving skills essential for career success.

Free Demo Class Available