gpt4 book ai didi

scala - 如何编写 sbt 插件以通过代理启动应用程序

转载 作者:行者123 更新时间:2023-12-04 15:44:21 25 4
gpt4 key购买 nike

我想在开源之前为我的项目创建一个 sbt 插件。

该项目将一个 Java 代理附加到运行应用程序的开始,以对其进行各种类型的分析。代理写出文本文件供以后处理。

我希望能够编写一个可以

  • 有一个替代 runrunWithProfiling它启动一个新的 java 进程,将代理添加到参数列表中,并传递所有用户命令。
  • 退出时,我想调用一些任意的后处理代码来生成 HTML 报告

  • 我大致知道如何创建新命令,但我不知道如何最好地实现 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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com