3

I would like to build a Spring Boot app using the maven wrapper provided by spring.io.starter inside a docker container. My Dockerfile is:

FROM openjdk:8-jdk-alpine
# install bash --> commented because I just need /bin/sh which is already provided by the base image
#RUN apk add --no-cache bash
ENV APP_DIR /app
ENV APP app.jar
WORKDIR ${APP_DIR}
COPY . ${APP_DIR}
# ===> HERE THE PROBLEM
RUN /bin/sh -c "./mvnw clean install"
ENTRYPOINT ["java","-jar","chicowa-app.jar"]
EXPOSE 8080

I have this error:

/bin/sh: ./mvnw: not found

After making some researches I still don't find a solution. My docker version Docker version 18.06.1-ce, build e68fc7a on Windows10 pro

Thanks a lot for your help.

EDIT

A solution would be to install maven directly with a RUN apk add --no-cache maven but I would like to minimize as possible the image' size.

Community
  • 1
  • 1
akuma8
  • 4,160
  • 5
  • 46
  • 82

9 Answers9

6

We were experimenting this issue only on Windows 10 and were able to solve this issue on just applying dos2unix

dos2unix mvnw
Juanan
  • 61
  • 1
  • 2
4

I have the same issue when build Dockerfile in Window 10. This because mvnw file contains CRLF (\r\n) at the end of line. I open it with Notepad++ and use Find \r\n and Replace with \n (with Extended (\n, \r, ...) checked). You can use dos2unix tool to convert as @Juanan said above.

After that, the build run with no problem.

Minh Toan
  • 91
  • 1
  • 3
1

Unless you are running the script in the exact location that the file mvnw is located, it won’t work.

If your PATH is set correct, and Maven was installed in a suitable location, simply removing the “./“ in front of the command will suffice.

RUN /bin/sh -c “mvnw clean install”

If mvnw is NOT in your PATH, you can specify the full path in your script (but recognize that Maven is likely to call other things that would want the same PATH changes).

Don Simon
  • 144
  • 5
  • I removed the `./` but same result. I don't want to install Maven so what do you mean by "If your PATH is set correct, and Maven was installed in a suitable location..." – akuma8 Oct 11 '18 at 16:39
  • If you haven’t installed Maven, then mvnw does not exist, and there is no solution to your problem, that doesn’t include installing maven. – Don Simon Oct 11 '18 at 17:00
  • I think you didn't understand my question. Take a look here about the `maven wrapper` https://www.baeldung.com/maven-wrapper – akuma8 Oct 11 '18 at 17:07
  • I did misunderstand, that said, I’m not sure why that page is claiming you won’t need maven installed. The installation instructions for maven wrapper specifically use Maven. You can’t add it to the project and get the required files downloaded, without it. cd your_project mvn -N io.takari:maven:wrapper Good luck. – Don Simon Oct 11 '18 at 17:14
  • I did the test on my PC, I removed MAVEN_HOME from the PATH, run the `./mvnw clean install` in my project and it worked. So the author of that page is absolutely right. – akuma8 Oct 11 '18 at 17:22
  • Exactly. On Your PC that had Maven installed, you set up a project with Maven Wrapper. If you want to setup a new project, on a new computer, you have to install the maven wrapper in that project too. The command to do that is a maven command. – Don Simon Oct 11 '18 at 17:30
  • If we need to have maven installed before running the wrapper so the wrapper would be absolutely useless. It does not make sense I think. – akuma8 Oct 12 '18 at 08:47
  • You can use maven to add the wrapper to a project, then remove maven from your machine, and then use the maven-wrapper. It could have been explained better. – Don Simon Oct 12 '18 at 17:02
1

if you have already maven configured then you may run the following:-

mvn clean install

It will build the docker image seamlessly.

cammando
  • 596
  • 7
  • 20
1

use

RUN chmod +x mvnw 

before using

./mvnw clean install 

it'll make your mvnw file executable

0

If you are using windows git may change the line ending of the mvnw file to CRLF. If you open the file with any text editor and save it to LF it will probably work.

0

The problem can also be fixed without bothered with line endings or path variables even on Windows10.

Just include Maven to your Dockerfile and run the maven commands via this:

Here is an example Dockerfile:

FROM maven:3.8.1-openjdk-17-slim AS builder   # <-- Include Maven
WORKDIR /app
COPY pom.xml ./
COPY src ./src

RUN mvn clean install

# Second stage: Minimal runtime environment
FROM eclipse-temurin:17-jre-jammy
WORKDIR /app

# copy jar from the first stage
COPY --from=builder /app/target/*.jar /app/app.jar

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

After using this approach, I solved this problem. Hope this helps...

Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
-1

This is working solution as I don't find anywhere this solution:

mvnw -version error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

Root Cause: maven public certificate is missing in java trust store

Solutions:

  1. download - repo.maven.apache.org leaf certificate from https://repo.maven.apache.org and save it as repo_maven.cer file in base64 format
  2. Install the repo_maven.cer into to cacerts (truststore) keytool -import -keystore "...\openjdk-8u41\jre\lib\security\cacerts" -file "repo_maven.cer" -alias maven

Output: mvnw -version Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)

-1

I had the same problem, I just create another Spring project with Initializer and copy / paste mvnw and mvnw.cmd in the "old" project: after that it was found by Docker (version bump from 3.8.6 to 3.8.7 don't know if it was the problem).

Just edit .mvn/maven-wrapper.prop to be in part the with new version after that also.

user16217248
  • 3,119
  • 19
  • 19
  • 37
user10942209
  • 11
  • 1
  • 3