使用gradle进行简单的protobuf编译

cqoc49vn  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(220)

如果您正在寻找示例gradle protobuf项目,请查找here
我很难使用gradle和protobuf,我想创建一个简单的gradle项目,该项目将从默认的src/main/protosrc/test/proto中获取任何proto文件,并相应地将其编译为src/main/javasrc/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中的那些类在业务逻辑中使用它们...
因此,我们只需要一个任务,编译到适当的src目录proto ...没有更多。

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

目前的项目,因为它是位于here。请帮助配置这一点,我很肯定很多人会需要它以后...

nfzehxib

nfzehxib1#

如果我没有误解你的问题的话,这个问题很容易解决。如果你不想区分你自己的源代码和生成的源代码,你只需要像generateProtoTasks.generatedFilesBaseDir = 'src'这样添加设置generatedFileBaseDir
因此,整个构建文件如下所示:

// ...

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
    }

文件夹的外观:

  • Java语言的开发者可以通过以下方式来开发JavaAddressBookProtos.java
  • java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/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')

    }   
}

您的目录将如下所示

  • 源代码/主文件/协议/dtos.协议
  • java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/Main.java
  • java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/java/AddressBookProtos.java

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

相关问题