├── .github └── workflows │ ├── release.yml │ └── scala.yml ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── NOTICE ├── README.md ├── build.sbt ├── maxwell └── src │ └── main │ └── java │ └── maxwell │ └── Maxwell.java ├── project ├── build.properties └── plugins.sbt └── src ├── main ├── scala-sbt-0.13 │ └── com │ │ └── lightbend │ │ └── sbt │ │ └── javaagent │ │ ├── Dependencies.scala │ │ └── Modules.scala ├── scala-sbt-1.0 │ └── com │ │ └── lightbend │ │ └── sbt │ │ └── javaagent │ │ ├── Dependencies.scala │ │ └── Modules.scala └── scala │ └── com │ └── lightbend │ └── sbt │ └── javaagent │ ├── JavaAgent.scala │ └── JavaAgentPackaging.scala └── sbt-test └── agent ├── arguments ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt ├── compile ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt ├── confs ├── build.sbt ├── project │ └── plugins.sbt ├── src │ ├── main │ │ └── scala │ │ │ └── Main.scala │ └── test │ │ └── scala │ │ └── Test.scala ├── test └── test.sbt ├── dist ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt ├── dist_1.9.x ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt ├── named ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt ├── provided ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt ├── run ├── build.sbt ├── project │ └── plugins.sbt ├── src │ └── main │ │ └── scala │ │ └── Main.scala ├── test └── test.sbt └── test ├── build.sbt ├── project └── plugins.sbt ├── src └── test │ └── scala │ └── Test.scala ├── test └── test.sbt /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: # Snapshots 6 | - master 7 | tags: ["**"] # Releases 8 | 9 | jobs: 10 | publish-artifacts: 11 | name: JDK 8 12 | runs-on: ubuntu-24.04 13 | if: ${{ github.repository_owner == 'sbt' }} 14 | steps: 15 | - name: Checkout 16 | uses: actions/checkout@v3 17 | with: 18 | # we don't know what commit the last tag was it's safer to get entire repo so previousStableVersion resolves 19 | fetch-depth: 0 20 | 21 | - name: Coursier Cache 22 | id: coursier-cache 23 | uses: coursier/cache-action@v6 24 | 25 | - name: Install Adoptium Temurin OpenJDK 26 | uses: coursier/setup-action@v1 27 | with: 28 | jvm: adoptium:8 29 | 30 | - name: Install sbt 31 | uses: sbt/setup-sbt@v1 32 | 33 | - name: Publish artifacts 34 | run: sbt ci-release 35 | env: 36 | SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} 37 | SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} 38 | PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} 39 | PGP_SECRET: ${{ secrets.PGP_SECRET }} 40 | 41 | - name: Cleanup before cache 42 | shell: bash 43 | run: | 44 | find $HOME/Library/Caches/Coursier/v1 -name "ivydata-*.properties" -delete || true 45 | find $HOME/.ivy2/cache -name "ivydata-*.properties" -delete || true 46 | find $HOME/.cache/coursier/v1 -name "ivydata-*.properties" -delete || true 47 | find $HOME/.sbt -name "*.lock" -delete || true 48 | -------------------------------------------------------------------------------- /.github/workflows/scala.yml: -------------------------------------------------------------------------------- 1 | name: Scala CI 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Set up JDK 1.8 17 | uses: actions/setup-java@v3 18 | with: 19 | distribution: temurin 20 | java-version: 8.0.372+7 21 | cache: sbt 22 | - name: Install sbt 23 | uses: sbt/setup-sbt@v1 24 | - name: Run tests 25 | run: sbt "^ scripted" 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | project/project 3 | .bsp/ 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Release Procedure 4 | 5 | If necessary, fix the version number in `version.sbt` and commit and push the change. It is fine to have `-SNAPSHOT` suffix, it will be discarded during the release process. 6 | 7 | Open the [Release workflow](https://github.com/sbt/sbt-javaagent/actions/workflows/release.yml) and click the “Run workflow” button. This will publish a release to Sonatype, create a Git tag (based on the version in `version.sbt` without its qualifier), and update the version number in `version.sbt` with the next release version. 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | sbt-javaagent 2 | Copyright 2016-2017 Lightbend Inc. https://www.lightbend.com 3 | License: http://www.apache.org/licenses/LICENSE-2.0 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sbt-javaagent 2 | 3 | This sbt plugin adds Java agents to projects in a general way. It can enable agents in [sbt-native-packager] dists, as compile-time dependencies, in forked run, or in forked tests. 4 | 5 | ## Plugin dependency 6 | 7 | Add the plugin to your `project/plugins.sbt`: 8 | 9 | ```scala 10 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % versionNumber) 11 | // Until version 0.1.6 12 | addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.6") 13 | ``` 14 | 15 | See [sbt-javaagent releases] for a list of released versions. 16 | 17 | [sbt-javaagent releases]: https://github.com/sbt/sbt-javaagent/releases 18 | 19 | ## Java agent 20 | 21 | To add a Java agent to an [sbt-native-packager] distribution, enable the `JavaAgent` plugin on a project that also has `JavaAppPackaging` enabled, and then add the agent dependency using the `javaAgents` setting. For example: 22 | 23 | ```scala 24 | lazy val distProject = project 25 | .in(file("somewhere")) 26 | .enablePlugins(JavaAgent, JavaAppPackaging) 27 | .settings( 28 | javaAgents += "com.example" % "agent" % "1.2.3" 29 | ) 30 | ``` 31 | 32 | This will automatically resolve the agent module, bundle the agent artifact in the distribution, and add a `-javaagent` option to the start script. 33 | 34 | > **Note**: sbt-javaagent has a dynamic dependency on [sbt-native-packager]. You need to add sbt-native-packager separately. 35 | 36 | ## Scopes 37 | 38 | By default, sbt-javaagent will only add an agent to distributions. Agents can be optionally enabled for compile, run, or test. 39 | 40 | The following scopes are supported: 41 | 42 | * **dist** — bundle the agent in production distributions and add a `-javaagent` option to start scripts 43 | * **compile** — add the agent as a `provided` dependency so that it's available on the compile classpath 44 | * **runtime** — automatically fork the run and add a `-javaagent` option 45 | * **test** — automatically fork tests and add a `-javaagent` option 46 | 47 | The plugin can derive these scopes from module configurations. 48 | 49 | For example, to add an agent to *compile*, to build against an API provided by an agent, use the `compile` or `provided` configuration: 50 | 51 | ```scala 52 | javaAgents += "com.example" % "agent" % "1.2.3" % "compile" 53 | ``` 54 | 55 | Marking a dependency for *compile* will also automatically enable the agent for *run* as well. 56 | 57 | To enable for run or tests, use the `runtime` or `test` configurations. 58 | 59 | Multiple configurations can be specified. For example, the following will enable both *compile* and *test* (and implicitly *run*): 60 | 61 | ```scala 62 | javaAgents += "com.example" % "agent" % "1.2.3" % "compile;test" 63 | ``` 64 | 65 | Note that in this case, the agent dependency is actually added under the `provided` configuration, so that a project can compile against the agent and then have the agent provided at runtime using a `-javaagent` option. 66 | 67 | If the *compile* scope is not enabled, then the agent dependency is put under a special `javaagent` configuration so that it doesn't appear as a regular library dependency or on build classpaths. 68 | 69 | ## Agent arguments 70 | 71 | A Java agent can have an extra argument string added to it that is provided to the `premain` method in the agent. To add an argument string simply provide it to the `JavaAgent` constructor. 72 | 73 | ```scala 74 | javaAgents += JavaAgent("com.example" % "agent" % "1.2.3" % "compile;test", arguments = "java_agent_argument_string") 75 | ``` 76 | 77 | [sbt-native-packager]: https://github.com/sbt/sbt-native-packager 78 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2017 Lightbend, Inc. 3 | */ 4 | 5 | // sbt cross build 6 | crossSbtVersions := Seq("1.11.1") 7 | 8 | // dependencies 9 | val packagerVersion = "1.10.4" 10 | val packager19xVersion = "1.9.16" 11 | 12 | addSbtPlugin( 13 | "com.github.sbt" % "sbt-native-packager" % packagerVersion % "provided" 14 | ) 15 | 16 | // compile settings 17 | scalacOptions ++= Seq( 18 | "-encoding", 19 | "UTF-8", 20 | "-unchecked", 21 | "-deprecation", 22 | "-feature" 23 | ) 24 | javacOptions ++= Seq("-encoding", "UTF-8") 25 | 26 | // test agent 27 | lazy val maxwell = project 28 | .in(file("maxwell")) 29 | .settings( 30 | name := "maxwell", 31 | organization := "sbt.javaagent.test", 32 | autoScalaLibrary := false, 33 | crossPaths := false, 34 | packageOptions += Package 35 | .ManifestAttributes("Premain-Class" -> "maxwell.Maxwell"), 36 | publish := {} 37 | ) 38 | 39 | // plugin module 40 | lazy val `sbt-javaagent` = (project.in(file("."))) 41 | .enablePlugins(SbtPlugin) 42 | .settings( 43 | name := "sbt-javaagent", 44 | organization := "com.github.sbt", 45 | scriptedBufferLog := false, 46 | scriptedLaunchOpts ++= Seq( 47 | "-Dproject.version=" + version.value, 48 | "-Dpackager.version=" + packagerVersion, 49 | "-Dpackager.19x.version=" + packager19xVersion 50 | ), 51 | scriptedDependencies := { 52 | (maxwell / publishLocal).value 53 | publishLocal.value 54 | } 55 | ) 56 | 57 | // publish settings 58 | licenses += "Apache-2.0" -> url( 59 | "http://www.apache.org/licenses/LICENSE-2.0.html" 60 | ) 61 | scmInfo := Some( 62 | ScmInfo( 63 | url("https://github.com/sbt/sbt-javaagent"), 64 | "scm:git:git@github.com:sbt/sbt-javaagent.git" 65 | ) 66 | ) 67 | homepage := scmInfo.value.map(_.browseUrl) 68 | developers := List( 69 | Developer( 70 | "contributors", 71 | "Contributors", 72 | "https://github.com/sbt/sbt-javaagent/discussions", 73 | url("https://github.com/sbt/sbt-javaagent/graphs/contributors") 74 | ) 75 | ) 76 | -------------------------------------------------------------------------------- /maxwell/src/main/java/maxwell/Maxwell.java: -------------------------------------------------------------------------------- 1 | package maxwell; 2 | 3 | import java.lang.instrument.Instrumentation; 4 | import java.util.regex.Pattern; 5 | 6 | public class Maxwell { 7 | public static void premain(String agentArgs, Instrumentation instrumentation) { 8 | String[] output = new String[] { "Agent 86" }; 9 | if (agentArgs != null) { 10 | output = agentArgs.split(Pattern.quote(";")); 11 | } 12 | for (String string: output) 13 | System.out.println(string); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.11.1 2 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2017 Lightbend, Inc. 3 | */ 4 | 5 | addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.11.1") 6 | -------------------------------------------------------------------------------- /src/main/scala-sbt-0.13/com/lightbend/sbt/javaagent/Dependencies.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2023 Lightbend, Inc. 3 | */ 4 | 5 | package com.lightbend.sbt.javaagent 6 | 7 | object Dependencies { 8 | // no bash script plugin dependency for sbt 0.13 9 | val BashStartScriptPlugin = "sbt.plugins.JvmPlugin" 10 | } 11 | -------------------------------------------------------------------------------- /src/main/scala-sbt-0.13/com/lightbend/sbt/javaagent/Modules.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2017 Lightbend, Inc. 3 | */ 4 | 5 | package com.lightbend.sbt.javaagent 6 | 7 | import sbt.{ ModuleFilter, ModuleID } 8 | 9 | object Modules { 10 | def withConfigurations(module: ModuleID, configurations: Option[String]): ModuleID = 11 | module.copy(configurations = configurations) 12 | 13 | def exactFilter(module: ModuleID) = new ModuleFilter { 14 | def apply(m: ModuleID) = { 15 | m.organization == module.organization && 16 | m.name == module.name && 17 | m.revision == module.revision 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/scala-sbt-1.0/com/lightbend/sbt/javaagent/Dependencies.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2023 Lightbend, Inc. 3 | */ 4 | 5 | package com.lightbend.sbt.javaagent 6 | 7 | object Dependencies { 8 | val BashStartScriptPlugin = "com.typesafe.sbt.packager.archetypes.scripts.BashStartScriptPlugin" 9 | } 10 | -------------------------------------------------------------------------------- /src/main/scala-sbt-1.0/com/lightbend/sbt/javaagent/Modules.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2017 Lightbend, Inc. 3 | */ 4 | 5 | package com.lightbend.sbt.javaagent 6 | 7 | import sbt.ModuleID 8 | import sbt.librarymanagement.ModuleFilter 9 | 10 | object Modules { 11 | def withConfigurations(module: ModuleID, configurations: Option[String]): ModuleID = 12 | module.withConfigurations(configurations) 13 | 14 | def exactFilter(module: ModuleID) = new ModuleFilter { 15 | def apply(m: ModuleID) = { 16 | m.organization == module.organization && 17 | m.name == module.name && 18 | m.revision == module.revision 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/scala/com/lightbend/sbt/javaagent/JavaAgent.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2017 Lightbend, Inc. 3 | */ 4 | 5 | package com.lightbend.sbt.javaagent 6 | 7 | import sbt._ 8 | import sbt.Keys._ 9 | 10 | /** 11 | * Plugin for adding Java agents to projects in a general way. 12 | * Supports agents as compile-time dependencies, in forked tests, in forked run, 13 | * and in `sbt-native-packager` dists through the `JavaAgentPackaging` plugin. 14 | */ 15 | object JavaAgent extends JavaAgent { 16 | val autoImport = JavaAgentKeys 17 | 18 | object JavaAgentKeys { 19 | val javaAgents = settingKey[Seq[AgentModule]]("Java agent modules enabled for this project.") 20 | val resolvedJavaAgents = taskKey[Seq[ResolvedAgent]]("Java agent modules with resolved artifacts.") 21 | } 22 | 23 | val AgentConfig = config("javaagent").hide 24 | 25 | case class AgentScope(compile: Boolean = false, test: Boolean = false, run: Boolean = false, dist: Boolean = true) 26 | 27 | case class AgentModule(name: String, module: ModuleID, scope: AgentScope, arguments: String) 28 | 29 | case class ResolvedAgent(agent: AgentModule, artifact: File) 30 | 31 | object AgentModule { 32 | import scala.language.implicitConversions 33 | implicit def moduleToAgentModule(module: ModuleID): AgentModule = JavaAgent(module) 34 | } 35 | 36 | /** 37 | * Create an agent module from a module dependency. 38 | * Scope is also derived from the given module configuration. 39 | */ 40 | def apply(module: ModuleID, name: String = null, scope: AgentScope = AgentScope(), arguments: String = null): AgentModule = { 41 | val agentName = Option(name).getOrElse(module.name) 42 | val agentArguments = Option(arguments).map("=" + _).getOrElse("") 43 | val confs = module.configurations.toSeq.flatMap(_.split(";")) 44 | val inCompile = scope.compile || confs.contains(Compile.name) || confs.contains(Provided.name) 45 | val inRun = scope.run || inCompile || confs.contains(Runtime.name) 46 | val inTest = scope.test || confs.contains(Test.name) 47 | val inDist = scope.dist 48 | val configuration = if (inCompile) Provided else AgentConfig 49 | val reconfiguredModule = Modules.withConfigurations(module, Some(configuration.name)) 50 | val configuredScope = AgentScope(compile = inCompile, test = inTest, run = inRun, dist = inDist) 51 | AgentModule(agentName, reconfiguredModule, configuredScope, agentArguments) 52 | } 53 | } 54 | 55 | class JavaAgent extends AutoPlugin { 56 | import JavaAgent._ 57 | import JavaAgent.JavaAgentKeys._ 58 | 59 | override def requires: Plugins = plugins.JvmPlugin 60 | 61 | override def projectSettings = Seq( 62 | javaAgents := Seq.empty, 63 | ivyConfigurations += AgentConfig, 64 | libraryDependencies ++= javaAgents.value.map(_.module), 65 | resolvedJavaAgents := resolveAgents.value, 66 | run/fork := enableFork(run/fork, _.scope.run).value, 67 | run/connectInput := enableFork(run/fork, _.scope.run).value, 68 | Test/fork := enableFork(Test/fork, _.scope.test).value, 69 | run/javaOptions ++= agentOptions(_.agent.scope.run).value, 70 | Test/javaOptions ++= agentOptions(_.agent.scope.test).value, 71 | Test/fullClasspath := filterAgents((Test/fullClasspath).value, resolvedJavaAgents.value) 72 | ) 73 | 74 | private def resolveAgents = Def.task[Seq[ResolvedAgent]] { 75 | val missingAgentDependencies = javaAgents.value.map(_.module).filterNot(libraryDependencies.value.contains) 76 | if (missingAgentDependencies.nonEmpty) { 77 | sys.error( 78 | s"Some agents missing from libraryDependencies. " + 79 | s"It might mean you override libraryDependencies with := instead of adding new ones with ++=. " + 80 | s"Missing agents: $missingAgentDependencies" 81 | ) 82 | } 83 | val resolvedAgents = javaAgents.value flatMap { agent => 84 | update.value.matching(Modules.exactFilter(agent.module)).headOption map { 85 | jar => ResolvedAgent(agent, jar) 86 | } 87 | } 88 | val unresolvedAgents = javaAgents.value.filterNot(agent => resolvedAgents.map(_.agent.module).contains(agent.module)) 89 | if (unresolvedAgents.nonEmpty) 90 | sys.error(s"Unable to resolve agents, missing agents: $unresolvedAgents") 91 | else resolvedAgents 92 | } 93 | 94 | private def enableFork(forkKey: SettingKey[Boolean], enabled: AgentModule => Boolean) = Def.setting[Boolean] { 95 | forkKey.value || javaAgents.value.exists(enabled) 96 | } 97 | 98 | private def agentOptions(enabled: ResolvedAgent => Boolean) = Def.task[Seq[String]] { 99 | resolvedJavaAgents.value filter enabled map { resolved => 100 | "-javaagent:" + resolved.artifact.absolutePath + resolved.agent.arguments 101 | } 102 | } 103 | 104 | def filterAgents(classpath: Classpath, resolvedAgents: Seq[ResolvedAgent]): Classpath = { 105 | val agents = resolvedAgents.map(resolved => resolved.artifact.absolutePath) 106 | classpath.filter(aFile => !agents.contains(aFile.data.getAbsolutePath)) 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/scala/com/lightbend/sbt/javaagent/JavaAgentPackaging.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2016-2017 Lightbend, Inc. 3 | */ 4 | 5 | package com.lightbend.sbt.javaagent 6 | 7 | import sbt._ 8 | import sbt.Keys._ 9 | import com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.Universal 10 | import java.io.File 11 | 12 | /** 13 | * Plugin for adding Java agents to `sbt-native-packager` distributions. 14 | */ 15 | object JavaAgentPackaging extends AutoPlugin { 16 | import JavaAgent.JavaAgentKeys._ 17 | 18 | override def trigger = allRequirements 19 | 20 | override def requires = JavaAgent && 21 | PluginRef("com.typesafe.sbt.packager.archetypes.JavaAppPackaging") && 22 | PluginRef(Dependencies.BashStartScriptPlugin) 23 | 24 | override def projectSettings = { 25 | import com.typesafe.sbt.packager.{ Keys => PackagerKeys } 26 | Seq( 27 | Universal / mappings ++= agentMappings.value.map(m => m._1 -> m._2), 28 | PackagerKeys.bashScriptExtraDefines ++= agentBashScriptOptions.value, 29 | PackagerKeys.batScriptExtraDefines ++= agentBatScriptOptions.value 30 | ) 31 | } 32 | 33 | private def agentMappings = Def.task[Seq[(File, String, String)]] { 34 | resolvedJavaAgents.value filter (_.agent.scope.dist) map { resolved => 35 | (resolved.artifact, 36 | Project.normalizeModuleID(resolved.agent.name) + File.separator + resolved.artifact.name, 37 | resolved.agent.arguments) 38 | } 39 | } 40 | 41 | private def agentBashScriptOptions = Def.task[Seq[String]] { 42 | agentMappings.value map { 43 | case (_, path, arguments) => s"""addJava "-javaagent:$${app_home}/../${normalizeBashPath(path)}$arguments" """ 44 | } 45 | } 46 | 47 | private def normalizeBashPath(path: String, separator: Char = File.separatorChar): String = 48 | normalizePath(path, '/') 49 | 50 | private def agentBatScriptOptions = Def.task[Seq[String]] { 51 | agentMappings.value map { 52 | case (_, path, arguments) => s"""set _JAVA_OPTS=-javaagent:%@@APP_ENV_NAME@@_HOME%\\${normalizeBatPath(path)}$arguments %_JAVA_OPTS%""" 53 | } 54 | } 55 | 56 | private def normalizeBatPath(path: String, separator: Char = File.separatorChar): String = 57 | normalizePath(path, '\\') 58 | 59 | private def normalizePath(path: String, expected: Char, actual: Char = File.separatorChar): String = { 60 | if (actual == expected) path else path.replace(actual, expected) 61 | } 62 | 63 | /** 64 | * Reflectively load an auto plugin if it's on the classpath, otherwise 65 | * return an empty plugin that won't trigger all-requirements loading. 66 | * See also: https://github.com/sbt/sbt/issues/2538 67 | */ 68 | private def PluginRef(className: String): AutoPlugin = { 69 | try { 70 | val name = if (className.endsWith("$")) className else (className + "$") 71 | Class.forName(name).asInstanceOf[Class[AutoPlugin]] 72 | .getDeclaredField("MODULE$").get(null) match { 73 | case plugin: AutoPlugin => plugin 74 | case _ => throw new RuntimeException(s"[$name] is not an AutoPlugin object") 75 | } 76 | } catch { 77 | case _: Throwable => NoPlugin 78 | } 79 | } 80 | 81 | } 82 | 83 | object NoPlugin extends AutoPlugin 84 | -------------------------------------------------------------------------------- /src/sbt-test/agent/arguments/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentOptions = 2 | project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging) 3 | 4 | javaAgents += JavaAgent( 5 | module = "sbt.javaagent.test" % "maxwell" % sys.props( 6 | "project.version" 7 | ) % "dist;test", 8 | arguments = "Get_Smart;Agent_99" 9 | ) 10 | -------------------------------------------------------------------------------- /src/sbt-test/agent/arguments/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/arguments/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/arguments/test: -------------------------------------------------------------------------------- 1 | > stage 2 | > check 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/arguments/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | assert( 3 | (Test / fork).value, 4 | "Test / fork is not enabled" 5 | ) 6 | 7 | assert( 8 | (Test / javaOptions).value exists (s => 9 | s.contains("-javaagent:") && s.contains("maxwell") 10 | ), 11 | "Test / javaOptions do not contain 'maxwell' agent" 12 | ) 13 | 14 | assert( 15 | (Test / javaOptions).value exists (s => 16 | s.contains("-javaagent:") && s.contains("maxwell") && s.contains( 17 | "=Get_Smart;Agent_99" 18 | ) 19 | ), 20 | "Test / javaOptions do not contain 'Get_Smart;Agent_99' agent arguments" 21 | ) 22 | 23 | assert( 24 | (Universal / mappings).value exists { case (file, path) => 25 | path == "maxwell/maxwell.jar" 26 | }, 27 | "dist mappings do not include 'maxwell/maxwell.jar'" 28 | ) 29 | 30 | import scala.sys.process._ 31 | val output = 32 | ((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!! 33 | 34 | assert( 35 | !(output contains "Agent 86"), 36 | "output includes 'Agent 86'" 37 | ) 38 | assert( 39 | !(output contains ";"), 40 | "output includes ';'" 41 | ) 42 | assert( 43 | output contains "Get_Smart", 44 | "output does not include 'Get_Smart'" 45 | ) 46 | assert( 47 | output contains "Agent_99", 48 | "output does not include 'Agent_99'" 49 | ) 50 | } 51 | -------------------------------------------------------------------------------- /src/sbt-test/agent/compile/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentCompile = 2 | project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging) 3 | 4 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props( 5 | "project.version" 6 | ) % "compile" 7 | -------------------------------------------------------------------------------- /src/sbt-test/agent/compile/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/compile/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App { 2 | println(classOf[maxwell.Maxwell]) 3 | } 4 | -------------------------------------------------------------------------------- /src/sbt-test/agent/compile/test: -------------------------------------------------------------------------------- 1 | > set outputStrategy := Some(CustomOutput(new java.io.FileOutputStream(file("run.log")))) 2 | > checkDependency 3 | > compile 4 | > run 5 | > checkLog 6 | > stage 7 | > checkDist 8 | > checkTestAndRunPaths 9 | -------------------------------------------------------------------------------- /src/sbt-test/agent/compile/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("checkDependency") := { 2 | assert( 3 | libraryDependencies.value contains ("sbt.javaagent.test" % "maxwell" % sys 4 | .props("project.version") % "provided"), 5 | "maxwell test agent is not in libraryDependencies under 'provided'" 6 | ) 7 | } 8 | 9 | def expect(name: String, contents: String, expected: String): Unit = 10 | assert(contents contains expected, s"$name should contain '$expected'") 11 | 12 | TaskKey[Unit]("checkLog") := { 13 | val log = IO.read(file("run.log")) 14 | expect("run log", log, "Agent 86") 15 | expect("run log", log, "class maxwell.Maxwell") 16 | } 17 | 18 | TaskKey[Unit]("checkDist") := { 19 | import scala.sys.process._ 20 | val output = 21 | ((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!! 22 | expect("dist run", output, "Agent 86") 23 | expect("dist run", output, "class maxwell.Maxwell") 24 | } 25 | 26 | TaskKey[Unit]("checkTestAndRunPaths") := { 27 | assert( 28 | !((Runtime / dependencyClasspath).value exists (f => 29 | f.data.name.contains("maxwell") 30 | )), 31 | "maxwell test agent is available on the runtime class path" 32 | ) 33 | assert( 34 | (Test / dependencyClasspath).value exists (f => 35 | f.data.name.contains("maxwell") 36 | ), 37 | "maxwell test agent is not available on the test compile class path" 38 | ) 39 | assert( 40 | !((Test / fullClasspath).value exists (f => 41 | f.data.name.contains("maxwell") 42 | )), 43 | "maxwell test agent is available on the test run class path" 44 | ) 45 | } 46 | -------------------------------------------------------------------------------- /src/sbt-test/agent/confs/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentConfs = project.in(file(".")).enablePlugins(JavaAgent) 2 | 3 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props("project.version") % "compile;test" 4 | 5 | libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test" 6 | -------------------------------------------------------------------------------- /src/sbt-test/agent/confs/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/confs/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App { 2 | println(classOf[maxwell.Maxwell]) 3 | } 4 | -------------------------------------------------------------------------------- /src/sbt-test/agent/confs/src/test/scala/Test.scala: -------------------------------------------------------------------------------- 1 | class Test { 2 | @org.junit.Test def test(): Unit = { 3 | println(classOf[maxwell.Maxwell]) 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /src/sbt-test/agent/confs/test: -------------------------------------------------------------------------------- 1 | > set outputStrategy := Some(CustomOutput(new java.io.FileOutputStream(file("run.log")))) 2 | > run 3 | > checkRunLog 4 | > set outputStrategy := Some(CustomOutput(new java.io.FileOutputStream(file("test.log")))) 5 | > test 6 | > checkTestLog 7 | -------------------------------------------------------------------------------- /src/sbt-test/agent/confs/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("checkRunLog") := checkLog("run.log") 2 | TaskKey[Unit]("checkTestLog") := checkLog("test.log") 3 | 4 | def checkLog(logFile: String): Unit = { 5 | val log = IO.read(file(logFile)) 6 | 7 | def expect(expected: String): Unit = { 8 | assert(log contains expected, s"log should contain '$expected'") 9 | } 10 | 11 | expect("Agent 86") 12 | expect("class maxwell.Maxwell") 13 | } 14 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentDist = 2 | project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging) 3 | 4 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props("project.version") 5 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist/test: -------------------------------------------------------------------------------- 1 | > stage 2 | > check 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | assert( 3 | (Universal / mappings).value exists { case (file, path) => 4 | path == "maxwell/maxwell.jar" 5 | }, 6 | "dist mappings do not include 'maxwell/maxwell.jar'" 7 | ) 8 | 9 | import scala.sys.process._ 10 | val output = 11 | ((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!! 12 | 13 | assert( 14 | output contains "Agent 86", 15 | "output does not include 'Agent 86'" 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist_1.9.x/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentDist = 2 | project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging) 3 | 4 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props("project.version") 5 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist_1.9.x/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.19x.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist_1.9.x/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist_1.9.x/test: -------------------------------------------------------------------------------- 1 | > stage 2 | > check 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/dist_1.9.x/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | assert( 3 | (Universal / mappings).value exists { case (file, path) => 4 | path == "maxwell/maxwell.jar" 5 | }, 6 | "dist mappings do not include 'maxwell/maxwell.jar'" 7 | ) 8 | 9 | import scala.sys.process._ 10 | val output = 11 | ((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!! 12 | 13 | assert( 14 | output contains "Agent 86", 15 | "output does not include 'Agent 86'" 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /src/sbt-test/agent/named/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentNamed = 2 | project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging) 3 | 4 | javaAgents += JavaAgent( 5 | name = "Get Smart", 6 | module = "sbt.javaagent.test" % "maxwell" % sys.props("project.version") 7 | ) 8 | -------------------------------------------------------------------------------- /src/sbt-test/agent/named/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/named/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/named/test: -------------------------------------------------------------------------------- 1 | > stage 2 | > check 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/named/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | assert( 3 | (Universal / mappings).value exists { case (file, path) => 4 | path == "get-smart/maxwell.jar" 5 | }, 6 | "dist mappings do not include 'get-smart/maxwell.jar'" 7 | ) 8 | 9 | import scala.sys.process._ 10 | val output = 11 | ((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!! 12 | 13 | assert( 14 | output contains "Agent 86", 15 | "output does not include 'Agent 86'" 16 | ) 17 | } 18 | -------------------------------------------------------------------------------- /src/sbt-test/agent/provided/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentProvided = 2 | project.in(file(".")).enablePlugins(JavaAgent, JavaAppPackaging) 3 | 4 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props( 5 | "project.version" 6 | ) % "provided" 7 | -------------------------------------------------------------------------------- /src/sbt-test/agent/provided/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | addSbtPlugin("com.github.sbt" % "sbt-native-packager" % sys.props("packager.version")) 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/provided/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App { 2 | println(classOf[maxwell.Maxwell]) 3 | } 4 | -------------------------------------------------------------------------------- /src/sbt-test/agent/provided/test: -------------------------------------------------------------------------------- 1 | > set outputStrategy := Some(CustomOutput(new java.io.FileOutputStream(file("run.log")))) 2 | > compile 3 | > stage 4 | > run 5 | > check 6 | -------------------------------------------------------------------------------- /src/sbt-test/agent/provided/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | def expect(name: String, contents: String, expected: String): Unit = 3 | assert(contents contains expected, s"$name should contain '$expected'") 4 | 5 | val log = IO.read(file("run.log")) 6 | 7 | expect("run log", log, "Agent 86") 8 | expect("run log", log, "class maxwell.Maxwell") 9 | 10 | import scala.sys.process._ 11 | val output = 12 | ((Universal / com.typesafe.sbt.packager.universal.UniversalPlugin.autoImport.stagingDirectory).value / "bin" / packageName.value).absolutePath.!! 13 | 14 | expect("dist run", output, "Agent 86") 15 | expect("dist run", output, "class maxwell.Maxwell") 16 | } 17 | -------------------------------------------------------------------------------- /src/sbt-test/agent/run/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentRun = project.in(file(".")).enablePlugins(JavaAgent) 2 | 3 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props( 4 | "project.version" 5 | ) % "runtime" 6 | -------------------------------------------------------------------------------- /src/sbt-test/agent/run/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/run/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | object Main extends App 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/run/test: -------------------------------------------------------------------------------- 1 | > set outputStrategy := Some(CustomOutput(new java.io.FileOutputStream(file("run.log")))) 2 | > run 3 | > check 4 | -------------------------------------------------------------------------------- /src/sbt-test/agent/run/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | assert( 3 | libraryDependencies.value contains ("sbt.javaagent.test" % "maxwell" % sys 4 | .props("project.version") % "javaagent"), 5 | "maxwell test agent is not in libraryDependencies under 'javaagent'" 6 | ) 7 | 8 | assert( 9 | (run / fork).value, 10 | "run / fork is not enabled" 11 | ) 12 | 13 | assert( 14 | (run / javaOptions).value exists (s => 15 | s.contains("-javaagent:") && s.contains("maxwell") 16 | ), 17 | "run / javaOptions do not contain 'maxwell' agent" 18 | ) 19 | 20 | val runLog = IO.read(file("run.log")) 21 | 22 | assert( 23 | runLog contains "Agent 86", 24 | "run log does not include 'Agent 86'" 25 | ) 26 | } 27 | -------------------------------------------------------------------------------- /src/sbt-test/agent/test/build.sbt: -------------------------------------------------------------------------------- 1 | lazy val agentTest = project.in(file(".")).enablePlugins(JavaAgent) 2 | 3 | javaAgents += "sbt.javaagent.test" % "maxwell" % sys.props( 4 | "project.version" 5 | ) % "test" 6 | 7 | libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test" 8 | 9 | // send test stdout to log — works but should be easier? 10 | // outputStrategy is a setting, so it can't access tasks like streams 11 | Test / testGrouping := { 12 | (Test / testGrouping).value map { 13 | case group @ Tests.Group(_, _, Tests.SubProcess(forkOptions)) => 14 | group.copy(runPolicy = 15 | Tests.SubProcess( 16 | copyForkOptions(forkOptions, LoggedOutput(streams.value.log)) 17 | ) 18 | ) 19 | case group => group 20 | } 21 | } 22 | 23 | // copy manually to be compatible with both sbt 0.13 (case class) and sbt 1.0 (contraband) 24 | def copyForkOptions( 25 | o: ForkOptions, 26 | newOutputStrategy: OutputStrategy 27 | ): ForkOptions = { 28 | ForkOptions( 29 | o.javaHome, 30 | Some(newOutputStrategy), 31 | o.bootJars, 32 | o.workingDirectory, 33 | o.runJVMOptions, 34 | o.connectInput, 35 | o.envVars 36 | ) 37 | } 38 | -------------------------------------------------------------------------------- /src/sbt-test/agent/test/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.github.sbt" % "sbt-javaagent" % sys.props("project.version")) 2 | -------------------------------------------------------------------------------- /src/sbt-test/agent/test/src/test/scala/Test.scala: -------------------------------------------------------------------------------- 1 | class Test { 2 | @org.junit.Test def test(): Unit = () 3 | } 4 | -------------------------------------------------------------------------------- /src/sbt-test/agent/test/test: -------------------------------------------------------------------------------- 1 | > test 2 | > check 3 | -------------------------------------------------------------------------------- /src/sbt-test/agent/test/test.sbt: -------------------------------------------------------------------------------- 1 | TaskKey[Unit]("check") := { 2 | assert( 3 | (Test / fork).value, 4 | "Test / fork is not enabled" 5 | ) 6 | 7 | assert( 8 | (Test / javaOptions).value exists (s => 9 | s.contains("-javaagent:") && s.contains("maxwell") 10 | ), 11 | "Test / javaOptions do not contain 'maxwell' agent" 12 | ) 13 | 14 | assert( 15 | !((Test / fullClasspath).value exists (f => 16 | f.data.name.contains("maxwell") 17 | )), 18 | "maxwell test agent is available on the test run class path" 19 | ) 20 | 21 | val testLog = IO.read(BuiltinCommands.lastLogFile(state.value).get) 22 | 23 | assert( 24 | testLog contains "Agent 86", 25 | "test log does not include 'Agent 86'" 26 | ) 27 | } 28 | --------------------------------------------------------------------------------