I want to use com.hubspot.maven.plugins:prettier-maven-plugin with maven project and integrate Prettier Java with Intellij. Below is the maven plugin configuration:
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>0.22</version>
<configuration>
<prettierJavaVersion>2.6.6</prettierJavaVersion>
<printWidth>110</printWidth>
<tabWidth>2</tabWidth>
<useTabs>false</useTabs>
<ignoreConfigFile>true</ignoreConfigFile>
<ignoreEditorConfig>true</ignoreEditorConfig>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>write</goal>
</goals>
</execution>
</executions>
</plugin>
Code formatting works well in the command line by calling "mvn prettier:write". The problem is integration with the Intellij. I want the code be formatted on every save of the file or by calling standard function Reformat code. All the examples I found on the Internet do not work for me. As far as I understood, to achieve the formatting on save, the plugin File Watcher should be used.
There are like two ways to achieve the formatting - either calling mvn or npx. npx should work faster as a maven, so it should be a preferred way, but looks like Java project should have package.json file with dependencies and .prettierrc.yml file. This does not sound like a very nice solution because I have a Java project, not a node...
However, despite which solution (with mvn or npx) I choose, File Watcher has troubles by recognising a Program. If I give only mvn or npx, it always complains, that the Program is not found, so the absolute path to mvn or npx should be provided:
- mvn -> ~/.sdkman/candidates/maven/current/bin/mvn
- npx -> ~/.nvm/versions/node/v20.11.1/bin/npx
But still, wenn a changes to the file are made and File Watcher is triggered, following errors occur:
- mvn -> /home/user/.sdkman/candidates/maven/current/bin/mvn prettier:write -Dprettier.inputGlobs=src/main/java/com/example/MyFile.java
[ERROR] No plugin found for prefix 'prettier' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/home/user/.m2/repository)
- npx -> ~/.nvm/versions/node/v20.11.1/bin/npx prettier --write /home/user/project/src/main/java/com/example/MyFile.java
/usr/bin/env: ‘node’: No such file or directory
If I call the same command in the path of the project in the command line directly, then everything works with both tools (mvn and npx).
Should I define some global variables, or is it bug in Intellij?