Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Explore Spotless for Java projects #714

Closed
3 tasks done
Tracked by #717
tschaffter opened this issue Sep 18, 2022 · 2 comments · Fixed by #719
Closed
3 tasks done
Tracked by #717

Explore Spotless for Java projects #714

tschaffter opened this issue Sep 18, 2022 · 2 comments · Fixed by #719
Assignees

Comments

@tschaffter
Copy link
Member

tschaffter commented Sep 18, 2022

We have been using Google formatting configuration since the time we added the first Java project. In #712, I updated our formatting setup to use google-java-format.

The author of @nxrocks/nx-spring-boot recommend to use Spotless. We generate our Java projects with this generator and they come configured to use Spotless to format Java files. The default configuration runs google-java-format under the hood.

Most of the features of Spotless are available when using Gradle, which we now use. :)

Tasks:

  • Generate a new Java library with @nxrocks/nx-spring-boot and review the Spotless setup.
  • Review Spotless + Gradle setup (see instructions)
  • Enable integration of Spotless + Gradle with VS Code

Depends on:

Note
The Spotless plugin for Gradle was actually added in #713 to the user service.

@tschaffter
Copy link
Member Author

Early exploration

  • build.gradle

    plugins {
      id 'java'
      id 'com.diffplug.spotless' version '6.8.0'
    }
    
    repositories {
      mavenCentral()
      mavenLocal()
    }
    
    spotless {
      // format 'misc', {
      //   target '*.gradle', '*.md', '.gitignore'
    
      //   trimTrailingWhitespace()
      //   indentWithSpaces(2)
      //   endWithNewline()
      // }
    
      java {
        target 'src/*/java/**/*.java'
    
        importOrder()
        removeUnusedImports()
        googleJavaFormat()
      }
    }
  • The VS Code extension richardwillis.vscode-spotless-gradle should interface with the Spotless + Gradle setup.

    • Recommended configuration in settings.json is:

        "[java]": {
          // "files.trimTrailingWhitespace": false,
          "spotlessGradle.diagnostics.enable": true,
          "spotlessGradle.format.enable": true,
          "editor.defaultFormatter": "richardwillis.vscode-spotless-gradle",
          "editor.codeActionsOnSave": {
            "source.fixAll.spotlessGradle": true
          }
        },

@tschaffter
Copy link
Member Author

tschaffter commented Sep 19, 2022

VS Code extension Spotless Gradle

  • Its activation requires gradlew in the workspace folder.
  • Requires build.gradle in the workspace folder.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant