Skip to content

sgilhooly/sbt-protobuf

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sbt-protobuf

A plugin for sbt-0.(12|13).x that transforms *.proto files into gazillion-loc Java source files, and potentially to other languages too.

Usage

Adding the plugin dependency

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

libraryDependencies += "com.google.protobuf" % "protobuf-java" % "2.5.0"

addSbtPlugin("com.github.gseitz" % "sbt-protobuf" % "0.3.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 sbt._

import sbtprotobuf.{ProtobufPlugin=>PB}

object MyBuild extends Build {
  lazy val myproject = 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.

Additional options to protoc

All options passed to protoc are configured via the protobuf-protoc-options. To add options, for example to run a custom plugin, add them to this setting key. For example:

protocOptions in PB.protobufConfig :+= Seq("--custom-option")

Additional target directories

The source directories where the files are generated, and the globs used to identify the generated files, are configured by generatedTargets in PB.protobufConfig. In case only Java files are generated, this setting doesn't need to change, since it automatically inherits the value of javaSource in PB.protobufConfig, paired with the glob *.java. In case other types of source files are generated, for example by using a custom plugin (see previous section), the corresponding target directories and source file globs must be configured by adding them to this setting. For example:

generatedTargets in PB.protobufConfig <++= (sourceDirectory in Compile){ dir =>
    Seq((dir / "generated" / "scala", "*.scala"))
}

This plugin uses the generatedTargets setting to:

  • add the generated source directories to cleanFiles and managedSourceDirectories
  • collect the generated files after running protoc and return them to SBT for the compilation phase

Scope

All settings and tasks are in the protobuf scope. If you want to execute the protobuf-generate task directly, just run protobuf: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.5.0" Which version of the protobuf library should be used. A dependency to "com.google.protobuf" % "protobuf-java" % "$version" is automatically added to libraryDependencies
protoc protobuf-protoc "protoc"The path to the 'protoc' executable.
includePaths protobuf-include-paths Seq($generated-source, protobuf-external-include-path)The path for additional *.proto files.
externalIncludePath protobuf-external-include-path target/protobuf_externalThe path to which protobuf:library-dependencies are extracted and which is used as protobuf:protobuf-include-path for protoc
protocOptions protobuf-protoc-options --java_out=[java generated source directory from generatedTargets] the list of options passed to the protoc binary
generatedTargets protobuf-generated-targets (file(java source directory based on javaSource in PB.protobufConfig), "*.java") the list of target directories and source file globs for the generated files

Tasks

nameshell-namedescription
generateprotobuf-generatePerforms the hardcore compiling action and is automatically executed as a "source generator" in the Compile scope.
unpackDependenciesprotobuf-unpack-dependenciesExtracts proto files from library-dependencies into protobuf-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.

About

sbt-0.12.x plugin for compiling protobuf files

Resources

License

Stars

Watchers

Forks

Packages

No packages published