sbt / sbt-java-formatter Goto Github PK
View Code? Open in Web Editor NEWAn sbt plugin for formating Java code
License: Apache License 2.0
An sbt plugin for formating Java code
License: Apache License 2.0
To enable formatting modern Java source code, the Google Java Formatter needs to be updated.
Versions later than 1.8 require JDK 11.
And as chatted on the eng meeting, such a thing could live under lightbend
on github (or under sbt
).
Shared ownership between our teams of this plugin would be perfect I thought @bantonsson, as we want to use it in Akka in any case.
Hey guys,
would it be possible to release non-snapshot version of this plugin? At work we are not able to use snapshots :( and i struggle finding a non-snapshot version post-bintray.
Thanks!
Current implementation of this plugin uses an eclipse library and XML file to setup and apply the formatting rules.
Instead, that impl could be replaced with Google's google-java-format which, despite not being configurable and enforce Google Java Style , seems to be maintained and integrates with IntelliJ and Eclipse alike.
We'd like to use this in Akka, as people contributing Java code (for Akka HTTP) have settings all over the place, so we have spaces, tabs, 4 spaces, 2 spaces and everthing in between sometimes. Would want to just have a formatter there.
Makes things easier if it's just "pop this file there" and less configuration
v0.9.0
)sbt-authors
show version
then run clean; publishSigned
. You should start seeing "published sbt-java-formatter to https://oss.sonatype.org/service/local/staging/deploy/maven2/..".I have this error randomly popping up. A week ago this worked fine. Removing the plugin also fixes the error.
java.lang.SecurityException: class "org.eclipse.jdt.core.compiler.InvalidInputException"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:898)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:668)
at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jdt.core.ToolFactory.createCodeFormatter(ToolFactory.java:173)
at org.eclipse.jdt.core.ToolFactory.createCodeFormatter(ToolFactory.java:139)
at com.lightbend.sbt.javaformatter.JavaFormatter$.apply(JavaFormatter.scala:67)
at com.lightbend.sbt.JavaFormatterPlugin$$anonfun$toBeScopedSettings$2.apply(JavaFormatterPlugin.scala:88)
at com.lightbend.sbt.JavaFormatterPlugin$$anonfun$toBeScopedSettings$2.apply(JavaFormatterPlugin.scala:76)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
at sbt.std.Transform$$anon$4.work(System.scala:63)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
at sbt.Execute.work(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
The formatting rules of the formatting-java.xml
file are not used, no matter at which location it is specified:
Example
sbt scripted test default-xml-file-location
Here, the formatting-java.xml has defined the indentation size of 4, but after compiling the source code it is formatted to an indentation size of 2.
<setting id="org.eclipse.jdt.core.formatter.indentation.size" value="4"/>
The expected java file contains 2 spaces and the test is passing: https://github.com/typesafehub/sbt-java-formatter/blob/master/plugin/src/sbt-test/sbt-java-formatter/default-xml-file-location/src/main/java-expected/com/lightbend/BadFormatting.java
I've also tried it out with other projects and I was never able to format the source code based on my defined rules.
Conclusion
Currently, it is not possible to override the default formatting. If a formatting file is specified then the source code is formatted, but not by the rules of the formatting file.
Given the poor handling of the recent security incident (https://arstechnica.com/information-technology/2021/09/travis-ci-flaw-exposed-secrets-for-thousands-of-open-source-projects/) I'm suspending Travis CI integration indefinitely.
Let's move on to GitHub Actions.
Hi,
I'm seeing that this plugin does not reorder imports as required by the google java style guide 3.3. For instance, if I make the following change to get the imports out of order
import static play.test.Helpers.contentAsString;
import akka.actor.ActorSystem;
-import controllers.AsyncController;
import controllers.CountController;
+import controllers.AsyncController;
import java.util.concurrent.CompletionStage;
import org.junit.Test;
import play.mvc.Result;
and run sbt javafmtAll
, then git diff
, I still see the diff (which is intentionally erroneous).
import static play.test.Helpers.contentAsString;
import akka.actor.ActorSystem;
-import controllers.AsyncController;
import controllers.CountController;
+import controllers.AsyncController;
import java.util.concurrent.CompletionStage;
import org.junit.Test;
import play.mvc.Result;
My plugins.sbt file contains addSbtPlugin("com.lightbend.sbt" % "sbt-java-formatter" % "0.6.0")
.
This sbt plugin could eventually support .editorconfig
or we could even discontinue this plugin in favour of a new, language-agnostic, .editorconfig
-based one.
I don't have admin rights in this repo so can't do it.
Admin me or please add travis :)
@ktoso since this plugin is being released to your bintray repo (https://bintray.com/ktosopl/sbt-plugins/sbt-java-formatter) could you please update the credentials in .travis.yml file
sbt-java-formatter/.travis.yml
Lines 32 to 37 in ff3ef38
to your Bintray username and API key from here?
This will enable automatic releases from Travis and I'll then be able to cut the 0.4.0
release.
The scalafmt docs explain how to use it when the it
config is enabled:
https://scalameta.org/scalafmt/docs/installation.html#enable-integrationtest
I think it would be nice to explain the same for sbt-java-formatter
in its README as well.
@ennru I think following should be correct (?)
inConfig(IntegrationTest)(JavaFormatterPlugin.toBeScopedSettings)
and now you can run it:javafmt
Given:
subproject/formatting.xml
subproject/src/... // should use `/subproject/formatting.xml`
src/... // should use `/formatting.xml`
formatting.xml
other/src/... // should use `/formatting.xml`
This way a sub project can be formatted using a different style if needed.
v0.4.0
)sbt-authors
show version
then run clean; publishSigned
. You should start seeing "published sbt-java-formatter to https://oss.sonatype.org/service/local/staging/deploy/maven2/..".v0.4.0
)sbt-authors
show version
then run clean; publishSigned
. You should start seeing "published sbt-java-formatter to https://oss.sonatype.org/service/local/staging/deploy/maven2/..".I tried to set up publish-from-tag in #157 but the project is missing publishing credentials.
Unable to use version 0.5.0 but sbt javafmt
works fine for 0.4.3
Error
14:27 $ sbt javafmt
[info] Loading global plugins from /Users/bharath/.sbt/1.0/plugins
[info] Loading settings for project bank-account-validation-build from plugins.sbt ...
[info] Loading project definition from /Users/bharath/Documents/devel/monorepo/mosaic/services/bank-account-validation/project
[info] Loading settings for project bank-account-validation from build.sbt ...
[info] Set current project to bank-account-validation (in build file:/Users/bharath/Documents/devel/monorepo/mosaic/services/bank-account-validation/)
[error] java.lang.NoClassDefFoundError: Could not initialize class com.lightbend.sbt.javaformatter.JavaFormatter$Analysis$
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.trackSourcesViaCache(JavaFormatter.scala:135)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.cachedFormatSources(JavaFormatter.scala:109)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.apply(JavaFormatter.scala:36)
[error] at com.lightbend.sbt.JavaFormatterPlugin$.$anonfun$toBeScopedSettings$2(JavaFormatterPlugin.scala:69)
[error] at com.lightbend.sbt.JavaFormatterPlugin$.$anonfun$toBeScopedSettings$2$adapted(JavaFormatterPlugin.scala:63)
[error] at scala.Function1.$anonfun$compose$1(Function1.scala:44)
[error] at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:40)
[error] at sbt.std.Transform$$anon$4.work(System.scala:67)
[error] at sbt.Execute.$anonfun$submit$2(Execute.scala:269)
[error] at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:16)
[error] at sbt.Execute.work(Execute.scala:278)
[error] at sbt.Execute.$anonfun$submit$1(Execute.scala:269)
[error] at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:178)
[error] at sbt.CompletionService$$anon$2.call(CompletionService.scala:37)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error] at java.lang.Thread.run(Thread.java:748)
[error] java.lang.NoSuchMethodError: sbt.package$.singleFileJsonFormatter()Lsjsonnew/JsonFormat;
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$Analysis$.<init>(JavaFormatter.scala:67)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$Analysis$.<clinit>(JavaFormatter.scala)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.trackSourcesViaCache(JavaFormatter.scala:135)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.cachedFormatSources(JavaFormatter.scala:109)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.apply(JavaFormatter.scala:36)
[error] at com.lightbend.sbt.JavaFormatterPlugin$.$anonfun$toBeScopedSettings$2(JavaFormatterPlugin.scala:69)
[error] at com.lightbend.sbt.JavaFormatterPlugin$.$anonfun$toBeScopedSettings$2$adapted(JavaFormatterPlugin.scala:63)
[error] at scala.Function1.$anonfun$compose$1(Function1.scala:44)
[error] at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:40)
[error] at sbt.std.Transform$$anon$4.work(System.scala:67)
[error] at sbt.Execute.$anonfun$submit$2(Execute.scala:269)
[error] at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:16)
[error] at sbt.Execute.work(Execute.scala:278)
[error] at sbt.Execute.$anonfun$submit$1(Execute.scala:269)
[error] at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:178)
[error] at sbt.CompletionService$$anon$2.call(CompletionService.scala:37)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error] at java.lang.Thread.run(Thread.java:748)
[error] java.lang.NoClassDefFoundError: Could not initialize class com.lightbend.sbt.javaformatter.JavaFormatter$Analysis$
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.trackSourcesViaCache(JavaFormatter.scala:135)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.cachedFormatSources(JavaFormatter.scala:109)
[error] at com.lightbend.sbt.javaformatter.JavaFormatter$.apply(JavaFormatter.scala:36)
[error] at com.lightbend.sbt.JavaFormatterPlugin$.$anonfun$toBeScopedSettings$2(JavaFormatterPlugin.scala:69)
[error] at com.lightbend.sbt.JavaFormatterPlugin$.$anonfun$toBeScopedSettings$2$adapted(JavaFormatterPlugin.scala:63)
[error] at scala.Function1.$anonfun$compose$1(Function1.scala:44)
[error] at sbt.internal.util.$tilde$greater.$anonfun$$u2219$1(TypeFunctions.scala:40)
[error] at sbt.std.Transform$$anon$4.work(System.scala:67)
[error] at sbt.Execute.$anonfun$submit$2(Execute.scala:269)
[error] at sbt.internal.util.ErrorHandling$.wideConvert(ErrorHandling.scala:16)
[error] at sbt.Execute.work(Execute.scala:278)
[error] at sbt.Execute.$anonfun$submit$1(Execute.scala:269)
[error] at sbt.ConcurrentRestrictions$$anon$4.$anonfun$submitValid$1(ConcurrentRestrictions.scala:178)
[error] at sbt.CompletionService$$anon$2.call(CompletionService.scala:37)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
[error] at java.util.concurrent.FutureTask.run(FutureTask.java:266)
[error] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[error] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[error] at java.lang.Thread.run(Thread.java:748)
[error] (bank-account-validation-api / Compile / javafmt) java.lang.NoClassDefFoundError: Could not initialize class com.lightbend.sbt.javaformatter.JavaFormatter$Analysis$
[error] (bank-account-validation-impl / Compile / javafmt) java.lang.NoSuchMethodError: sbt.package$.singleFileJsonFormatter()Lsjsonnew/JsonFormat;
[error] (Compile / javafmt) java.lang.NoClassDefFoundError: Could not initialize class com.lightbend.sbt.javaformatter.JavaFormatter$Analysis$
[error] Total time: 0 s, completed 14 Jan, 2020 2:27:52 PM
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.