My favorites | Sign in
Project Home Downloads Wiki Issues
Search
for

sbt has moved

sbt has now completely moved to GitHub.

See https://github.com/harrah/xsbt/wiki.

Getting Started

SbtPlugins  
Updated Jul 27, 2011 by dmhar...@gmail.com

sbt has moved

sbt has now completely moved to GitHub.

See https://github.com/harrah/xsbt/wiki/Plugins.

sbt Plugins

Existing Plugins

See Integration Support for IDE-related plugins.

See also the complementary extension mechanism Processors, which includes a list of existing processors.

Basics

Plugins are libraries for your project definition. Declare them like dependencies for your project, except in a plugin definition in the project/plugins/ directory. For example, to use MarkdownJ in your project definition, create:

project/plugins/Plugins.scala

import sbt._
class Plugins(info: ProjectInfo) extends PluginDefinition(info)
{
  val a = "org.markdownj" % "markdownj" % "0.3.0-1.0.2b4"
}

Verify it is correct by doing reload and then console-project. Call some MarkdownJ code:

scala> new com.petebevin.markdown.MarkdownProcessor
res1: com.petebevin.markdown.MarkdownProcessor = Markdown Processor for Java 0.3.0 (compatible with Markdown 1.0.2b2)

This dependency was independent of sbt. You can also create and use plugins that extend or use sbt. The rest of this page describes sbt plugins. The first part shows the results of a plugin that creates a self-extracting jar out of a project. The second part shows how to create a simple plugin that provides a new action to a project.

Hello Lift Installer Example

  • Download the jar
  • Run java -jar hello-lift-1.0-setup.jar . The Hello Lift project and an embedded sbt launcher will be extracted. The project will be loaded and predefined actions run. In this case, the project will be set up, dependencies will be downloaded, and the project will be compiled and Jetty started. Browse to http://localhost:8080.
  • You have a ready example Lift project. For example, you can enter the extracted project directory and start sbt with java -jar sbt-launcher-0.7.1.jar.
  • The project uses Scala X-Ray to generate browsable sources. You can view these in the project/target/classes.sxr directory.
  • You can run installer to regenerate the self-extracting jar from the project. Note: there is currently the issue of including the sbt-launcher in the project zip, making the jar substantially larger than necessary.
  • See the project/plugins/Plugins.scala directory for how the plugin is defined or see the Installer Plugin section below.

Hello World Plugin

To try out plugins, we'll make a simple hello world plugin and use it.

Make the Plugin

To make a plugin, create a new project with a project definition that extends PluginProject. This can now go in project/build. The basic example:

  import sbt._
  class TestPluginProject(info: ProjectInfo) extends PluginProject(info)

Make a simple plugin by putting the following in src/main/scala :

package hw
import sbt._
trait HelloWorldPlugin extends Project {
  lazy val hello = task { log.info("Hello World!"); None }
}

Publish it by running the publish-local action.

Use the Plugin

To use the plugin, create a new project. Declare plugins by putting a plugin definition (basically a project definition that extends PluginDefinition) in project/plugins/. The following example assumes that the plugin above had was setup with the name "test" and with version "1.0".

import sbt._
class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
  val a = "test" % "test" % "1.0"
}

Define the project definition in project/build/ to use the HelloWorldPlugin :

import sbt._
class TestProject(info: ProjectInfo) extends DefaultProject(info) with hw.HelloWorldPlugin

Try it out by running the hello action at the prompt.

Details

sbt plugins are a way to reuse code between project definitions. They can consist of any Scala code you want. Specifically, they are not restricted to providing a single trait extending Project, as in the example.

You can provide a trait that extends Project, DefaultProject, or DefaultWebProject that the user then mixes in to use. You can provide multiple project types or you can just provide code that can be used to implement tasks.

See the Existing Plugins section at the top of the page for examples of plugins.

Installer Plugin

The installer plugin provides a task 'installer' that creates a self-executable jar that bundles the current project. When executed, the jar will extract the sbt launcher and the project, run sbt on the project, and execute predefined commands.

To use it, first declare the plugin:

project/plugins/Plugins.scala
import sbt._
class Plugins(info: ProjectInfo) extends PluginDefinition(info) {
  val extract = "org.scala-tools.sbt" % "installer-plugin" % "0.3.0"
}

Then, mix the BasicSelfExtractingProject trait into your build definition:

project/build/YourProject.scala
import sbt._
class YourProject(info: ProjectInfo) extends DefaultProject(info) 
  with extract.BasicSelfExtractingProject
{
  override def installActions = "update" :: "run" :: Nil
}

Overriding installActions is optional. It defines the actions that sbt runs after the project is extracted. By default, 'update' and 'compile' are run.

Finally, create the self-extracting jar by running the installer action on the project. The resulting jar can be run with java -jar ....

The installer project is a subproject of sbt in the sbt/install/ directory.

Comment by heiko.se...@gmail.com, Apr 29, 2010

I would appreciate putting a link to bnd4sbt on this page ... http://github.com/weiglewilczek/bnd4sbt

Comment by project member dmhar...@gmail.com, Apr 29, 2010

Added, thanks!

Comment by brian.cl...@gmail.com, May 4, 2010

Mark, here's another good plugin for your list: http://github.com/markchadwick/jython-sbt-plugin

Comment by project member dmhar...@gmail.com, May 4, 2010

Thanks, Brian. I added it.

Comment by mailtola...@gmail.com, May 10, 2010

Could you add sbt-eclipsify plugin to the list http://github.com/musk/SbtEclipsify

Comment by project member dmhar...@gmail.com, May 10, 2010

I added a link to IntegrationSupport, where you are already listed. If you'd like the section there expanded, please feel free to comment on that page.

Comment by jmhofer....@gmail.com, Nov 9, 2010

Maybe you'd like to add findbugs4sbt to the list, too? - see http://bitbucket.org/jmhofer/findbugs4sbt

Comment by project member dmhar...@gmail.com, Nov 12, 2010

Added, thanks!

Comment by jmhofer....@gmail.com, Dec 1, 2010

I've now created a plug-in for the Copy/Paste Detector (CPD) of PMD, too. - Could you add that one, too, please? - see http://bitbucket.org/jmhofer/cpd4sbt

Comment by project member dmhar...@gmail.com, Dec 4, 2010

Added, thanks.

Comment by stuart.r...@gmail.com, Jan 7, 2011

Would it be possible to add a link here to the sbt-coverage (undercover code coverage tool) processor? I know it's not a plugin, but I imagine that most people will look on this page for plugin like things! It's at http://github.com/sroebuck/sbt-coverage

Comment by project member dmhar...@gmail.com, Jan 7, 2011

I added it to the Processors page, which is now linked to from this page and DocumentationHome

Comment by wyue...@gmail.com, Feb 6, 2011

I've just made a new plugin for SBT that does resource filtering. Do you mind spreading the word?

https://bitbucket.org/wyuenho/sbt-filter-plugin

Thanks!

Comment by project member dmhar...@gmail.com, Feb 7, 2011

I added the link and you can certainly send an announcement to the list if you want.

Powered by Google Project Hosting