用gradle简单的protobuf编译

如果您正在寻找示例gradle protobuf项目,请查看此处 。

我很难使用gradle和protobuf,我想创建一个简单的gradle项目,它将从默认的src/main/protosrc/test/proto获取任何proto文件,并将它们编译为src/main/javasrc/test/java ,然后将其打包到jar中并发布到本地repo。

不幸的是,我是新手,无法弄清楚原始项目的组成方式。

这是我未完成的build.gradle文件

 apply plugin: 'java' apply plugin: "com.google.protobuf" buildscript { repositories { mavenCentral() } dependencies { classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0' } } repositories { mavenCentral() } dependencies { compile 'com.google.protobuf:protobuf-java:3.0.0-beta-1' } sourceSets { main { proto { srcDir 'src/main/proto' } java { srcDir 'src/main/java' } } test { proto { srcDir 'src/test/proto' } proto { srcDir 'src/test/java' } } } protobuf { // Configure the protoc executable protoc { // Download from repositories artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3' } generateProtoTasks { // all() returns the collection of all protoc tasks all().each { task -> // Here you can configure the task } // In addition to all(), you may get the task collection by various // criteria: // (Java only) returns tasks for a sourceSet ofSourceSet('main') } } 

运行jar任务后我们有:

在此处输入图像描述

你可以看到gradle将test和main protos构建到相同的类目录(红色箭头),在jar中我可以看到包含的两个生成的类(应该跳过测试)。

但主要问题是我想将编译原型文件直接编译到适当的源目录 (蓝色箭头),之后普通构建将做正确的事情……毕竟我们需要src中的那些类在业务逻辑中使用它们。 ..

所以我们只需要一个将proto编译成适当的src目录的任务……仅此而已。

 src/main/proto to src/main/java src/test/proto to src/test/java 

目前的项目就在这里 。 请帮忙配置一下,我很确定很多人以后会需要它…

如果我不误解你的问题,解决起来就很简单了。 如果你不想区分你自己和你生成的源你只需添加set generatedFileBaseDir就像这样generateProtoTasks.generatedFilesBaseDir = 'src'

所以整个构建文件看起来像:

 // ... protobuf { // Configure the protoc executable protoc { // Download from repositories artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3' } generateProtoTasks.generatedFilesBaseDir = 'src' // <- that line generateProtoTasks { // all() returns the collection of all protoc tasks all().each { task -> // Here you can configure the task } 

比您的文件夹看起来像:

  • 的src / main / JAVA / COM / vach /试用/ AddressBookProtos.java
  • 的src / main / JAVA / COM / vach /试用/ protobuf的/ Main.java

但是:将生成与手工制作的源代码混合起来可能不是最好的主意。 所以我的建议是将源代码生成到像generatedSources这样的自己的目录中,并将此目录添加到java sourceSet中。 构建文件如下所示:

 sourceSets { main { proto { srcDir 'src/main/proto' } java { // include self written and generated code srcDirs 'src/main/java', 'generated-sources/main/java' } } // remove the test configuration - at least in your example you don't have a special test proto file } protobuf { // Configure the protoc executable protoc { // Download from repositories artifact = 'com.google.protobuf:protoc:3.0.0-alpha-3' } generateProtoTasks.generatedFilesBaseDir = 'generated-sources' generateProtoTasks { // all() returns the collection of all protoc tasks all().each { task -> // Here you can configure the task } // In addition to all(), you may get the task collection by various // criteria: // (Java only) returns tasks for a sourceSet ofSourceSet('main') } } 

您的目录将如下所示

  • 的src /主/原/ dtos.proto
  • 的src / main / JAVA / COM / vach /试用/ protobuf的/ Main.java
  • 产生的来源/主/​​ JAVA / COM / vach /试用/ AddressBookProtos.java

一个很好的副作用是你可以在你的git配置中忽略这个generated-sources目录。 不发布生成的源代码总是一个好主意。