如果您正在寻找示例 gradle protobuf 项目,请查看here https://github.com/google/protobuf-gradle-plugin/tree/master/examples/exampleProject.
我在 gradle 和 protobuf 上遇到了困难,
我想创建一个简单的 gradle 项目,它将采用默认的任何原始文件src/main/proto
, src/test/proto
并将它们编译为src/main/java
, src/test/java
相应地,然后将其打包到 jar 中并发布到本地存储库。
不幸的是,我是 gradle 新手,无法弄清楚原始项目是如何组成的。
这是我未完成的 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 将测试和主原型构建到同一个类目录(红色箭头),在 jar 中我可以看到两个生成的类都包含在内(而测试应该被跳过)。
但主要问题是我想做直接将 proto 文件编译到适当的源目录(蓝色箭头),之后普通构建将执行正确的操作...毕竟我们需要 src 中的这些类才能在业务逻辑中使用它们...
所以我们只需要一项任务,将 proto 编译到适当的 src 目录......仅此而已。
src/main/proto to src/main/java
src/test/proto to src/test/java
当前项目所在位置here https://github.com/vach/sample-gradle-protobuf。请帮助配置这个,我很确定很多人稍后会需要它......