编译后SBT在项目中运行代码(SBT run code in project after compile)

编程入门 行业动态 更新时间:2024-10-27 12:33:57
编译后SBT在项目中运行代码(SBT run code in project after compile)

我们需要在编译步骤之后运行一些代码。 在编译步骤之后使事情变得容易:

compile in Compile <<= (compile in Compile) map{x=> // post-compile work doFoo() x }

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

关于场景的更多信息:我们在电梯项目中使用较少的css。 我们希望电梯能够在运行时(如果需要)更少地编译成css来帮助dev,但是在构建期间,在测试等运行之前,使用相同的代码产生更少的代码。 less-sbt可能有所帮助,但我们对如何解决这个问题感兴趣。

we need to run some code after the compile step. Making things happen after the compile step seems easy:

compile in Compile <<= (compile in Compile) map{x=> // post-compile work doFoo() x }

but how do you run something in the freshly compiled code?

More info on the scenario: we are using less for css in a lift project. We wanted lift to compile less into css on the fly (if needed) to help dev, but produce less using the same code, during the build, before tests etc run. less-sbt may help but we are interested in how to solve this generally.

最满意答案

您可以像这样使用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)

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

You can use the triggeredBy method like this:

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)

This will instantiate your class that has just been compiled, using the runtime classpath for your application, after any compile.

更多推荐

本文发布于:2023-07-17 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   项目   SBT   run   compile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!