-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automated Java code formatting and verification #2694
Automated Java code formatting and verification #2694
Conversation
Define a common format for all georchestra modules using an eclipse formatter definition file, which will be passed as an argument to the net.revelc.code.formatter:formatter-maven-plugin. The format follows standard Java formatting conventions with 120 characters line wrap limit and 4 spaces for indentation.
Add the net.revelc.code.formatter:formatter-maven-plugin maven plugin to the root pom's pluginManagement section and configure it with to be inherited by child projects that add it to their <plugins> section. The plugin is configured to format only java source code files using the .mvn/formatter.xml eclipse formatter definition. This formatter can be directly imported from eclipse IDE and from IntelliJ IDEA's "Eclipse Code Formatter" plugin (https://plugins.jetbrains.com/plugin/6546-eclipse-code-formatter). When compiling a project that includes the formatter-maven-plugin, the default action is to format the source code to adhere to the defined code style. The other option is to instruct the plugin to check for correct formatting and fail the build. This behavior is controlled by the fmt.action property, like in: mvn compile -Dfmt.action=format or -Dfmt.action=validate. On a CI build server (Jenkins/Travis-CI), the -Dfmt.action=validate should be used, to ensure a committer did his/her due diligence before pushing to the upstream git repository. Finally, the plugin can be disabled with the -Dfmt.skip=true maven property.
Add the formatter-maven-plugin to the build/plugins section on the following projects: analytics, atlas, commons, console, extractorapp, header, mapfishapp, ogc-server-statistics, security-proxy. The plugin configuration is inherited from the root pom.
Tell Travis-ci to validate the code formatting during the install phase, and to ignore the formatter-maven-plugin during the script phase. This ensures the build fails if ill-formatted code was committed at an early stage, and avoids double running for the plugin.
This is the first commit after configuring the formatter-maven-plugin, with formatting applied. From this point on, all commits pushed to github must have well formatted content, or the build will fail on travis-ci. There's nothing special to do though, other than having compiled/built the project before committing. The maven plugin will re-format the code when building, before the compile phase.
Not a big fan of big PRs reformatting code, as it often invalidates/creates useless conflicts with all pending PRs. Oh well.. has to be done someday, i guess. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 (maybe squash when merge)
I think it will be better to keep the reformatting with no changes in a separate commit (for history and understanding purposes). |
Thanks Gabriel, awesome. |
Apply formatter-maven-plugin formatting to all projects.
Add the
net.revelc.code.formatter:formatter-maven-plugin
and configure it to be inherited by georchestra projects (no geoserver/geonetwork submodules).The plugin is configured to format only java source code files using the
.mvn/formatter.xml
eclipse format definition.This file can be directly imported from eclipse IDE and from IntelliJ IDEA's Eclipse Code Formatter plugin.
When compiling a project that includes the formatter-maven-plugin, the default action is to format the source code to adhere to the defined code style.
The other option is to instruct the plugin to check for correct formatting and fail the build.
This behavior is controlled by the fmt.action property, like in
mvn compile -Dfmt.action=format
or-Dfmt.action=validate
. By default a project build will run theformat
action, so nothing special needs to be done in order to ensure the proper format of the code other than making sure to run the build before committing/pushing to github.On a CI build server (Jenkins/Travis-CI), the
-Dfmt.action=validate
should be used, to ensure a committer did his/her due diligence before pushing to the upstream git repository, which is already the case for Travis-CI.Finally, the plugin can be disabled with the
-Dfmt.skip=true
maven property.