使用项目中的类的 sbt 自定义任务 [英] sbt custom task using a class in project

查看:31
本文介绍了使用项目中的类的 sbt 自定义任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向使用(使用类、运行方法等)项目源的 sbt 构建定义添加自定义任务?似乎它甚至在编译之前就试图找到它们.

How can I add a custom task to sbt build definition that consumes (uses classes, runs methods etc.) the project sources ? It seems it tries to find them before compiling even.

推荐答案

我需要知道您为什么要调用这些方法,因为这会改变答案.如果你想做点什么...

I need to know why you want to call the methods, since this changes the answer. If you want to do something ...

想要使用类/方法来做一些与构建相关的事情(最小化的东西,上传 jar/wars 到服务器等.)

Want to use class/ methods which do something build related ( minfiy things, uploaded jar/wars to servers, etc..)

  • 你必须把它放在一个插件中,或者你需要把源放在一个项目文件夹中.
  • 无法从您的项目中调用代码

如果它与构建相关,那么可能有人处理过类似的问题,并且可能已经有一个 sbt 插件,但如果没有,请告诉我,我可以解释如何创建 sbt 插件.

If it is build related, someone has probably dealt with a similar problem and there is probably a sbt plugin already, but if not, then let me know and I can explain creating sbt plugins.

只想调用/测试与构建周期无关的方法.

Want to just call / test methods which don't have anything to do with the build cycle.

  • 您可以将它放在项目中名为 Script 的对象中(随便命名),启动控制台,然后导入脚本对象并运行.
  • 为了使这更容易,您可以为控制台创建一个自定义导入脚本,该脚本会自动导入所有脚本,然后您可以运行该脚本

例如,打包脚本

object Script {
    def foo = println("I am doing things non-build related")
}

在 sbt 现在运行

console
>> import script._
>> foo    // prints out  "I am doing things non-build related"

这篇关于使用项目中的类的 sbt 自定义任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆