Sbt / javaAgents / force jar-with-dependencies

Sbt / javaAgents / force jar-with-dependencies

我想将 "org.kohsuke" % "file-leak-detector" % "1.13" java 代理添加到我的 sbt 配置中。

//plugins.sbt
addSbtPlugin("com.lightbend.sbt" % "sbt-javaagent" % "0.1.5")

//build.sbt
enablePlugins(SbtWeb, PlayScala, DebianPlugin, JavaAgent)
javaAgents += "org.kohsuke" % "file-leak-detector" % "1.13"

但这在 运行 时失败了。

Failed to find Premain-Class manifest attribute in /usr/share/someApp/bin/../file-leak-detector/file-leak-detector-1.13.jar
Error occurred during initialization of VM
agent library failed to init: instrument

我发现我需要依赖于 运行 的 jar 作为 javaAgent 但是如果理论上我可以从 https://repo1.maven.org/maven2/org/kohsuke/file-leak-detector/1.13/ 下载它的话 我不知道如何将它与 sbt-javaagent 插件一起使用。

我也试过:

javaAgents += ("org.kohsuke" % "file-leak-detector" % "1.13") from "https://repo1.maven.org/maven2/org/kohsuke/file-leak-detector/1.13/file-leak-detector-1.13-jar-with-dependencies.jar"

但运气不好。关于如何强制 sbt-javaagent 插件使用 file-leak-detector-1.13-jar-with-dependencies.jar 而不是 file-leak-detector-1.13-jar 的任何想法?

我认为您使用 from 提供明确的 URL

是正确的做法

If your project requires a dependency that is not present in a repository, a direct URL to its jar can be specified as follows:

libraryDependencies += "slinky" % "slinky" % "2.1" from "https://slinky2.googlecode.com/svn/artifacts/2.1/slinky.jar" 

当我像这样提供显式 scope 时,它似乎在我的机器上工作

javaAgents += JavaAgent(
  "org.kohsuke" % "file-leak-detector-1.13-jar-with-dependencies" % "1.13" % Runtime from "https://repo1.maven.org/maven2/org/kohsuke/file-leak-detector/1.13/file-leak-detector-1.13-jar-with-dependencies.jar",
  arguments = "help"
)

备注% Runtime。我认为它有效,因为 arguments = "help" 受到尊重并且 sbt run 输出

sbt:sbt-javaagents-leka> run
[info] running (fork) example.Hello
[error] File leak detector arguments (to specify multiple values, separate them by ',':
[error]   help          - show the help screen.
[error]   trace         - log every open/close operation to stderr.
[error]   trace=FILE    - log every open/close operation to the given file.
[error]   error=FILE    - if 'too many open files' error is detected, send the dump here.
[error]                   by default it goes to stderr.
[error]   threshold=N   - instead of waiting until 'too many open files', dump once
[error]                   we have N descriptors open.
[error]   http=PORT     - Run a mini HTTP server that you can access to get stats on demand
[error]                   Specify 0 to choose random available port, -1 to disable, which is default.
[error]   strong        - Don't let GC auto-close leaking file descriptors
[error]   listener=S    - Specify the fully qualified name of ActivityListener class to activate from beginning
[error]   dumpatshutdown- Dump open file handles at shutdown
[error]   excludes=FILE - Ignore files opened directly/indirectly in specific methods.
[error]                   File lists 'some.pkg.ClassName.methodName' patterns.

此外,如果我设置 arguments = "threshold=1",它会输出

[info] running (fork) example.Hello
[error] File leak detector installed
[error] 2 descriptors are open
[error] #1 /private/var/folders/y7/33z7gnqn1nl2vwzn26j59xy9rwcgwx/T/sbt_412c7765/target/edca7f33/f7a642d3/scala-library-2.13.1.jar by thread:main on Mon Apr 20 11:51:20 BST 2020
...