One of the things I like about SBT is that it's interactive. SBT stays up as a long running process, and you interact with it many times, while it manages your project and compiles code for you.
Because SBT is interactive and runs on the JVM, you can use it for more than just builds. You can use it for communication. Specifically, you can use it to make HTTP requests out to things you're interested in communicating with.
Unfortunately, I knew very little about SBT plugins. So, I talked to Christopher Hunt and Josh Suereth, downloaded eigengo's sbt-mdrw project, read the activator blog post on markdown and then worked it out on the plane back from Germany.
I made a 0.13 SBT plugin that uses the ROME RSS library to display titles from a list of RSS feeds. It's available from https://github.com/wsargent/sbt-rss and has lots of comments.
The SBT RSS plugin adds a single command to SBT. You type rss at the console, and it displays the feed:
Let's show how it does that.
First, the build file. This looks like a normal build.sbt file, except that there's a sbtPlugin setting in it:
Next, there's the Plugin scala code itself.
So, the first thing to note is the AutoPlugin class. The Plugins page talks about AutoPlugin – all you really need to know is if you define an autoImport object with your setting keys and then import it into an AutoPlugin, you will make the settingKey available to SBT.
The next bit is the globalSettings entry:
Here, we're saying we're going to add a command to SBT's global settings, by merging it with super.globalSettings.
The next two bits detail how to create the RSS command in SBT style.
Finally, there's the command itself.
The first thing we need to do within a command is call Project.extract(state). This gives us a bunch of useful settings such as currentRef, which we can use to pull the value of the SettingKey out. The SBT documentation on Build State - Project related data shows some more examples:
Once we have the extracted.currentRef object, we can pull out the list of URLs with this construct, where the documentation is from Build State - Project data:
And then we can put that together with the ROME library to print something out.
This is an intentionally trivial example, but it's easy to show how you could use this to check if the build failed, for example. Have fun.