This plugin is copied from sbt/sbt-protobuf, and changed to compile file by file. For example, you have a.proto, b.proto, c.proto in src/main/protobuf, then
In sbt/sbt-protobuf, the compile command would be
proto -I[includePath] --java_out[output_path] src/main/protobuf/a.proto src/main/protobuf/b.proto src/main/protobuf/c.proto
When we have the same class name defined in different package name, e.g. com.package.a.ClassA in a.protobuf, com.package.b.ClassA in b.protobuf, the command would throw error of ClassA is already define in xxx.
In fuqiliang/sbt-protobuf, the compile command would splited to seperate proto file, thus compile file by file
proto -I[includePath] --java_out[output_path] src/main/protobuf/a.proto
proto -I[includePath] --java_out[output_path] src/main/protobuf/b.proto
proto -I[includePath] --java_out[output_path] src/main/protobuf/c.proto
Then we got no error and can compile successfully.
A plugin for sbt that transforms *.proto files into gazillion-loc Java source files, and potentially to other languages too.
In your project, create a file for plugin library dependencies project/plugins.sbt
and add the following line:
resolvers ++= Seq("Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/groups/public")
addSbtPlugin("com.github.fuqiliang" % "sbt-protobuf" % "0.6.4-appschema")
OSSRH Ticket link: https://issues.sonatype.org/browse/OSSRH-44290
The dependency to "com.google.protobuf" % "protobuf-java"
is automatically added to the Compile
scope.
The version for protobuf-java
can be controlled by the setting version in ProtobufConfig
(set to 3.4.0
by default).
To actually "activate" the plugin, its settings need to be included in the build.
enablePlugins(ProtobufPlugin)
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" % 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 --proto_path
for protoc
.
Internally the setting protobufExternalIncludePath
is used to track 3rd party proto files.
Line #2 adds the artifact to the regular compile classpath.
The *.proto
files of dependencies are extracted and added to the --proto_path
parameter for protoc
, but are not compiled.
You can specify a dependency on an artifact that contains only .proto
files in the protobuf
configuration with a proto
classifier like so:
libraryDependencies += ("some.groupID" % "some.artifactID" % "1.0" classifier protoClassifier) % s"${ProtobufConfig.name}->${ProtobufConfig.name}"
Sometimes it's desirable to compile external proto files (eg. because the library is compiled with an older version of protoc
).
This can be achieved by adding the following setting:
sourceDirectories in ProtobufConfig += (protobufExternalIncludePath in ProtobufConfig).value
*.proto
files can be included in the jar file by adding the following setting to your build definition:
unmanagedResourceDirectories in Compile += (sourceDirectory in ProtobufConfig).value
Alternatively, *.proto
files can be packaged in a separate jar file in the protobuf
configuration with a proto
classifier:
addArtifact(artifact in (ProtobufConfig, protobufPackage), protobufPackage in ProtobufConfig)
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 ProtobufConfig := ((sourceDirectory in Compile).value / "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.
-
If you encounter compile errors, as
[...] is already defined as object [...]
you could change the compile order ascompileOrder := CompileOrder.JavaThenScala
,the default ismixed
. -
The inner message's name could not be the
.proto
's file name.that will cause problem too, you could change the inner message's name or the.proto
file name or add theoption java_outer_classname = "NewNameNotAsTheFileName";
to you.proto
file.
All options passed to protoc
are configured via the protobufProtocOptions
. To add options, for example to run a custom plugin, add them to this setting key. For example:
protobufProtocOptions in ProtobufConfig ++= Seq("--custom-option")
The source directories where the files are generated, and the globs used to identify the generated files, are configured by protobufGeneratedTargets in ProtobufConfig
.
In case only Java files are generated, this setting doesn't need to change, since it automatically inherits the value of javaSource in 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:
protobufGeneratedTargets in ProtobufConfig ++= {
Seq(((sourceDirectory in Compile).value / "generated" / "scala", "*.scala"))
}
This plugin uses the protobufGeneratedTargets
setting to:
- add the generated source directories to
cleanFiles
andmanagedSourceDirectories
- collect the generated files after running
protoc
and return them to SBT for the compilation phase
All settings and tasks are in the protobuf
scope. If you want to execute the protobufGenerate
task directly, just run protobuf:protobufGenerate
.
name | built-in key | default | description |
---|---|---|---|
sourceDirectory | x | "src/main/protobuf" |
Path containing *.proto files. |
sourceDirectories | x | sourceDirectory |
This setting is used to collect all directories containing *.proto files to compile |
javaSource | x | "$sourceManaged/compiled_protobuf" |
Path for the generated *.java files. |
version | x | "3.4.0" |
Which version of the protobuf library should be used. A dependency to "com.google.protobuf" % "protobuf-java" % "$version" is automatically added to libraryDependencies |
protobufProtoc | "protoc" |
The path to the 'protoc' executable. | |
protobufIncludePaths | Seq($generated-source, protobufExternalIncludePath) |
The path for additional *.proto files. |
|
protobufExternalIncludePath | target/protobuf_external |
The path to which protobuf:libraryDependencies are extracted and which is used as protobuf:protobufIncludePath for protoc |
|
protobufProtocOptions | --java_out= [java generated source directory from protobufGeneratedTargets ] |
the list of options passed to the protoc binary |
|
protobufGeneratedTargets | (file( java source directory based on javaSource in ProtobufConfig ), "*.java") |
the list of target directories and source file globs for the generated files |
name | description |
---|---|
protobufGenerate | Performs the hardcore compiling action and is automatically executed as a "source generator" in the Compile scope. |
protobufUnpackDependencies | Extracts proto files from libraryDependencies into protobufExternalInludePatch |
protobufRunProtoc | A function that executes the protobuf compiler with the given arguments,
returning the exit code. The default implementation runs the executable referenced by the protoc setting. |
protobufPackage | Produces a jar artifact containing only *.proto files, with a proto classifier |
sbt-protobuf
is based on softprops/coffeescripted-sbt for the sbt-0.10 specific parts and codahale/protobuf-sbt for the protobuf specifics.