编译后SBT在项目中运行代码

Cha*_*ton 5 sbt

我们需要在编译步骤之后运行一些代码。在编译步骤之后让事情发生似乎很容易:

compile in Compile <<= (compile in Compile) map{x=>
    // post-compile work
    doFoo()
    x
}
Run Code Online (Sandbox Code Playgroud)

但是你如何在新编译的代码中运行一些东西呢?

关于场景的更多信息:我们在提升项目中使用较少的 css。我们希望提升在运行中(如果需要)将较少的编译成 css 以帮助开发人员,但在构建期间、测试等运行之前使用相同的代码生成较少的代码。less-sbt 可能会有所帮助,但我们对如何解决这个问题很感兴趣。

Ray*_*low 4

您可以像这样使用triggeredBy方法:

yourTask <<= (fullClasspath in Runtime) map {classpath =>
  val loader: ClassLoader = ClasspathUtilities.toLoader(classpath.map(_.data).map(_.getAbsoluteFile))
  loader.loadClass("your.class.Here").newInstance()
} triggeredBy(compile in Compile)
Run Code Online (Sandbox Code Playgroud)

在任何编译之后,这将使用应用程序的运行时类路径实例化刚刚编译的类。