• Stars
    star
    173
  • Rank 211,610 (Top 5 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created almost 13 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

sbt plugin for compiling protobuf files

sbt-protobuf

A plugin for sbt 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/plugins.sbt and add the following line:

addSbtPlugin("com.github.sbt" % "sbt-protobuf" % "0.7.1")

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 ProtobufConfig / version (set to 3.9.0 by default).

Importing sbt-protobuf settings

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

build.sbt
enablePlugins(ProtobufPlugin)

Declaring dependencies

Artifacts containing both *.proto files and java binaries

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.

Artifacts in the protobuf configuration containing only *.proto files

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}"

Compiling external proto files

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:

ProtobufConfig / sourceDirectories += (ProtobufConfig / protobufExternalIncludePath).value

Packaging proto files

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

Compile / unmanagedResourceDirectories += (ProtobufConfig / sourceDirectory).value

Alternatively, *.proto files can be packaged in a separate jar file in the protobuf configuration with a proto classifier:

addArtifact(ProtobufConfig / protobufPackage / artifact, ProtobufConfig / protobufPackage)

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:

ProtobufConfig / javaSource := ((Compile / sourceDirectory).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.

Note

  1. If you encounter compile errors, as [...] is already defined as object [...] you could change the compile order as compileOrder := CompileOrder.JavaThenScala,the default is mixed.

  2. 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 the option java_outer_classname = "NewNameNotAsTheFileName"; to you .proto file.

Additional options to protoc

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:

ProtobufConfig / protobufProtocOptions ++= 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 ProtobufConfig / protobufGeneratedTargets. In case only Java files are generated, this setting doesn't need to change, since it automatically inherits the value of ProtobufConfig / javaSource, 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:

ProtobufConfig / protobufGeneratedTargets ++= {
  Seq(((Compile / sourceDirectory).value / "generated" / "scala", "*.scala"))
}

This plugin uses the protobufGeneratedTargets 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 protobufGenerate task directly, just run protobuf:protobufGenerate.

Settings

namebuilt-in keydefaultdescription
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.9.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 ProtobufConfig / javaSource), "*.java") the list of target directories and source file globs for the generated files

Tasks

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

IntelliJ IDEA BSP bug

IntelliJ has a bug where it only recognizes generated sources if there is at least one Scala class in the same package - otherwise you'll see red squiggles. As a workaround, you can configure your project to add a private empty class, e.g. like this:

Compile / sourceGenerators += Def.task {
  // adapt this for your build:
  val protoPackage = "org.example.proto.foo"

  val scalaFile = (Compile/sourceManaged).value / "_ONLY_FOR_INTELLIJ.scala"
  
  IO.write(scalaFile,
    s"""package $protoPackage
      |
      |private class _ONLY_FOR_INTELLIJ
      |""".stripMargin)

  Seq(scalaFile)
}.taskValue

Credits

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

More Repositories

1

sbt

sbt, the interactive build tool
Scala
4,685
star
2

sbt-native-packager

sbt Native Packager
Scala
1,585
star
3

sbt-dependency-graph

sbt plugin to create a dependency graph for your project
Scala
1,243
star
4

sbt-jmh

"Trust no one, bench everything." - sbt plugin for JMH (Java Microbenchmark Harness)
Scala
781
star
5

sbt-eclipse

Plugin for sbt to create Eclipse project definitions
Scala
721
star
6

sbt-release

A release plugin for sbt
Scala
638
star
7

sbt-buildinfo

I know this because build.sbt knows this.
Scala
545
star
8

sbt-web

Library for building sbt plugins for the web
Scala
365
star
9

sbt-git

A git plugin for sbt
Scala
343
star
10

zinc

Scala incremental compiler library, used by sbt and other build tools
Scala
324
star
11

docker-sbt

Official sbt docker images
Dockerfile
308
star
12

sbt-dynver

An sbt plugin to dynamically set your version from git
Scala
294
star
13

sbt-ci-release

sbt plugin to automate Sonatype releases from GitHub Actions
Scala
274
star
14

sbt-onejar

Packages your project using One-JARβ„’
Scala
268
star
15

sbt-scalariform

sbt plugin adding support for source code formatting using Scalariform
Scala
259
star
16

sbt-fresh

sbt-plugin to create an opinionated fresh sbt project
Scala
235
star
17

sbt-github-actions

An sbt plugin which makes it easier to build with GitHub Actions
Scala
192
star
18

sbt-header

sbt-header is an sbt plugin for creating file headers, e.g. copyright headers
Scala
190
star
19

sbt-bintray

fresh packages delivered from your sbt console
Scala
180
star
20

sbt-site

Site generation for sbt
Scala
175
star
21

sbt-start-script

SBT Plugin to create a "start" script to run the program
Scala
144
star
22

sbt-pgp

PGP plugin for sbt
Scala
141
star
23

sbt-groll

sbt plugin to roll the Git history
Scala
134
star
24

junit-interface

Implementation of sbt's test interface for JUnit
Java
132
star
25

sbt-unidoc

sbt plugin to create a unified Scaladoc or Javadoc API document across multiple subprojects.
Scala
127
star
26

sbt-jacoco

an sbt plugin for JaCoCo Code Coverage
Scala
123
star
27

sbt-jni

SBT Plugin to ease working with JNI
Scala
122
star
28

sbt-projectmatrix

Scala
116
star
29

sbt-boilerplate

sbt plugin for generating scala.Tuple/Function related boilerplate code
Scala
110
star
30

sbt-proguard

Proguard sbt plugin
Scala
99
star
31

sbt-atmos

sbt plugin for running Typesafe Console in development
Scala
98
star
32

sbt-launcher-package

Packaging for sbt so you can run it.
Scala
90
star
33

sbt-dirty-money

clean Ivy2 cache
Scala
88
star
34

sbt-license-report

Report on licenses used in an sbt project.
Scala
85
star
35

sbt-doge

sbt plugin to aggregate tasks across subprojects and their crossScalaVersions
Scala
78
star
36

website

The source for scala-sbt.org
Scala
75
star
37

sbt-pom-reader

Translates xml -> awesome. Maven-ish support for sbt.
Scala
75
star
38

sbt-remote-control

Create and manage sbt process using unicorns and forks
Scala
74
star
39

sbt-aspectj

AspectJ sbt plugin
Scala
73
star
40

sbt-scalabuff

SBT plugin which generate case classes and support for serialization from Google Protocol Buffer definitions using ScalaBuff
Scala
72
star
41

contraband

http://www.scala-sbt.org/contraband/
Scala
68
star
42

sbt-s3

sbt-s3 is a simple sbt plugin to manipulate objects on Amazon S3
Scala
62
star
43

sbt-multi-jvm

Multi-JVM testing in sbt
Scala
56
star
44

sbt-javaagent

sbt plugin for adding java agents to projects
Scala
54
star
45

sbt-paradox-material-theme

Material Design theme for Paradox
StringTemplate
51
star
46

sbt-cpd

Copy & Paste Detector plugin using PMD for sbt.
Scala
49
star
47

sbt-osgi

sbt plugin for creating OSGi bundles
Scala
47
star
48

sbt-findbugs

FindBugs static analysis plugin for sbt.
Scala
47
star
49

sbt-man

Looks up scaladoc.
Scala
46
star
50

librarymanagement

librarymanagement module for sbt
Scala
46
star
51

sbt-less

Scala
42
star
52

ipcsocket

IPC: Unix Domain Socket and Windows Named Pipes for Java
Java
42
star
53

io

IO module for sbt
Scala
41
star
54

sbt-js-engine

Support for sbt plugins that use JavaScript
Scala
40
star
55

launcher

The sbt launcher as its own project. Can launch any ivy/maven published project with a main class, with some fancy features.
Scala
40
star
56

sbt-autoversion

Scala
35
star
57

sbt-digest

sbt-web plugin for checksum files
Scala
31
star
58

sbt-jupiter-interface

Implementation of SBT's test interface for JUnit Jupiter
Java
30
star
59

sbt-avro

sbt plugin for compiling Avro schemas, similar to sbt-protobuf
Scala
29
star
60

sbt-slash

unified slash syntax for both shell and build.sbt
Scala
29
star
61

sbt-java-formatter

An sbt plugin for formating Java code
Scala
27
star
62

sbt-gzip

sbt-web plugin for gzipping assets
Scala
25
star
63

sbt-unique-version

emulates Maven's uniqueVersion snapshots
Scala
24
star
64

sbt-pull-request-validator

Plugin that optimizes pull request validation to only validate sub projects that have changed
Scala
23
star
65

sbt.github.com

See https://github.com/sbt/website for the source
HTML
22
star
66

sbt-duplicates-finder

Find classes and resources conflicts in your build
Scala
22
star
67

sbt-autoplugin.g8

giter8 template for sbt 0.13.5+ AutoPlugin
Scala
20
star
68

sbt-cucumber

Cucumber plugin for SBT.
Scala
20
star
69

sbt-jcstress

Trust no-one, and especially not memory visibility.
HTML
19
star
70

sbt-sriracha

Scala
18
star
71

adept

adept helps you find, declare, and download dependencies. http://groups.google.com/group/adept-dev/
18
star
72

sbt-mocha

SBT plugin for running mocha JavaScript unit tests on node
Scala
17
star
73

sbt-multi-release-jar

Support for JDK9's Multi Release JAR Files (JEP 238)
Scala
17
star
74

sbt-xjc

SBT plugin to compile an XML Schema with XJC
Scala
15
star
75

util

util modules for sbt
Scala
15
star
76

sbt-export-repo

exports your dependency graph to a preloaded local repository
Scala
15
star
77

sbt-nocomma

sbt-nocomma reduces commas from your build.sbt.
Scala
13
star
78

serialization

serialization facility for sbt
Scala
13
star
79

sbt-maven-resolver

An sbt plugin to resolve dependencies using Aether
Scala
12
star
80

sbt-core-next

sbt APIs targeted for eventual inclusion in sbt core
Scala
12
star
81

sbt-houserules

House rules for sbt modules.
Scala
12
star
82

sbt-pamflet

sbt plugin to run Pamflet (and Pamflet plugin to run sbt)
Scala
11
star
83

sbt-sdlc

Scaladoc link checker for sbt
Scala
11
star
84

sbt-appbundle

A plugin for the simple-build-tool to create an OS X application bundle.
Scala
10
star
85

bintry

your packages, delivered fresh
Scala
10
star
86

sbt-fmpp

FreeMarker Scala/Java Templating Plugin for SBT
Scala
9
star
87

sbt-ynolub

Scala
9
star
88

sbt-testng

Implementation of the sbt testing interface for TestNG, bundled with an sbt plug-in for convenience.
Scala
9
star
89

sbt-concat

sbt-web plugin for concatenating web assets
Scala
8
star
90

sbt-ant

SBT plug-in to call Ant targets from within SBT builds
Scala
7
star
91

sbtn-dist

Shell
6
star
92

sbt-community-plugins

All community plugins that opt into an uber-build
Scala
6
star
93

sbt-vimquit

an sbt plugin that adds :q command.
Scala
5
star
94

helloworld-one

An example build for sbt 1.0.0.
Scala
5
star
95

sbt-giter8-resolver

Scala
5
star
96

sbt-sequential

adds sequential tasks to sbt
Scala
4
star
97

sbt-scalashim

generates sys.error.
Scala
4
star
98

sbt-experimental

Experimental APIs to fix rough edges in sbt
Scala
3
star
99

sbt-web-build-base

Scala
3
star
100

sbt-validator

Builds sbt 1.0.x against recent versions of the sbt modules
Shell
3
star