-
Notifications
You must be signed in to change notification settings - Fork 460
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
Add support for google-java-format #33
Comments
Interesting you raise this issue, because I actually use google-java-format in one my own Gradle projects via Spotless's custom step (instead of, say, google-java-format-gradle-plugin), with something like the following. buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.google.googlejavaformat:google-java-format:1.1'
}
}
spotless {
java {
custom('google-java-format') { String source ->
new com.google.googlejavaformat.java.Formatter().formatSource(source)
}
}
} I'm open to the idea of adding the functionality for a single version of GJF myself. However, I anticipate I'll be very busy soon, and I'm also inexperienced with writing Gradle plugins and I've never written software that had to support multiple versions of something before. So I don't know when I'll have the time to start, or if I could go beyond a single version of GJF. |
Cool! Sounds like this would be a good first project for you. I'm in no hurry, and your manual workaround above is great. I think it's fine to only support a single version - if someone else figures out how to support multiple versions later, we can just add Btw, you can speed this up a huge amount using buildscript {
repositories {
jcenter()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
dependencies {
classpath 'com.google.googlejavaformat:google-java-format:1.2-SNAPSHOT'
}
}
plugins {
id "com.diffplug.gradle.spotless" version "2.1.0"
}
spotless {
java {
customLazyGroovy('google-java-format') {
com.google.googlejavaformat.java.Formatter formatter = new com.google.googlejavaformat.java.Formatter()
return { source -> formatter.formatSource(source) }
}
}
} |
Released in 2.2.0. Sorry @jbduncan for swiping it before you had a chance, but looking at google-java-format's lack of gradle instructions made me want to get this to their users ASAP. |
That's fine! Thanks for putting it in so quickly! (I'm impressed by the speed at which you managed to do so, so kudos to you.) Also, thanks for sharing that tip regarding |
Updating to 2.2.0 as well... 👍 |
Spotless has recently removed Before: spotless {
java {
customLazyGroovy('google-java-format') {
com.google.googlejavaformat.java.JavaFormatterOptions options = new com.google.googlejavaformat.java.JavaFormatterOptions.Builder()
.style(com.google.googlejavaformat.java.JavaFormatterOptions.Style.AOSP)
.formatJavadoc(false)
.build()
com.google.googlejavaformat.java.Formatter formatter = new com.google.googlejavaformat.java.Formatter(options)
return { source -> formatter.formatSource(source) }
}
}
} After: spotless {
java {
custom 'google-java-format', { source ->
com.google.googlejavaformat.java.JavaFormatterOptions options = new com.google.googlejavaformat.java.JavaFormatterOptions.Builder()
.style(com.google.googlejavaformat.java.JavaFormatterOptions.Style.AOSP)
.formatJavadoc(false)
.build()
com.google.googlejavaformat.java.Formatter formatter = new com.google.googlejavaformat.java.Formatter(options)
return formatter.formatSource(source)
}
}
} |
@TimvdLippe Any reason in particular you're not just using this? https://github.com/diffplug/spotless/tree/main/plugin-gradle#google-java-format |
@nedtwigg Yes, |
Roger. Happy to take a PR to change that. Some useful snippets: spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Lines 158 to 160 in 93c80ac
Doing #524 for google-java-format would be a good first-step towards making it easier. |
The google java format is popular, and until 2014 it could be enforced using the eclipse code formatter.
Now they have a separate jar called google-java-format. It would be nice if Spotless had a step:
Which used the google-java-gormat jar.
One tricky issue is it seems that these jars are evolving, and there is some desire in the community to have access to snapshot versions of the jar. Very easy to support a fixed version of the jar (it is available on maven central, but I'm not sure how to support multiple versions...
If any contributors are interested in adding this functionality, all that is required is a method here: JavaExtension.java.
The text was updated successfully, but these errors were encountered: