From 813864ec0f509a2110a2e05b109979f86cc89328 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Tue, 14 Jan 2020 14:01:17 -0800 Subject: [PATCH] Add swoval java formatting plugin The swoval javafmt plugin uses the google java formatter (which I believe is the only widely used java formatter) to format source files. It does not provide an automatic javafmtOnCompile method like the scalafmt plugin so I had to manually implement that functionality. In general the java formatter is much faster than scalafmt so the impact of having javafmtOnCompile set to true is very low. --- .travis.yml | 2 +- build.sbt | 11 +++++++++++ project/plugins.sbt | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 52fcfe5dc..1602d81c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ env: # WHITESOURCE_PASSWORD= - secure: d3bu2KNwsVHwfhbGgO+gmRfDKBJhfICdCJFGWKf2w3Gv86AJZX9nuTYRxz0KtdvEHO5Xw8WTBZLPb2thSJqhw9OCm4J8TBAVqCP0ruUj4+aqBUFy4bVexQ6WKE6nWHs4JPzPk8c6uC1LG3hMuzlC8RGETXtL/n81Ef1u7NjyXjs= matrix: - - SBT_CMD="mimaReportBinaryIssues ;scalafmtCheckAll ; scalafmtSbtCheck; headerCheck ;test:headerCheck ;whitesourceOnPush ;test:compile; publishLocal ;mainSettingsProj/test ;safeUnitTests ;otherUnitTests; doc; $UTIL_TESTS; ++$SCALA_213; $UTIL_TESTS" + - SBT_CMD="mimaReportBinaryIssues ; javafmtCheck ; Test / javafmtCheck; scalafmtCheckAll ; scalafmtSbtCheck; headerCheck ;test:headerCheck ;whitesourceOnPush ;test:compile; publishLocal ;mainSettingsProj/test ;safeUnitTests ;otherUnitTests; doc; $UTIL_TESTS; ++$SCALA_213; $UTIL_TESTS" - SBT_CMD="scripted actions/* apiinfo/* compiler-project/* ivy-deps-management/* reporter/* tests/* watch/* classloader-cache/* package/*" - SBT_CMD="scripted dependency-management/* plugins/* project-load/* java/* run/* nio/*" - SBT_CMD="repoOverrideTest:scripted dependency-management/*; scripted source-dependencies/* project/*" diff --git a/build.sbt b/build.sbt index 1d236719c..eadab0a66 100644 --- a/build.sbt +++ b/build.sbt @@ -87,6 +87,16 @@ def commonBaseSettings: Seq[Setting[_]] = Def.settings( s"https://github.com/sbt/sbt/tree/$tagOrSha€{FILE_PATH}.scala" ) }, + Compile / javafmtOnCompile := Def + .taskDyn(if ((scalafmtOnCompile).value) Compile / javafmt else Def.task(())) + .value, + Test / javafmtOnCompile := Def + .taskDyn(if ((Test / scalafmtOnCompile).value) Test / javafmt else Def.task(())) + .value, + Compile / unmanagedSources / inputFileStamps := + (Compile / unmanagedSources / inputFileStamps).dependsOn(Compile / javafmtOnCompile).value, + Test / unmanagedSources / inputFileStamps := + (Test / unmanagedSources / inputFileStamps).dependsOn(Test / javafmtOnCompile).value, crossScalaVersions := Seq(baseScalaVersion), bintrayPackage := (bintrayPackage in ThisBuild).value, bintrayRepository := (bintrayRepository in ThisBuild).value, @@ -1257,6 +1267,7 @@ lazy val otherProjects: ScopeFilter = ScopeFilter( ), inConfigurations(Test) ) +lazy val javafmtOnCompile = taskKey[Unit]("Formats java sources before compile") def customCommands: Seq[Setting[_]] = Seq( commands += Command.command("setupBuildScala212") { state => diff --git a/project/plugins.sbt b/project/plugins.sbt index 5fe1f6908..c1a2b30d6 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -10,3 +10,4 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.9.0") addSbtPlugin("com.lightbend" % "sbt-whitesource" % "0.1.14") addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9") addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.6.1") +addSbtPlugin("com.swoval" % "sbt-java-format" % "0.2.3")