Formatting violations found in the java files when docker run

Hi, I am new to docker

i follow the instructions on the following page and could build a docker image
Build your Java image | Docker Documentation

but when i try to run using the command “docker run java-docker” on the following page:
Run your image as a container | Docker Documentation

the following messages appear on my command prompt:

[INFO]
[INFO] — spring-javaformat-maven-plugin:0.0.27:validate (default) @ spring-petclinic —
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.034 s
[INFO] Finished at: 2022-01-07T04:36:28Z
[ERROR] Failed to execute goal io.spring.javaformat:spring-javaformat-maven-plugin:0.0.27:validate (default) on project spring-petclinic: Formatting violations found in the following files:
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/system/CrashController.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/system/CacheConfiguration.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/PetController.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/OwnerRepository.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/Visit.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/PetValidator.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/VisitController.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/OwnerController.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/PetType.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/Pet.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/PetTypeFormatter.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/owner/Owner.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/model/Person.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/model/NamedEntity.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/model/BaseEntity.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/model/package-info.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/PetClinicApplication.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/vet/VetRepository.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/vet/Vets.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/vet/VetController.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/vet/Specialty.java
[ERROR] * /app/src/main/java/org/springframework/samples/petclinic/vet/Vet.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/PetclinicIntegrationTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/system/CrashControllerTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/service/ClinicServiceTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/service/EntityUtils.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/owner/PetControllerTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/owner/PetTypeFormatterTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/owner/VisitControllerTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/owner/OwnerControllerTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/model/ValidatorTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/vet/VetTests.java
[ERROR] * /app/src/test/java/org/springframework/samples/petclinic/vet/VetControllerTests.java
[ERROR]
[ERROR] Run spring-javaformat:apply to fix.
[ERROR] → [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]

May you help? thx

Any chance this is related to using Windows line endings on the machine from which you copied the source code into the Linux image?

To test whether this is a docker problem or not, you can use a linux vm or WSL2 distribution, preferably with the same os as the base image, install git+java+maven, checkout the sources of the petclinic and try to run the command there. I am sure you will get the same errors.

Java compilation shouldn’t care how the lines breaks are on the java sources, but it’s possible the spring-javaformat-maven-plugin does.

Probably the plugin works as designed and the code simply does not follow the validation rules and just needs to be reformed, like it says here: [ERROR] Run spring-javaformat:apply to fix.

I tried it on Linux. First without Docker, then with Docker. There was no error. Then I changed /app/src/main/java/org/springframework/samples/petclinic/system/WelcomeController.java

I replaced tabs with spaces, rebuilt the image, run the container and got formatting violation.
The same result when I use tabs but add one windows line ending at the end of the file.

Git supports changing line endings so @avbentem is probably right.

I missed the point that git in a vm/wsl2 could auto correct the Windows CRLF line ending to Unix LF line ending while pulling. So the real test scenario would be to checkout the sources on Windows and use WSL2 to execute the maven goal inside the checked out Windows sources folder (/mnt/c/wherever/checkedout/in/windows).

It’s the job a format validator to be nosy about every bit and piece of the formatting. So the error appears to be a functional error code based on the validation result, rather then a technical error based on technical issues while running the maven plugin.

Thx a lot everyone. I replaced tab to space and \r\n to \n for all the java files, and then i build the image again and then run, but the problem still persists.

So should I change my OS to Linux? or any other thing i could do in order to use Docker in Windows?

I just follow the normal procedures instructed in the website like everyone else using Windows as their OS. I just wonder why only me encounter such problem.

Thx a lot!

Are you sure you replaced all of the invalid (tabs, windows line endings) characters? How did you do that?

Sometimes the IDE can change the line endings when you save the files. Maybe it is only at the end of the file.

If you can at least try on a Linux machine, I would definitely do that. If you can, use the same IDE with same settings if it is multiplatform. You should be able to build on Windows if the sourcecode is correct. I know you tried to fix it, but you may missed something.

If your git client changes the line endings for example, you can try to download the sourcecode without git from a webbrowser as a ZIP archive. (Click on Code, then “Download ZIP”)

The other solution could be to use multi-stage build where you use a different build stage to download the sourcecode and copy the code into an other stage. This is the reommended way if you don’t want to change the code. Of course you would need to make sure you always use the same version but that is perhaps another question for later.

While you are trying these solutions I wil try on it on Windows.

Update:

I tried it and it works if the core.autocrlf is false in the git config.This is how I installed Git For Windows:
git-for-windows-autocrlf
You can change it after the installation in the git config

Thx rimelek, i guess Git client’s auto conversion is the problem. I tried to download the zip instead and there is no such exception. I will try to change the config of git client afterwards.

I replaced the tab and end of line(\r\n) by space and (\n) respectively using Notepad++ and it didn’t work. I will figure out why later. thx!