我想在开源之前为我的项目创建一个 sbt 插件。
该项目将一个 Java 代理附加到运行应用程序的开始,以对其进行各种类型的分析。代理写出文本文件供以后处理。
我希望能够编写一个可以
run
叫 runWithProfiling
它启动一个新的 java 进程,将代理添加到参数列表中,并传递所有用户命令。 我大致知道如何创建新命令,但我不知道如何最好地实现
run
的替代方案...我不想通过复制 run
的所有代码来重新发明轮子做。有什么方法可以调用 run
但确保我的参数被传递(一次)并且它绝对是一个新的java进程?此外,能够为测试做同样的事情会很棒。
更新 : 这是我目前拥有的代码,但它有几个问题,标记为
TODO
秒import sbt._
import Keys._
import sbt.Attributed.data
object LionPlugin extends Plugin {
val lion = TaskKey[Unit]("lion", "Run a main class with lions-share profiling.")
override val projectSettings = Seq(
fork := true,
javaOptions ++= Seq(
"-Xloggc:gc.log", "-XX:+PrintGCDetails", "-XX:+PrintGCDateStamps",
"-XX:+PrintTenuringDistribution", "-XX:+PrintHeapAtGC"
// TODO: need to get hold of the local jar file for a particular artifact
// IMPL: pass the jar as the agent
),
lion <<= (
runner,
fullClasspath in Runtime,
mainClass in Runtime,
streams in Runtime
) map runLion
)
// TODO: update to a task that can take parameters (e.g. number of repeats, profiling settings)
def runLion(runner: ScalaRun, cp: Classpath, main: Option[String], streams: TaskStreams): Unit = {
assert(runner.isInstanceOf[ForkRun], "didn't get a forked runner... SBT is b0rk3d")
println("RUNNING with " + runner.getClass)
// TODO: ask user if main is None, like 'run' does
val m = main.getOrElse("Scratch")
// TODO: get the user's arguments
val args = Nil
runner.run(m, data(cp), args, streams.log)
// IMPL: post-process and produce the report
println("FINISHED")
}
}
最佳答案
请参阅“除了运行之外,如何创建自定义运行任务?”在 http://www.scala-sbt.org/0.13.0/docs/faq.html .在您的自定义任务中,您需要设置 fork
为 true 以启动新的 JVM。
关于scala - 如何编写 sbt 插件以通过代理启动应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23029910/