简单的protobuf编译与gradle [英] simple protobuf compilation with gradle

查看:1302
本文介绍了简单的protobuf编译与gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您正在寻找样本gradle protobuf项目,请



您可以看到gradle将测试和主protos构建到相同的类目录(红色箭头),在jar中我可以看到包括生成的类(尽管应该跳过测试)。



但主要问题是我想将直接编译为合适的源目录(蓝色箭头),之后,普通版本会做正确的事情...毕竟我们需要这些类在商业逻辑中使用它们......

所以我们只需要一个任务将proto编译到合适的src目录中......仅此而已。

  src / main / proto到src / main / java 
src / test / proto到src / test / java

当前项目位于这里。请帮忙配置这个,我很确定很多人以后会需要它......

如果我不'不要误解你的问题,这很容易解决。如果你不想区分你自己和生成的源,你只需要添加像这样设置generatedFileBaseDir generateProtoTasks.generatedFilesBaseDir ='src'



因此,整个构建文件如下所示:

  // ... 

protobuf {
//配置protoc可执行文件
protoc {
//从版本库下载
artifact ='com.google.protobuf:protoc:3.0.0 -alpha-3'
}

generateProtoTasks.generatedFilesBaseDir ='src'//< - that line

generateProtoTasks {
// all( )返回所有protoc任务的集合
all()。each {task - >
//在这里您可以配置任务
}

例如:
$ b


  • src / main / java / com / vach / tryout / AddressBookProtos.java
  • src / main / java / com / vach / tryout / protobuf / Main.java

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

  sourceSets {
main {
proto {
srcDir'src / main / proto'
}
java {
//包括自写和生成的代码
srcDirs'src / main / java','generated-sources / main / java'
}
}
//删除测试配置 - 至少在你的例子中你没有特殊的测试原型文件
}

protobuf {
//配置protoc可执行文件
protoc {
//从版本库下载
artifact ='com.google.protobuf:protoc:3.0.0-alpha- 3'
}

generateProtoTasks.generatedFilesBaseDir ='generated-sources'

generateProtoTasks {
// all()返回所有protoc任务的集合
all()。each {task - >
//在这里您可以配置任务
}

//除了all(),您还可以通过各种
//标准获取任务集合:

(仅限Java)返回SourceSet
的任务('main')

}
}

您的目录看起来像这样


  • src / main / proto / dtos.proto

  • src / main / java / com / vach / tryout / protobuf / Main.java
  • source / main / java / com / vach / tryout / AddressBookProtos.java


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


If you're looking for sample gradle protobuf project look here.

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.

Unfortunately i'm new to gradle and cant figure out how the original project is composed.

Here is my unfinished build.gradle file

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 :

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).

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...

So we only need one task that compiles proto to appropriate src directory... nothing more.

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...

解决方案

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'

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

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 generatedSources and add this directory to the java sourceSet. The build file would look like this:

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

A nice side effect is that you can ignore this generated-sources dir in your git configuration. That's always a good idea not to publish generated source code.

这篇关于简单的protobuf编译与gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆