sbt-scalaxb 1.4.x

This page describes adding scalaxb task to your sbt project. (The page describing sbt-scalaxb for sbt 0.7 is moved)

automate using g8

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

$ g8 eed3si9n/scalaxb

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:

import ScalaxbKeys._
 
lazy val commonSettings = Seq(
  organization  := "com.example",
  scalaVersion  := "2.11.5"
)
 
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.2"
lazy val dispatch = "net.databinder.dispatch" %% "dispatch-core" % dispatchV
 
lazy val root = (project in file(".")).
  settings(commonSettings: _*).
  settings(
    name          := "foo-project",
    libraryDependencies ++= Seq(dispatch),
    libraryDependencies ++= {
      if (scalaVersion.value startsWith "2.11") Seq(scalaXml, scalaParser)
      else Seq()
    }).
  settings(scalaxbSettings: _*).
  settings(
    sourceGenerators in Compile += (scalaxb in Compile).taskValue,
    dispatchVersion in (Compile, scalaxb) := dispatchV,
    async in (Compile, scalaxb)           := true,
    packageName in (Compile, scalaxb)     := "generated"
    // packageNames 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 default setting scalaxbSettings scopes scalaxb task into Compile configuration, which compiles all xsd files from src/main/xsd and wsdl files under src/main/wsdl.

By adding the task to sourceGenerators, 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:

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

Here is the list of settings that may be rewired:

sourceManaged  sources         xsdSource         wsdlSource
packageName    packageNames    attributePrefix   classPrefix         paramPrefix
wrapContents   chunkSize       packageDir        generateRuntime     laxAny
protocolFileName protocolPackageName scalaxbConfig logLevel