Introduction to Google Java Format
google-java-format
is a powerful tool designed to reformat Java source code, making it adhere strictly to the Google Java Style Guide. This tool is particularly helpful for developers who want to ensure their code remains consistent with established formatting standards, thus enhancing readability and maintainability.
How to Use Google Java Format
Command-Line Usage
The google-java-format
tool can be easily used from the command line. Developers can download the formatter from its releases page. Execution involves running the downloaded JAR file with Java:
java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...]
This command reformats Java files by either modifying the entire file or selectively formatting specific lines or offsets. By default, the results are outputted to the standard-out, but users have the option to overwrite the original files using the --replace
option.
For those interested in only reformatting code that has changed, there's a script called google-java-format-diff.py
that can be used.
Important Note: The formatter follows a fixed algorithm with no options for customization, to ensure unified code formatting.
Integration with IntelliJ and Android Studio
The formatter integrates seamlessly with JetBrains IDEs like IntelliJ IDEA and Android Studio through a dedicated plugin available on the plugin repository. Users can install this by navigating to the Plugins
category within the IDE settings. Once installed, the plugin needs to be enabled through the Project settings, which will then replace normal code reformatting and optimization actions.
JRE Configuration for IntelliJ
To utilize the plugin in IntelliJ, some additional configurations are required in the IDE's Java runtime environment. This involves adding several JVM options to ensure the internal classes essential for the formatter are accessible.
Eclipse Plugin
For Eclipse users, a plugin is available for download from the formatter's releases page. Activation involves placing it into Eclipse's drop-ins folder. This integrates google-java-format
with Eclipse, allowing users to select it as the active formatter under Code Style preferences.
Third-party Integrations
google-java-format
extends its usability through various third-party tools and plugins for different development environments:
- Visual Studio Code: A specific extension google-java-format-for-vs-code is available.
- Gradle and Maven: Several plugins, including
spotless
and others, support integrating the formatter into build processes. - GitHub Actions: The
googlejavaformat-action
helps automate formatting each time code is pushed to a repository.
Usage as a Library
Developers generating Java code can use google-java-format
as a library to ensure outputted code is properly formatted. Incorporation into projects can be done through build systems like Maven and Gradle using the following configurations:
Maven Configuration
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>${google-java-format.version}</version>
</dependency>
Gradle Configuration
dependencies {
implementation 'com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion'
}
The Formatter's methods, such as formatSource
, can then be used programmatically to format code.
Building and Contributing
For those interested in building from source or contributing to the project, instructions and guidelines are provided.
Licensing
The project is open-source, licensed under the Apache License 2.0, which allows for wide usage and modification subject to the terms of the license.
google-java-format
offers a robust solution for maintaining code consistency across Java projects, making it a valuable tool for any developer adhering to Google's Java formatting standards.