revelc/formatter-maven-plugin

How can I format a single java file which I commit but not whole path

Closed this issue · 8 comments

Is your feature request related to a problem? Please describe.
How can I format a single java file which I commit but not whole path?

Describe the solution you'd like
Format a single java file which I commit

Describe alternatives you've considered
NA

Additional context
As accroding to :
#311
I used like this:
in commit-commit git hook I write

#handle format begin
STAGE_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.java')
if test ${#STAGE_FILES} -gt 0
then
    echo 'format java begin...'
    for FILE in $STAGE_FILES
    do
        echo 'formatted file:'$FILE
        mvn -Dformatter.includes=$FILE formatter:format
        #mvn formatter:format
        if [ $? -ne 0 ]; then
            echo 'format:'$FILE 'failed!'
            exit -1
        else
            echo 'format:'$FILE 'ok!'
            git add $FILE
        fi
    done
    echo 'format java end...'
else
    echo 'OK, no java files need to be formatted.'
fi
#handle format end

and in pom.xml I write

            <plugin>
                <groupId>net.revelc.code.formatter</groupId>
                <artifactId>formatter-maven-plugin</artifactId>
                <version>2.18.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>format</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <configFile>${project.basedir}/eclipse-codestyle.xml</configFile>
                    <includes>${formatter.includes}</includes>
                </configuration>
            </plugin>

with mvn -Dformatter.includes=$FILE formatter:format, It will not format file.
with mvn formatter:format, It will not format files more than a single java file which I commit T_T
Thanks again.

I think you actually need to have your config file look like this to include a single file:

 <includes>
   <include>${formatter.includes}</include>
 </includes>

Also, you don't need to specify the <executions> piece to configure it for formatting from the command-line. <executions> is for configuring executions of the plugin automatically during a mvn build.

with

${formatter.includes}

in pom.xml
And then run mvn formatter:format -Dformatter.includes=src/main/java/com/GitTest.java , it will not format the GitTest.java. @ctubbsii

@ctubbsii as your mentioned, I used <include>${formatter.includes}</include> instead of <includes>${formatter.includes}</includes>, the result keep the same as before

I tried this with a wildcard pattern, mvn formatter:format -Dformatter.includes='**/MyFileName.java' and that seems to have worked. However, I get the same behavior as you do for specifying the exact path to a file. It's very strange. I'm not sure what the problem is.

Ah, I figured it out. Apparently, the base directory the formatter is processing files relative to is src/main/java/ or src/test/java/ by default.

If you need a different directory, you can set something like:

            <directories>
              <directory>${project.basedir}</directory>
            </directories>
            <includes>
              <include>${formatter.includes}</include>
            </includes>

Information has been provided. Closing this.

@ctubbsii Thanks a lot, it works. with pom.xml as:

  <configuration>
    <configFile>${project.basedir}/eclipse-codestyle.xml</configFile>
    <directories>
      <directory>${project.basedir}</directory>
    </directories>
    <includes>
      <include>${formatter.includes}</include>
    </includes>
  </configuration>

and then run mvn formatter:format -Dformatter.includes=src/main/java/com/GitTest.java, it will format only GitTest.java.

Perfect! Thanks again.

@zhujiajun917 No problem. Also, I edited your comment to format the XML correctly. In the future, GitHub users will appreciate you learning to use proper GitHub-flavored Markdown syntax for code blocks: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks