google-java-format

repository·master·Indexed 27 days ago

https://github.com/google/google-java-format

A tool that reformats Java source code to strictly adhere to the Google Java Style guide. It provides a non-configurable formatting standard and can be used via a command-line interface, as a library using the com.google.googlejavaformat.java.Formatter class, or through plugins for Eclipse, IntelliJ IDEA, and Android Studio.

Tokens
2.1K
Snippets
6
Records
15
Agent score
90%

What's inside google-java-format

  1. Build the Eclipse Plugin against a local core snapshot

    master

    By default, the build pulls google-java-format core artifacts from a Maven repository. To build against a local (snapshot) version of the core that is not in a Maven repository:

    1. Manually place the desired core version JAR into the eclipse_plugin/lib/ directory.
    2. Run the standard build command (mvn clean package).
  2. Configure IntelliJ JRE for google-java-format plugin

    master

    The plugin requires access to internal JDK classes. To configure this, go to Help > Edit Custom VM Options... and append the following lines, then restart the IDE:

    --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
  3. Run google-java-format from the command line

    master

    You can run the formatter using the provided JAR file. The java binary used must be from a JDK (not a JRE) and must be a version equal to or newer than the Java language version of the files being formatted (currently requires Java 21). Alternatively, you can use GraalVM-based native binaries.

    Common options include:

    • --replace: Alter files in-place (default is to pass through to standard-out).
    • --lines: Act on limited lines.
    • --offset: Act on specific offsets.
    • --aosp: Use AOSP style.
    • --dry-run: Check for changes without applying them.
    • --set-exit-if-changed: Set exit code if files need formatting.
    • @<filename>: Read options and filenames from a file instead of arguments.

    To reformat only changed lines in a specific patch, use the google-java-format-diff.py script.

    java -jar /path/to/google-java-format-${GJF_VERSION?}-all-deps.jar <options> [files...]
  4. Build the Google Java Format Eclipse Plugin from source

    master

    To build the Eclipse plugin, follow these steps:

    1. Update Metadata: Before building, run the following command to update the bundle version in META-INF/MANIFEST.MF:

      mvn tycho-versions:update-eclipse-metadata
    2. Build the Plugin: Run the following command in the eclipse_plugin directory. This copies dependencies to eclipse_plugin/lib/ and triggers the Tycho build:

      mvn clean package
    3. Locate the Artifact: The built plugin JAR will be located at: eclipse_plugin/target/google-java-format-eclipse-plugin-<version>.jar

    mvn clean package
  5. Install google-java-format for IntelliJ and Android Studio

    master
    1. Install the google-java-format plugin from the JetBrains Marketplace via IDE Settings > Plugins.
    2. The plugin is disabled by default. To enable it, go to Project Settings > google-java-format Settings and check Enable google-java-format.
    3. To enable it for all new projects, configure it under Other Settings > google-java-format Settings in the default settings for new projects.

    Note: When enabled, it replaces the standard Reformat Code and Optimize Imports actions.

  6. Add google-java-format dependency

    master

    Add the following to your build configuration to use the formatter as a library.

    <!-- Maven -->
    <dependency>
      <groupId>com.google.googlejavaformat</groupId>
      <artifactId>google-java-format</artifactId>
      <version>${google-java-format.version}</version>
    </dependency>
    // Gradle
    dependencies {
      implementation 'com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion'
    }
  7. Install and configure google-java-format for Eclipse

    master
    1. Download the latest plugin from the releases page and drop it into the Eclipse dropins folder.
    2. Select the implementation in Window > Preferences > Java > Code Style > Formatter > Formatter Implementation.
      • google-java-format: 2-space indent.
      • aosp-java-format: 4-space indent.

    JRE Configuration: You must edit your eclipse.ini file and add the following lines after -vmargs to allow access to internal JDK classes, then restart Eclipse:

    --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
  8. Install the Eclipse Plugin to the local Maven repository

    master

    If you want to add the build artifact to your local Maven repository, use the following command.

    Warning: If you use this method, you must use this specific command for every subsequent build with that same version number to avoid using outdated artifacts from your local repository.

    mvn clean install -Dtycho.localArtifacts=ignore
    mvn clean install -Dtycho.localArtifacts=ignore
  9. Configure google-java-format in IntelliJ IDEA

    master

    The google-java-format IntelliJ plugin provides a configuration UI under "google-java-format Settings" to manage how the formatter behaves within your IDE.

    You can control the following settings:

    • Enable google-java-format: A checkbox to enable or disable the formatter for the current project.
    • Code style: A dropdown menu to select the desired formatting style (via UiFormatterStyle).
  10. Use google-java-format as a library

    master

    Include the library in your dependency management. Note that when running on JDK 16 or newer, you must provide the following JVM flags due to JEP 396:

    --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
    --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED

    Use the com.google.googlejavaformat.java.Formatter class to format source code.