jhipster / prettier-java

Prettier Java Plugin
http://www.jhipster.tech/prettier-java/
Apache License 2.0
1.06k stars 103 forks source link

Auto formatting on save using maven-plugin-java? #674

Closed jaoxford closed 1 week ago

jaoxford commented 1 week ago

Just curious if there's a way of formatting this on save? I am using intelliJ. I have tried to use the file-watcher plugin, with the following config for my IDE:

Screenshot 2024-06-20 at 15 11 19

When I save my file I get the following error:

//.m2/repository/com/hubspot/maven/plugins/prettier-maven-plugin/0.16/prettier-maven-plugin-0.16-prettier-java-0.7.0/prettier-java/node_modules/prettier/bin-prettier.js --write src/main/java/MyFile.java
[error] No matching files. Patterns tried: src/main/javaMyFile.java !**/node_modules/** !./node_modules/** !**/.{git,svn,hg}/** !./.{git,svn,hg}/**

Process finished with exit code 2

Here's the output of print-args:

printWidth=90
tabWidth=4
useTabs=false
noConfig=true
noEditorconfig=true
inputGlob=src/main/java/**/*.java
inputGlob=src/test/java/**/*.java
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.273 s
[INFO] Finished at: 2024-06-20T15:15:03+01:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

And if I manually trigger prettier:write that is also fine. Ideally this would be something that runs on every save, so I don't have to think about it!

Any idea or help would be appreciated!

murdos commented 1 week ago

I'm not using maven-plugin-java, but it works fine for me using Actions on Save: image

Don't forget to add the java extension to files that should be prettified: image

Maybe you can try the manual Prettier configuration above to fill the exact prettier location?

jaoxford commented 1 week ago

I can run the file watcher on save, like so:

Screenshot 2024-06-21 at 12 43 42

However, I still get the same prettier error I listed above.

jaoxford commented 1 week ago

Got it working, I had to force it to write to Java files: --write $FilePathRelativeToProjectRoot$ "**/*.java