sbt-scalaxb

This page describes sbt-scalaxb for scalaxb 1.5.0 and above. See the following for older versions:

automate using sbt new

The following steps has been scripted into a giter8 template called eed3si9n/scalaxb.g8. For a quick project, run the following after installing sbt 0.13.13+.

$ sbt new eed3si9n/scalaxb.g8

After asking you for a project name, the above sets up a nearly empty sbt project with sbt-scalaxb pre-installed. A convenient method to start using scalaxb.

If you want to do add scalaxb to an existing project, keep reading.

using build.sbt

step 1. plugins.sbt

Put this in your project/plugins.sbt:

addSbtPlugin("org.scalaxb" % "sbt-scalaxb" % "X.X.X")

resolvers += Resolver.sonatypeRepo("public")

where “X.X.X” is the latest scalaxb version.

step 2. build.sbt

Here’s a sample build.sbt:

lazy val scalaXml = "org.scala-lang.modules" %% "scala-xml" % "1.0.2"
lazy val scalaParser = "org.scala-lang.modules" %% "scala-parser-combinators" % "1.0.1"
lazy val dispatchV = "0.11.3"
lazy val dispatch = "net.databinder.dispatch" %% "dispatch-core" % dispatchV

lazy val root = (project in file(".")).
  enablePlugins(ScalaxbPlugin).
  settings(inThisBuild(List(
    organization  := "com.example",
    scalaVersion  := "2.11.8"
  ))).
  settings(
    name          := "foo-project",
    libraryDependencies ++= Seq(dispatch),
    libraryDependencies ++= {
      if (scalaVersion.value startsWith "2.10") Seq()
      else Seq(scalaXml, scalaParser)
    }).
  settings(
    scalaxbDispatchVersion in (Compile, scalaxb) := dispatchV,
    scalaxbPackageName in (Compile, scalaxb)     := "generated"
    // scalaxbPackageNames in (Compile, scalaxb)    := Map(uri("http://schemas.microsoft.com/2003/10/Serialization/") -> "microsoft.serialization"),
    // logLevel in (Compile, scalaxb) := Level.Debug
  )

This loads scalaxb task after running reload. The automatic setting in ScalaxbPlugin scopes scalaxb task into Compile configuration, which compiles all xsd files from src/main/xsd and wsdl files under src/main/wsdl. scalaxb will be invoked automatically upon compile.

step 3. directory structure

Make src/main/xsd and src/main/wsdl directory and place your schema document there.

step 4. (optional) basic settings

Settings for the way scalaxb generates the code can be customized by rewiring the settings scoped under scalaxb task and Compile configuration:

scalaxbPackageName in (Compile, scalaxb)     := "generated"

Type help scalaxb* in the sbt shell to list the configurable settings:

> help scalaxb*

scalaxbGenerateVisitor

  Generates visitor

scalaxbPrependFamily

  Prepends family name to class names

scalaxbWrapContents

  Wraps inner contents into a seperate case class

scalaxbDispatchVersion

  Dispatch version

scalaxbAutoPackages

  Generates packages for different namespaces automatically
....