Skip to content

Latest commit

 

History

History
127 lines (91 loc) · 4.75 KB

README.md

File metadata and controls

127 lines (91 loc) · 4.75 KB

sbt-protobuf

A plugin for sbt-0.(10|11).x that transforms *.proto files into gazillion-loc java files.

Usage

Adding the plugin dependency

In your project, create a file for plugin library dependencies project/plugins/build.sbt and add the following lines:

sbt-0.10.1

resolvers += "gseitz@github" at "http://gseitz.github.com/maven/"

libraryDependencies += "com.github.gseitz" %% "sbt-protobuf" % "0.2"

sbt-0.11.0

resolvers += "gseitz@github" at "http://gseitz.github.com/maven/"

addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.2")

Importing sbt-protobuf settings

To actually "activate" the plugin, its settings need to be included in the build.

build.sbt
import sbtprotobuf.{ProtobufPlugin=>PB}

seq(PB.protobufSettings: _*)
build.scala
import sbtprotobuf.{ProtobufPlugin=>PB}

object MyBuild extends Build {
  lazy val MyProject(
    id = "myproject",
    base = file("."),
    settings = Defaults.defaultSettings ++ PB.protobufSettings ++ Seq( /* custom settings here */ )
  )
}

Declaring dependencies

Assuming an artifact contains both *.proto files as well as the binaries of the generated *.java files, you can specify the dependency like so:

libraryDependencies += "some.groupID" % "some.artifactID" % "1.0" % PB.protobufConfig.name // #1

libraryDependencies += "some.groupID" % "some.artifactID" % "1.0" // #2

Line #1 tells sbt-protobuf that the specified artifact contains *.proto files which it needs to extract and add to the includePath for protoc.

Line #2 adds the artifact to the regular compile:libraryDependencies.

The *.proto files of dependencies are extracted and added to the includePath parameter for protoc, but are not compiled.

Packaging proto files

*.proto files can be included in the jar file by adding the following setting to your build definition:

unmanagedResourceDirectories in Compile <+= (sourceDirectory in PB.protobufConfig).identity,

Changing the location of the generated java files

By default, the compiled proto files are created in <project-dir>/target/<scala-version>/src_managed/main/compiled_protobuf. Changing the location to <project-dir>/src/generated can be done by adding the following setting to your build definition:

javaSource in PB.protobufConfig <<= (sourceDirectory in Compile)(_ / "generated")

WARNING: The content of this directory is removed by the clean task. Don't set it to a directory containing files you hold dear to your heart.

Scope

All settings and tasks are in the protobuf scope. If you want to execute the generate task directly, just run protobuf:generate.

Settings

namename in shellbuilt-in keydefaultdescription
sourceDirectory source-directory x "src/main/protobuf"Path containing *.proto files.
javaSource java-source x "$sourceManaged/compiled_protobuf" Path for the generated *.java files.
version version x "2.4.1" Which version of the protobuf library should be used. A dependency to "com.google.protobuf" % "protobuf-java" % "$version" is automatically added to libraryDependencies
protoc protoc "protoc"The path to the 'protoc' executable.
includePaths include-paths Seq($generated-source, external-include-path)The path for additional *.proto files.
externalIncludePath external-include-path target/protobuf_externalThe path to which protobuf:library-dependencies are extracted and which is used as protobuf:include-path for protoc

Tasks

nameshell-namedescription
generategeneratePerforms the hardcore compiling action and is automatically executed as a "source generator" in the Compile scope.
unpackDependenciesunpack-dependenciesExtracts proto files from library-dependencies into external-inlude-patch

Credits

sbt-protobuf is based on softprops/coffeescripted-sbt for the sbt-0.10 specific parts and codahale/protobuf-sbt for the protobuf specifics.