How can I solve the problem with maven-formatter-plugin?

Viewed 7471

I have some problem with build project on Windows system. My project uses formatter-maven-plugin with goal "validate". Before push project in GitHub, i format it and push. After, I clone it on my windows computer and try to "mvn clean install", but I get:

 Failed to execute goal net.revelc.code.formatter:formatter-maven-plugin:2.9.0:validate (default) on project application: File 'C:\Users\AUser\Desktop\demo\application\src\main\java\com\myapp\Application.java' has not been previously formatted. Please format file and commit before running validation!

In Linux that problem is not exist. Why is that? Before push in Git, i did format it. Why is problem not exist on linux? Change goal on format i can't.

<plugin>
                <groupId>net.revelc.code.formatter</groupId>
                <artifactId>formatter-maven-plugin</artifactId>
                <version>2.9.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>validate</goal>
                        </goals>
                        <configuration>
                            <configFile>${project.basedir}/../eclipse/eclipse-formatter.xml</configFile>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
3 Answers

Based on the official usage documentation:

Run the plugin with the format goal.

mvn formatter:format

After doing this once, you should be able to run like normal.

I met this question, as a result, Jenkins git fench code, some file end by CRLF, because of line separator is LF of LINUX, so validate is a failure. There are two ways to solve this problem:

you can use .gitattributes set text eol = lf, keep git fench code file end by LF.

another, use formatter-maven-plugin tag lineEnding, set <lineEnding>KEEP</lineEnding>, let line separator depend on file self, Not System.

I prefer the second way, because of format should keep system independence.

I think that you have missed a file when formatting. Based on this issue on the topic, it appears that a better error message was logged which indicates the problematic file. If you upgrade Maven Formatter to the latest stable version, you should see a new error message of the form:

File 'fileName' has not been previously formatted. Please format file and commit before running validation!

Related