Java 使用 gradle 进行简单的 protobuf 编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32820728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple protobuf compilation with gradle
提问by vach
If you're looking for sample gradle protobuf project look here.
如果您正在寻找示例 gradle protobuf 项目,请查看此处。
I'm having hard time with gradle and protobuf,
i want to create a simple gradle project that will take any proto files from default src/main/proto
, src/test/proto
and compile them to src/main/java
, src/test/java
accordingly, then pack that into a jar and publish to local repo.
我有困难时期gradle这个和protobuf的,我想创建一个简单的项目的Gradle,将采取从默认的任何原文件src/main/proto
,src/test/proto
并编译它们src/main/java
,src/test/java
因此,再包到这一个罐子,发布到本地回购。
Unfortunately i'm new to gradle and cant figure out how the original project is composed.
不幸的是,我是 gradle 的新手,无法弄清楚原始项目是如何组成的。
Here is my unfinished build.gradle file
这是我未完成的 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')
}
}
After runing jar task we have this :
运行 jar 任务后,我们有这个:
as you can see gradle builds both test and main protos to the same classes directory (red arrows), in the jar i can see both generated classes included (while tests should be skipped).
正如你所看到的,gradle 将测试和主原型构建到同一个类目录(红色箭头),在 jar 中我可以看到两个生成的类都包含在内(而测试应该被跳过)。
but the main problem is that I want to make compile proto files directly to appropriate source directories(blue arrows), after that ordinary build will do the correct thing... After all we need those classes in src to use them in business logic...
但主要问题是我想将proto 文件直接编译到适当的源目录(蓝色箭头),之后普通构建会做正确的事情......毕竟我们需要 src 中的这些类在业务逻辑中使用它们。 ..
So we only need one task that compiles proto to appropriate src directory... nothing more.
所以我们只需要一个将 proto 编译到适当的 src 目录的任务......仅此而已。
src/main/proto to src/main/java
src/test/proto to src/test/java
The current project as it is is located here. Please help to configure this, i'm pretty sure lot of people will need it later...
当前项目位于此处。请帮助配置它,我很确定很多人以后会需要它......
采纳答案by TobiSH
If I don't misunderstand your question it's quite simple to solve. If you don't want to distinguish between your own and the generated sources you just have to add set the generatedFileBaseDir like this generateProtoTasks.generatedFilesBaseDir = 'src'
如果我没有误解你的问题,那么解决起来很简单。如果你不想区分你自己的和生成的源,你只需要像这样添加设置generatedFileBaseDirgenerateProtoTasks.generatedFilesBaseDir = 'src'
So the entire build file looks like:
所以整个构建文件看起来像:
// ...
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
}
Than your folder looks like:
比你的文件夹看起来像:
- src/main/java/com/vach/tryout/AddressBookProtos.java
- src/main/java/com/vach/tryout/protobuf/Main.java
- src/main/java/com/vach/tryout/AddressBookProtos.java
- src/main/java/com/vach/tryout/protobuf/Main.java
BUT:That might not be the best idea to mix generate with handcrafted source code. So my suggestion would be to generate the source code into an own directory like generatedSourcesand add this directory to the java sourceSet. The build file would look like this:
但是:将生成与手工制作的源代码混合在一起可能不是最好的主意。所以我的建议是将源代码生成到一个自己的目录中,比如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')
}
}
Your directory will look like this
您的目录将如下所示
- src/main/proto/dtos.proto
- src/main/java/com/vach/tryout/protobuf/Main.java
- generated-sources/main/java/com/vach/tryout/AddressBookProtos.java
- src/main/proto/dtos.proto
- src/main/java/com/vach/tryout/protobuf/Main.java
- 生成源/main/java/com/vach/tryout/AddressBookProtos.java
A nice side effect is that you can ignore this generated-sourcesdir in your git configuration. That's always a good idea not to publish generated source code.
一个不错的副作用是您可以在 git 配置中忽略这个生成的源目录。最好不要发布生成的源代码。