javadockergradledockerfilegradlew

Build jar with Docker using gradle->gradlew


I'm new with Docker, I'm study and learning....So.....sorry for the bad code. I'm trying to generate the jar when I'm building my docker image; I would like to use a basic image of openjdk and use gradlew (gradle-wrapper) to do the clean and build but I'm having problems with gradlew.

Currently I have this code:

FROM openjdk:17.0.2
LABEL authors="ivanh"
WORKDIR /app
COPY ./ .
RUN ./gradlew clean build
ENTRYPOINT ["ls"]
EXPOSE 8001

when I run docker build -t citas . I have this problem

------
 > [4/4] RUN ./gradlew clean build:
0.390
0.390 xargs is not available
0.390
------
Dockerfile:5
--------------------
   3 |     WORKDIR /app
   4 |     COPY ./ .
   5 | >>> RUN ./gradlew clean build
   6 |     ENTRYPOINT ["ls"]
   7 |     EXPOSE 8001
--------------------
ERROR: failed to solve: process "/bin/sh -c ./gradlew clean build" did not complete successfully: exit code: 1

I'm search about that and ask to the IA. I found that one solution is change openjdk image and another solution is add this command RUN microdnf install findutils but with the last solutions the time is very slow while download and install the util.

I'm confused because I think the purpose of gradle-wrapper is run commands without have gradle installed. Can someone clarify this for me ? If the only solution is change the openjdk image, can you recomend me one?


Solution

  • Try using the gradle:jdk17 base image, which is based on OpenJDK 17.0.10.

    FROM gradle:jdk17
    
    WORKDIR /app
    
    COPY . .
    
    RUN ./gradlew clean build
    
    ENTRYPOINT [ "ls" ]
    

    Assuming a test project layout like this:

    ├── app
    │   ├── build.gradle.kts
    │   └── src
    │       ├── main
    │       │   ├── java
    │       │   │   └── org
    │       │   │       └── example
    │       │   │           └── App.java
    │       │   └── resources
    │       └── test
    │           ├── java
    │           │   └── org
    │           │       └── example
    │           │           └── AppTest.java
    │           └── resources
    ├── Dockerfile
    ├── gradle
    │   ├── libs.versions.toml
    │   └── wrapper
    │       ├── gradle-wrapper.jar
    │       └── gradle-wrapper.properties
    ├── gradlew
    ├── gradlew.bat
    └── settings.gradle.kts
    

    enter image description here