How to write a SBT 0.10+ plugin?
fujohnwang
author: fujohnwang
To draft a SBT 0.10+ plugin, TWO parts should be taken into consideration:
the build file of the plugin;
the source code file or the definition files of the plugin;
the build file of the plugin sample (build.sbt under project root)
sbtPlugin := true name := "aspectj_sbt_plugin" version := "0.0.1" organization := "name.fujohnwang" publishMavenStyle := true scalacOptions := Seq("-deprecation", "-unchecked") resolvers += "Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/" libraryDependencies ++= Seq("org.aspectj" % "aspectjtools" % "1.6.11.RELEASE", "org.aspectj" % "aspectjrt" % "1.6.11.RELEASE","org.aspectj" % "aspectjweaver" % "1.6.11.RELEASE")
the definition file of the plugin sample(src/main/scala/*.scala)
import sbt._
import Keys._
object MyPlugin extends Plugin {
val MyConfiguration = config("myconf")
val mySetting = SettingKey[String]("my-setting")
val myTask = TaskKey[Unit]("my-task", "task description")
override lazy val settings = inConfig(MyConfiguration)(Seq(
:= "initial value for my-setting",
mySetting
<<= (streams, mySetting, …) map{
myTask
(s, ms, …)=>
// do what u want to do with the arguments
},
// other settings
))
}
Note: U refer to each setting via their key and refer to their values via map from key.
Last Mile - How to use the plugin u have just finished?
in your project, 2 places should be taken care about:
1- the “project/plugins.sbt”resolvers += yourResolver // help sbt to find out where your plugin is addSbtPlugin("name.fujohnwang" % "aspectj_sbt_plugin" % "0.0.1") // declare to use your plugin
2- build.sbt under the root path of your project(light configuration) or project/Build.scala(full configuration)
usually, you can customize the settings of the plugin or add necessary dependencies in your build file(s), this is variable as per your usage scenarios. If default values are ok for you, nothing about plugin is needed to add to your build file.
What U can learn from the experience of writing a plugin?
1- declare custom Configuration to enhance the modularity;
2- declare necessary SettingKey(s) to make your plugin flexible(which allows your users to customize the plugin)
3- each Keys(Setting or Task) can be initialized or implemented by <<= with other Key(s)
References:
- https://github.com/harrah/xsbt/wiki/Plugins-Best-Practices
- https://github.com/harrah/xsbt/wiki/sbt-0.10-plugins-list
- http://eed3si9n.com/sbt-010-guide
- The AspectJ compiler API
- typesafehub-sbt-aspectj plugin
「为AI疯狂」星球上,扶墙老师正在和朋友们讨论有趣的AI话题,你要不要⼀起来呀?^-^
这里
- 不但有及时新鲜的AI资讯和深度探讨
- 还分享AI工具、产品方法和商业机会
- 更有体系化精品付费内容等着你,加入星球(https://t.zsxq.com/0dI3ZA0sL) 即可免费领取。(加入之后一定记得看置顶消息呀!)

存量的时代,省钱就是赚钱。
在增量的时代,省钱其实是亏钱。
避坑儿是省钱的一种形式,更是真正聪明人的选择!
弯路虽然也是路,但还是能少走就少走,背后都是高昂的试错成本。
订阅「福报」,少踩坑,少走弯路,多走一步,就是不一样的胜率!
