A plugin for sbt-0.(10|11).x that transforms *.proto files into gazillion-loc java files.
In your project, create a file for plugin library dependencies project/plugins/build.sbt
and add the following lines:
resolvers += "gseitz@github" at "http://gseitz.github.com/maven/"
libraryDependencies += "com.github.gseitz" %% "sbt-protobuf" % "0.2"
resolvers += "gseitz@github" at "http://gseitz.github.com/maven/"
addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.2")
To actually "activate" the plugin, its settings need to be included in the build.
import sbtprotobuf.{ProtobufPlugin=>PB}
seq(PB.protobufSettings: _*)
import sbtprotobuf.{ProtobufPlugin=>PB}
object MyBuild extends Build {
lazy val MyProject(
id = "myproject",
base = file("."),
settings = Defaults.defaultSettings ++ PB.protobufSettings ++ Seq( /* custom settings here */ )
)
}
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.
*.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,
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.
All settings and tasks are in the protobuf
scope. If you want to execute the generate
task directly, just run protobuf:generate
.
name | name in shell | built-in key | default | description |
---|---|---|---|---|
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_external | The path to which protobuf:library-dependencies are extracted and which is used as protobuf:include-path for protoc |
name | shell-name | description |
---|---|---|
generate | generate | Performs the hardcore compiling action and is automatically executed as a "source generator" in the Compile scope. |
unpackDependencies | unpack-dependencies | Extracts proto files from library-dependencies into external-inlude-patch |
sbt-protobuf
is based on softprops/coffeescripted-sbt for the sbt-0.10 specific parts and codahale/protobuf-sbt for the protobuf specifics.