├── .github └── workflows │ ├── ci.yml │ └── release.yml ├── .gitignore ├── .scala-steward.conf ├── .scalafmt.conf ├── LICENSE ├── README.md ├── RELEASING.md ├── build.sbt ├── plugin └── src │ ├── main │ └── scala │ │ └── com │ │ └── github │ │ └── sbt │ │ ├── JavaFormatterPlugin.scala │ │ └── javaformatter │ │ └── JavaFormatter.scala │ └── sbt-test │ └── sbt-java-formatter │ ├── aosp-style │ ├── build.sbt │ ├── project │ │ └── plugins.sbt │ ├── src │ │ ├── main │ │ │ ├── java-expected │ │ │ │ └── com │ │ │ │ │ └── lightbend │ │ │ │ │ └── BadFormatting.java │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting.java │ │ └── test │ │ │ ├── java-expected │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting.java │ │ │ └── java │ │ │ └── com │ │ │ └── lightbend │ │ │ └── BadFormatting.java │ └── test │ ├── default │ ├── build.sbt │ ├── project │ │ └── plugins.sbt │ ├── src │ │ ├── main │ │ │ ├── java-expected │ │ │ │ └── com │ │ │ │ │ └── lightbend │ │ │ │ │ └── BadFormatting.java │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting.java │ │ └── test │ │ │ ├── java-expected │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting.java │ │ │ └── java │ │ │ └── com │ │ │ └── lightbend │ │ │ └── BadFormatting.java │ └── test │ ├── multi-module-crawl-up-for-file │ ├── build.sbt │ ├── inner │ │ ├── no-formatting-file-here │ │ └── src │ │ │ ├── main │ │ │ ├── java-expected │ │ │ │ └── com │ │ │ │ │ └── lightbend │ │ │ │ │ └── BadFormatting2.java │ │ │ └── java │ │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ ├── BadFormatting2.java │ │ │ │ └── GoodFormatting.java │ │ │ └── test │ │ │ ├── java-expected │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting3.java │ │ │ └── java │ │ │ └── com │ │ │ └── lightbend │ │ │ ├── BadFormatting3.java │ │ │ └── GoodFormatting.java │ ├── project │ │ └── plugins.sbt │ ├── src │ │ └── main │ │ │ ├── java-expected │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting.java │ │ │ └── java │ │ │ └── com │ │ │ └── lightbend │ │ │ └── BadFormatting.java │ └── test │ ├── on-compile-false │ ├── build.sbt │ ├── project │ │ └── plugins.sbt │ ├── src │ │ └── main │ │ │ ├── java-expected │ │ │ └── com │ │ │ │ └── lightbend │ │ │ │ └── BadFormatting.java │ │ │ └── java │ │ │ └── com │ │ │ └── lightbend │ │ │ └── BadFormatting.java │ └── test │ └── on-compile-true │ ├── build.sbt │ ├── project │ └── plugins.sbt │ ├── src │ └── main │ │ ├── java-expected │ │ └── com │ │ │ └── lightbend │ │ │ └── BadFormatting.java │ │ └── java │ │ └── com │ │ └── lightbend │ │ └── BadFormatting.java │ └── test └── project ├── build.properties └── plugins.sbt /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | pull_request: 4 | push: 5 | # schedule: 6 | # # 2am EST every Saturday 7 | # - cron: '0 7 * * 6' 8 | jobs: 9 | test: 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | include: 14 | - os: ubuntu-latest 15 | java: 11 16 | distribution: zulu 17 | jobtype: 1 18 | - os: ubuntu-latest 19 | java: 11 20 | distribution: zulu 21 | jobtype: 2 22 | runs-on: ${{ matrix.os }} 23 | env: 24 | # define Java options for both official sbt and sbt-extras 25 | JAVA_OPTS: -Xms2048M -Xmx2048M -Xss6M -XX:ReservedCodeCacheSize=256M -Dfile.encoding=UTF-8 26 | JVM_OPTS: -Xms2048M -Xmx2048M -Xss6M -XX:ReservedCodeCacheSize=256M -Dfile.encoding=UTF-8 27 | steps: 28 | - name: Checkout 29 | uses: actions/checkout@v4 30 | with: 31 | fetch-depth: 0 32 | - name: Setup JDK 33 | uses: actions/setup-java@v4 34 | with: 35 | distribution: "${{ matrix.distribution }}" 36 | java-version: "${{ matrix.java }}" 37 | cache: sbt 38 | - uses: sbt/setup-sbt@v1 39 | - name: Build and test (sbt 1.x) 40 | if: ${{ matrix.jobtype == 1 }} 41 | shell: bash 42 | run: sbt -v '++ 2.12.x' clean test scripted 43 | - name: Build and test (sbt 2.x) 44 | if: ${{ matrix.jobtype == 2 }} 45 | shell: bash 46 | run: sbt -v '++ 3.x' clean test scripted 47 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | branches: 5 | - main 6 | tags: 7 | - '*' 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | env: 12 | # define Java options for both official sbt and sbt-extras 13 | JAVA_OPTS: -Xms2048M -Xmx2048M -Xss6M -XX:ReservedCodeCacheSize=256M -Dfile.encoding=UTF-8 14 | JVM_OPTS: -Xms2048M -Xmx2048M -Xss6M -XX:ReservedCodeCacheSize=256M -Dfile.encoding=UTF-8 15 | steps: 16 | - name: Checkout 17 | uses: actions/checkout@v4 18 | with: 19 | fetch-depth: 0 20 | - name: Setup JDK 21 | uses: actions/setup-java@v4 22 | with: 23 | distribution: temurin 24 | java-version: 11 25 | cache: sbt 26 | - uses: sbt/setup-sbt@v1 27 | - name: Release 28 | env: 29 | SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} 30 | SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} 31 | PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} 32 | PGP_SECRET: ${{ secrets.PGP_SECRET }} 33 | CI_CLEAN: clean 34 | CI_RELEASE: +publishSigned 35 | run: | 36 | sbt ci-release 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .idea 3 | .bsp/ 4 | -------------------------------------------------------------------------------- /.scala-steward.conf: -------------------------------------------------------------------------------- 1 | // really slow dependency updates, please 2 | pullRequests.frequency = "90 days" 3 | 4 | updates.pin = [ 5 | // Releases after 1.24.x require JDK 17 6 | // https://github.com/google/google-java-format/releases/tag/v1.25.0 7 | { groupId = "com.google.googlejavaformat", artifactId = "google-java-format", version = "1.24." } 8 | ] 9 | commits.message = "bump: ${artifactName} ${nextVersion} (was ${currentVersion})" 10 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | version = 3.9.4 2 | 3 | style = defaultWithAlign 4 | 5 | runner.dialect = scala212 6 | docstrings.style = Asterisk 7 | indentOperator.preset = spray 8 | maxColumn = 120 9 | rewrite.rules = [RedundantParens, SortImports, AvoidInfix] 10 | indentOperator.topLevelOnly = false 11 | align.tokens = [{code = "=>", owner = "Case"}] 12 | align.openParenDefnSite = false 13 | align.openParenCallSite = false 14 | optIn.breakChainOnFirstMethodDot = false 15 | optIn.configStyleArguments = false 16 | danglingParentheses.preset = false 17 | spaces.inImportCurlyBraces = true 18 | rewrite.neverInfix.excludeFilters = [ 19 | and 20 | min 21 | max 22 | until 23 | to 24 | by 25 | eq 26 | ne 27 | "should.*" 28 | "contain.*" 29 | "must.*" 30 | in 31 | ignore 32 | be 33 | taggedAs 34 | thrownBy 35 | synchronized 36 | have 37 | when 38 | size 39 | only 40 | noneOf 41 | oneElementOf 42 | noElementsOf 43 | atLeastOneElementOf 44 | atMostOneElementOf 45 | allElementsOf 46 | inOrderElementsOf 47 | theSameElementsAs 48 | ] 49 | rewriteTokens = { 50 | "⇒": "=>" 51 | "→": "->" 52 | "←": "<-" 53 | } 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [sbt-java-formatter][] [![scaladex-badge][]][scaladex] 2 | 3 | [sbt-java-formatter]: https://github.com/sbt/sbt-java-formatter 4 | [scaladex]: https://index.scala-lang.org/sbt/sbt-java-formatter 5 | [scaladex-badge]: https://index.scala-lang.org/sbt/sbt-java-formatter/latest.svg 6 | 7 | An sbt plugin for formatting Java code. This plugin began as a combination of ideas from this 8 | [blog post](https://ssscripting.wordpress.com/2009/06/10/how-to-use-the-eclipse-code-formatter-from-your-code/) 9 | and this [maven plugin](https://github.com/revelc/formatter-maven-plugin), though it has evolved since. 10 | 11 | # Usage 12 | 13 | Add the plugin to `project/plugins.sbt`: 14 | 15 | ```scala 16 | addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % --latest version---) 17 | ``` 18 | 19 | For available versions see [releases](https://github.com/sbt/sbt-java-formatter/releases). 20 | 21 | * `javafmt` formats Java files 22 | * `javafmtAll` formats Java files for all configurations (`Compile` and `Test` by default) 23 | * `javafmtCheck` fails if files need reformatting 24 | * `javafmtCheckAll` fails if files need reformatting in any configuration (`Compile` and `Test` by default) 25 | 26 | * The `javafmtOnCompile` setting controls whether the formatter kicks in on compile (`false` by default). 27 | * The `javafmtStyle` setting defines the formatting style: Google Java Style (by default) or AOSP style. 28 | 29 | This plugin requires sbt 1.3.0+. 30 | 31 | ## Enable in other scopes (eg `IntegrationTest`) 32 | 33 | The sbt plugin is enabled by default for the `Test` and `Compile` configurations. Use `JavaFormatterPlugin.toBeScopedSettings` to enable the plugin for the `IntegrationTest` scope and then use `It/javafmt` to format. 34 | 35 | ```scala 36 | inConfig(IntegrationTest)(JavaFormatterPlugin.toBeScopedSettings) 37 | ``` 38 | 39 | # Configuration 40 | 41 | This plugin uses the [Google Java Format](https://github.com/google/google-java-format) library, which makes it quite opinionated and not particularly configurable. 42 | 43 | If you want to tweak the format, take a minute to consider whether it is really worth it, and have a look at the motivations in the [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html). 44 | If you decide you really need more flexibility, you could consider other plugins such as the [sbt-checkstyle-plugin](https://github.com/etsy/sbt-checkstyle-plugin) 45 | 46 | # Contributing 47 | 48 | Yes, we'll happily accept PRs to improve the plugin. 49 | 50 | Take a look at the [contributors graph](https://github.com/sbt/sbt-java-formatter/graphs/contributors) if you want to contact 51 | any of the contributors directly. 52 | 53 | # License 54 | 55 | Apache v2 56 | -------------------------------------------------------------------------------- /RELEASING.md: -------------------------------------------------------------------------------- 1 | ### Releasing 2 | 3 | See the [prerequisites](#prerequisites) if this is your first release. 4 | 5 | ## Step 0: Create a new release tracking 6 | 7 | 1. Click ["new issue"][issues/new]; 8 | 2. Opening the raw [RELEASING.md][] content; and 9 | 3. Copying the following release checklist, up to "You are done!", into the new issue and create it. 10 | 11 | ## Release checklist 12 | 13 | * [ ] Check [closed issues without a milestone][issues/orphan] and either assign them the upcoming release milestone or 'invalid' 14 | * [ ] Create a [new release][releases/new] with: 15 | * [ ] the next tag version (e.g. `v0.4.0`) 16 | * [ ] title and release description including notable changes 17 | * [ ] link to the [milestone][milestones/list] showing an overview of closed issues for this release 18 | * [ ] overview of contributors generated by [`sbt-authors`](https://github.com/2m/authors) 19 | * [ ] Pull the tag, check `show version` then run `clean; publishSigned`. You should start seeing "published sbt-java-formatter to https://oss.sonatype.org/service/local/staging/deploy/maven2/..". 20 | * [ ] [Find, close and release][sonatype/staging-repos] your staging repository. (See Sonatype's [Releasing the Deployment][sonatype/guide] guide.) 21 | * [ ] [Close][milestones/list] the milestone for this release and create a new one. 22 | 23 | [issues/new]: https://github.com/sbt/sbt-java-formatter/issues/new 24 | [issues/orphan]: https://github.com/sbt/sbt-java-formatter/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aclosed%20no%3Amilestone 25 | [compare/view]: https://github.com/sbt/sbt-java-formatter/compare/0.8.1...master 26 | [milestones/list]: https://github.com/sbt/sbt-java-formatter/milestones?direction=asc 27 | [releases/list]: https://github.com/sbt/sbt-java-formatter/releases 28 | [releases/new]: https://github.com/sbt/sbt-java-formatter/releases/new 29 | 30 | [RELEASING.md]: https://raw.githubusercontent.com/sbt/sbt-java-formatter/master/RELEASING.md 31 | [sonatype/guide]: https://central.sonatype.org/pages/releasing-the-deployment.html 32 | [sonatype/staging-repos]: https://oss.sonatype.org/#stagingRepositories 33 | 34 | ## Prerequisites 35 | 36 | * You need oss.sontaype.org publish permissions by means of a request to [OSSRH-1324](https://issues.sonatype.org/browse/OSSRH-1324). 37 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | lazy val scala212 = "2.12.18" 2 | lazy val scala3 = "3.6.4" 3 | ThisBuild / scalaVersion := scala212 4 | ThisBuild / crossScalaVersions := Seq(scala212, scala3) 5 | 6 | lazy val sbtJavaFormatter = project.in(file(".")).aggregate(plugin).settings(publish / skip := true) 7 | 8 | lazy val plugin = project 9 | .in(file("plugin")) 10 | .enablePlugins(SbtPlugin) 11 | .enablePlugins(AutomateHeaderPlugin) 12 | .settings( 13 | name := "sbt-java-formatter", 14 | homepage := scmInfo.value.map(_.browseUrl), 15 | scmInfo := Some( 16 | ScmInfo(url("https://github.com/sbt/sbt-java-formatter"), "scm:git:git@github.com:sbt/sbt-java-formatter.git")), 17 | developers := List( 18 | Developer("ktoso", "Konrad 'ktoso' Malawski", "", url("https://github.com/ktoso"))), 19 | libraryDependencies ++= Seq("com.google.googlejavaformat" % "google-java-format" % "1.24.0"), 20 | startYear := Some(2015), 21 | description := "Formats Java code in your project.", 22 | licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html")), 23 | (pluginCrossBuild / sbtVersion) := { 24 | scalaBinaryVersion.value match { 25 | case "2.12" => "1.9.0" 26 | case _ => "2.0.0-M4" 27 | } 28 | }, 29 | scalacOptions ++= { 30 | Vector("-encoding", "UTF-8", "-unchecked", "-deprecation", "-feature") ++ (scalaBinaryVersion.value match { 31 | case "2.12" => Vector("-Xsource:3") 32 | case _ => Vector("-Wconf:error") 33 | }) 34 | }, 35 | javacOptions ++= Seq("-encoding", "UTF-8"), 36 | scriptedLaunchOpts := { 37 | scriptedLaunchOpts.value ++ 38 | Seq("-Xmx1024M", "-Dplugin.version=" + version.value) 39 | }, 40 | scriptedBufferLog := false, 41 | scalafmtOnCompile := true) 42 | 43 | ThisBuild / organization := "com.github.sbt" 44 | ThisBuild / organizationName := "sbt community" 45 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/github/sbt/JavaFormatterPlugin.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 sbt community 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.sbt 18 | 19 | import _root_.sbt.Keys._ 20 | import _root_.sbt.{ Def, _ } 21 | import com.google.googlejavaformat.java.JavaFormatterOptions 22 | import com.github.sbt.javaformatter.JavaFormatter 23 | 24 | @deprecated("Use javafmtOnCompile setting instead", "0.5.1") 25 | object AutomateJavaFormatterPlugin extends AutoPlugin { 26 | override def trigger = allRequirements 27 | 28 | override def `requires` = plugins.JvmPlugin && JavaFormatterPlugin 29 | 30 | override def globalSettings: Seq[Def.Setting[?]] = Seq(JavaFormatterPlugin.autoImport.javafmtOnCompile := false) 31 | } 32 | 33 | object JavaFormatterPlugin extends AutoPlugin { 34 | 35 | object autoImport { 36 | val javafmt: TaskKey[Unit] = taskKey("Format Java sources") 37 | val javafmtCheck: TaskKey[Boolean] = taskKey("Fail, if a Java source needs reformatting.") 38 | val javafmtAll: TaskKey[Unit] = taskKey( 39 | "Execute the javafmt task for all configurations in which it is enabled. " + 40 | "(By default this means the Compile and Test configurations.)") 41 | val javafmtCheckAll: TaskKey[Unit] = taskKey( 42 | "Execute the javafmtCheck task for all configurations in which it is enabled. " + 43 | "(By default this means the Compile and Test configurations.)") 44 | val javafmtOnCompile = settingKey[Boolean]("Format Java source files on compile, off by default.") 45 | val javafmtStyle = 46 | settingKey[JavaFormatterOptions.Style]("Define formatting style, Google Java Style (default) or AOSP") 47 | val javafmtOptions = settingKey[JavaFormatterOptions]( 48 | "Define all formatting options such as style or enabling Javadoc formatting. See _JavaFormatterOptions_ for more") 49 | } 50 | 51 | import autoImport._ 52 | 53 | override def trigger = allRequirements 54 | 55 | override def `requires` = plugins.JvmPlugin 56 | 57 | override def projectSettings: Seq[Def.Setting[?]] = { 58 | val anyConfigsInThisProject = ScopeFilter(configurations = inAnyConfiguration) 59 | 60 | notToBeScopedSettings ++ 61 | Seq(Compile, Test).flatMap(inConfig(_)(toBeScopedSettings)) ++ 62 | Seq( 63 | javafmtAll := { 64 | val _ = javafmt.?.all(anyConfigsInThisProject).value 65 | }, 66 | javafmtCheckAll := { 67 | val _ = javafmtCheck.?.all(anyConfigsInThisProject).value 68 | }) 69 | } 70 | 71 | override def globalSettings: Seq[Def.Setting[?]] = 72 | Seq(javafmtOnCompile := false, javafmtStyle := JavaFormatterOptions.Style.GOOGLE) 73 | 74 | def toBeScopedSettings: Seq[Setting[?]] = 75 | List( 76 | (javafmt / sourceDirectories) := List(javaSource.value), 77 | javafmtOptions := JavaFormatterOptions.builder().style(javafmtStyle.value).build(), 78 | javafmt := { 79 | val streamz = streams.value 80 | val sD = (javafmt / sourceDirectories).value.toList 81 | val iF = (javafmt / includeFilter).value 82 | val eF = (javafmt / excludeFilter).value 83 | val cache = streamz.cacheStoreFactory 84 | val options = javafmtOptions.value 85 | JavaFormatter(sD, iF, eF, streamz, cache, options) 86 | }, 87 | javafmtCheck := { 88 | val streamz = streams.value 89 | val baseDir = (ThisBuild / baseDirectory).value 90 | val sD = (javafmt / sourceDirectories).value.toList 91 | val iF = (javafmt / includeFilter).value 92 | val eF = (javafmt / excludeFilter).value 93 | val cache = (javafmt / streams).value.cacheStoreFactory 94 | val options = javafmtOptions.value 95 | JavaFormatter.check(baseDir, sD, iF, eF, streamz, cache, options) 96 | }, 97 | javafmtDoFormatOnCompile := Def.settingDyn { 98 | if (javafmtOnCompile.value) { 99 | resolvedScoped.value.scope / javafmt 100 | } else { 101 | Def.task(()) 102 | } 103 | }.value, 104 | compile / compileInputs := (compile / compileInputs).dependsOn(javafmtDoFormatOnCompile).value) 105 | 106 | def notToBeScopedSettings: Seq[Setting[?]] = 107 | List(javafmt / includeFilter := "*.java") 108 | 109 | private val javafmtDoFormatOnCompile = 110 | taskKey[Unit]("Format Java source files if javafmtOnCompile is on.") 111 | 112 | } 113 | -------------------------------------------------------------------------------- /plugin/src/main/scala/com/github/sbt/javaformatter/JavaFormatter.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 sbt community 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.sbt.javaformatter 18 | 19 | import _root_.sbt.Keys._ 20 | import _root_.sbt._ 21 | import _root_.sbt.util.CacheImplicits._ 22 | import _root_.sbt.util.{ CacheStoreFactory, FileInfo, Logger } 23 | import com.google.googlejavaformat.java.{ Formatter, JavaFormatterOptions } 24 | import scala.collection.immutable.Seq 25 | 26 | object JavaFormatter { 27 | 28 | def apply( 29 | sourceDirectories: Seq[File], 30 | includeFilter: FileFilter, 31 | excludeFilter: FileFilter, 32 | streams: TaskStreams, 33 | cacheStoreFactory: CacheStoreFactory, 34 | options: JavaFormatterOptions): Unit = { 35 | val files = sourceDirectories.descendantsExcept(includeFilter, excludeFilter).get().toList 36 | cachedFormatSources(cacheStoreFactory, files, streams.log)(new Formatter(options)) 37 | } 38 | 39 | def check( 40 | baseDir: File, 41 | sourceDirectories: Seq[File], 42 | includeFilter: FileFilter, 43 | excludeFilter: FileFilter, 44 | streams: TaskStreams, 45 | cacheStoreFactory: CacheStoreFactory, 46 | options: JavaFormatterOptions): Boolean = { 47 | val files = sourceDirectories.descendantsExcept(includeFilter, excludeFilter).get().toList 48 | val analysis = cachedCheckSources(cacheStoreFactory, baseDir, files, streams.log)(new Formatter(options)) 49 | trueOrBoom(analysis) 50 | } 51 | 52 | private def plural(i: Int) = if (i == 1) "" else "s" 53 | 54 | private def trueOrBoom(analysis: Analysis): Boolean = { 55 | val failureCount = analysis.failedCheck.size 56 | if (failureCount > 0) { 57 | throw new MessageOnlyException(s"${failureCount} file${plural(failureCount)} must be formatted") 58 | } 59 | true 60 | } 61 | 62 | case class Analysis(failedCheck: Set[File]) 63 | 64 | object Analysis { 65 | 66 | import sjsonnew.{ :*:, LList, LNil } 67 | 68 | implicit val analysisIso: sjsonnew.IsoLList.Aux[Analysis, Set[File] :*: LNil] = LList.iso( 69 | { (a: Analysis) => ("failedCheck", a.failedCheck) :*: LNil }, 70 | { (in: Set[File] :*: LNil) => 71 | Analysis(in.head) 72 | }) 73 | } 74 | 75 | private def cachedCheckSources(cacheStoreFactory: CacheStoreFactory, baseDir: File, sources: Seq[File], log: Logger)( 76 | implicit formatter: Formatter): Analysis = { 77 | trackSourcesViaCache(cacheStoreFactory, sources) { (outDiff, prev) => 78 | log.debug(outDiff.toString) 79 | val updatedOrAdded = outDiff.modified & outDiff.checked 80 | val filesToCheck: Set[File] = updatedOrAdded 81 | val prevFailed: Set[File] = prev.failedCheck & outDiff.unmodified 82 | prevFailed.foreach { file => warnBadFormat(file.relativeTo(baseDir).getOrElse(file), log) } 83 | val result = checkSources(baseDir, filesToCheck.toList, log) 84 | prev.copy(failedCheck = result.failedCheck | prevFailed) 85 | } 86 | } 87 | 88 | private def warnBadFormat(file: File, log: Logger): Unit = { 89 | log.warn(s"${file.toString} isn't formatted properly!") 90 | } 91 | 92 | private def checkSources(baseDir: File, sources: Seq[File], log: Logger)(implicit formatter: Formatter): Analysis = { 93 | if (sources.nonEmpty) { 94 | log.info(s"Checking ${sources.size} Java source${plural(sources.size)}...") 95 | } 96 | val unformatted = withFormattedSources(sources, log)((file, input, output) => { 97 | val diff = input != output 98 | if (diff) { 99 | warnBadFormat(file.relativeTo(baseDir).getOrElse(file), log) 100 | Some(file) 101 | } else None 102 | }).flatten.flatten.toSet 103 | Analysis(failedCheck = unformatted) 104 | } 105 | 106 | private def cachedFormatSources(cacheStoreFactory: CacheStoreFactory, sources: Seq[File], log: Logger)(implicit 107 | formatter: Formatter): Unit = { 108 | trackSourcesViaCache(cacheStoreFactory, sources) { (outDiff, prev) => 109 | log.debug(outDiff.toString) 110 | val updatedOrAdded = outDiff.modified & outDiff.checked 111 | val filesToFormat: Set[File] = updatedOrAdded | prev.failedCheck 112 | if (filesToFormat.nonEmpty) { 113 | log.info(s"Formatting ${filesToFormat.size} Java source${plural(filesToFormat.size)}...") 114 | formatSources(filesToFormat, log) 115 | } 116 | Analysis(Set.empty) 117 | } 118 | } 119 | 120 | private def formatSources(sources: Set[File], log: Logger)(implicit formatter: Formatter): Unit = { 121 | val cnt = withFormattedSources(sources.toList, log)((file, input, output) => { 122 | if (input != output) { 123 | IO.write(file, output) 124 | 1 125 | } else { 126 | 0 127 | } 128 | }).flatten.sum 129 | log.info(s"Reformatted $cnt Java source${plural(cnt)}") 130 | } 131 | 132 | private def trackSourcesViaCache(cacheStoreFactory: CacheStoreFactory, sources: Seq[File])( 133 | f: (ChangeReport[File], Analysis) => Analysis): Analysis = { 134 | val prevTracker = Tracked.lastOutput[Unit, Analysis](cacheStoreFactory.make("last")) { (_, prev0) => 135 | val prev = prev0.getOrElse(Analysis(Set.empty)) 136 | Tracked.diffOutputs(cacheStoreFactory.make("output-diff"), FileInfo.lastModified)(sources.toSet) { 137 | (outDiff: ChangeReport[File]) => f(outDiff, prev) 138 | } 139 | 140 | } 141 | prevTracker(()) 142 | } 143 | 144 | private def withFormattedSources[T](sources: Seq[File], log: Logger)(onFormat: (File, String, String) => T)(implicit 145 | formatter: Formatter): Seq[Option[T]] = { 146 | sources.map { file => 147 | val input = IO.read(file) 148 | try { 149 | val output = formatter.formatSourceAndFixImports(input) 150 | Some(onFormat(file, input, output)) 151 | } catch { 152 | case e: Exception => Some(onFormat(file, input, input)) 153 | } 154 | } 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/build.sbt: -------------------------------------------------------------------------------- 1 | import com.google.googlejavaformat.java.JavaFormatterOptions 2 | // no settings needed 3 | 4 | ThisBuild / javafmtStyle := JavaFormatterOptions.Style.AOSP 5 | ThisBuild / javafmtOnCompile := true 6 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version")) 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/src/main/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/src/main/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/src/test/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/src/test/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/aosp-style/test: -------------------------------------------------------------------------------- 1 | # compile should trigger formatting 2 | > Test/compile 3 | 4 | #$ exec echo "====== FORMATTED ======" 5 | #$ exec cat src/main/java/com/lightbend/BadFormatting.java 6 | #$ exec echo "====== EXPECTED ======" 7 | #$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java 8 | 9 | #$ exec echo "====== DIFF ======" 10 | $ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java 11 | 12 | $ exec diff src/test/java/com/lightbend/BadFormatting.java src/test/java-expected/com/lightbend/BadFormatting.java 13 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/build.sbt: -------------------------------------------------------------------------------- 1 | // no settings needed 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version")) 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/src/main/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/src/main/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/src/test/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/src/test/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/default/test: -------------------------------------------------------------------------------- 1 | > javafmtAll 2 | 3 | #$ exec echo "====== FORMATTED ======" 4 | #$ exec cat src/main/java/com/lightbend/BadFormatting.java 5 | #$ exec echo "====== EXPECTED ======" 6 | #$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java 7 | 8 | #$ exec echo "====== DIFF ======" 9 | $ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java 10 | 11 | $ exec diff src/test/java/com/lightbend/BadFormatting.java src/test/java-expected/com/lightbend/BadFormatting.java 12 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val sbtJavaFormatter = project 2 | .in(file(".")) 3 | .aggregate(inner) 4 | .settings( 5 | TaskKey[Unit]("removeFile") := { 6 | val actualPath = (inner / Compile / javaSource).value / "com" / "lightbend" / "GoodFormatting.java" 7 | java.nio.file.Files.delete(actualPath.toPath) 8 | } 9 | ) 10 | 11 | lazy val inner = project 12 | .in(file("inner")) 13 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/no-formatting-file-here: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sbt/sbt-java-formatter/da513c01735b18b82ccb2cad379b1771b540b78b/plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/no-formatting-file-here -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/src/main/java-expected/com/lightbend/BadFormatting2.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting2 { 4 | BadFormatting2() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/src/main/java/com/lightbend/BadFormatting2.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting2 { 4 | BadFormatting2 () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/src/main/java/com/lightbend/GoodFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class GoodFormatting { 4 | GoodFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/src/test/java-expected/com/lightbend/BadFormatting3.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting3 { 4 | BadFormatting3() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/src/test/java/com/lightbend/BadFormatting3.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting3 { 4 | BadFormatting3 () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/inner/src/test/java/com/lightbend/GoodFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class GoodFormatting { 4 | GoodFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version")) 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/src/main/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/src/main/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/multi-module-crawl-up-for-file/test: -------------------------------------------------------------------------------- 1 | # compile should trigger formatting 2 | -> javafmtCheck 3 | > javafmt 4 | -> javafmtCheckAll 5 | # make sure the caching works when files disappear 6 | > removeFile 7 | > javafmtAll 8 | 9 | #$ exec echo "====== FORMATTED ======" 10 | #$ exec cat src/main/java/com/lightbend/BadFormatting.java 11 | #$ exec echo "====== EXPECTED ======" 12 | #$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java 13 | 14 | #$ exec echo "====== DIFF ======" 15 | $ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java 16 | $ exec diff inner/src/main/java/com/lightbend/BadFormatting2.java inner/src/main/java-expected/com/lightbend/BadFormatting2.java 17 | $ exec diff inner/src/test/java/com/lightbend/BadFormatting3.java inner/src/test/java-expected/com/lightbend/BadFormatting3.java 18 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-false/build.sbt: -------------------------------------------------------------------------------- 1 | // no settings needed 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-false/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version")) 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-false/src/main/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-false/src/main/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-false/test: -------------------------------------------------------------------------------- 1 | # compile should not trigger formatting 2 | > compile 3 | 4 | #$ exec echo "====== FORMATTED ======" 5 | #$ exec cat src/main/java/com/lightbend/BadFormatting.java 6 | #$ exec echo "====== EXPECTED ======" 7 | #$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java 8 | 9 | #$ exec echo "====== DIFF ======" 10 | $ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java 11 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-true/build.sbt: -------------------------------------------------------------------------------- 1 | ThisBuild / javafmtOnCompile := true 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-true/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-java-formatter" % System.getProperty("plugin.version")) 2 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-true/src/main/java-expected/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting() { 5 | example(); 6 | } 7 | 8 | public void example() {} 9 | } 10 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-true/src/main/java/com/lightbend/BadFormatting.java: -------------------------------------------------------------------------------- 1 | package com.lightbend; 2 | 3 | public class BadFormatting { 4 | BadFormatting () {example();} 5 | public void example () {} 6 | } 7 | -------------------------------------------------------------------------------- /plugin/src/sbt-test/sbt-java-formatter/on-compile-true/test: -------------------------------------------------------------------------------- 1 | # compile should not trigger formatting 2 | > compile 3 | 4 | #$ exec echo "====== FORMATTED ======" 5 | #$ exec cat src/main/java/com/lightbend/BadFormatting.java 6 | #$ exec echo "====== EXPECTED ======" 7 | #$ exec cat src/main/java-expected/com/lightbend/BadFormatting.java 8 | 9 | #$ exec echo "====== DIFF ======" 10 | $ exec diff src/main/java/com/lightbend/BadFormatting.java src/main/java-expected/com/lightbend/BadFormatting.java 11 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.11.1 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.4") 2 | addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.10.0") 3 | addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.11.1") 4 | 5 | libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value 6 | --------------------------------------------------------------------------------