如何使用Gradle从依赖项jar中导入

如何使用Gradle从依赖项jar中导入

本文介绍了如何使用Gradle从依赖项jar中导入.proto文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,该项目创建了一组protobuf对象和GRPC存根.

I have a project that creates a set of protobuf objects and GRPC stubs.

我对其中包含其他.proto文件的jar依赖,我想在我的项目中使用它.

I have a dependency on a jar with other .proto files in it, that I would like to use in my project.

即:

project-abc-0.0.1.jar包含一个文件:/some-types.proto它包含以下内容:

project-abc-0.0.1.jar contains a file: /some-types.protoIt contains these pieces:

package foo_companyname;
message StatusItem {
    string status = 1;
    string statusDescription = 2;
}

我的项目有一个build.gradle文件,我试图像这样导入它:

my project has a build.gradle file where I am trying to import it like so:

buildscript {
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
    }
}

dependencies {
    compile(group: 'com.companyname', name: 'project-abc', version: '0.0.1')
}

然后,在我的新"enhanced-status.proto"中,我正在执行以下操作:

Then, inside my new "enhanced-status.proto" I'm doing this:

import "foo_companyname/some-types.proto";

message EnhancedStatus{
    string author = 1;
    repeated StatusItem status = 2;
}

如果我没有引用其他.proto,那么一切正常-我能够生成所有正确的java任何python类.一旦添加它,我就会得到:

If I don't reference the other .proto, everything works fine - I'm able to generate all the correct java any python classes. As soon as I add it, I'm getting this:

Execution failed for task ':generateProto'.
> protoc: stdout: . stderr: foo_companyname/some-types.proto: File not found.
  enhanced-status.proto: Import "foo_companyname/some-types.proto" was not found or had errors.
  enhanced-status.proto:26:19: "StatusItem" is not defined.

我假设有某种技巧可以使gradle或protoc查找jar文件中的.proto源代码?还是我需要将jar文件的.proto解压缩到我自己的/proto目录中?这将导致冲突,因为jar已具有some-types.proto的编译版本,并且我不想再次对其进行编译.

I'm assuming there's some trick to getting gradle or protoc to find .proto sources that are in a jar file? Or do I need to extract the jar file's .proto into my own /proto dir? That would cause a conflict, since the jar has a compiled version of some-types.proto already, and I don't want to compile it again.

推荐答案

使用Google protobuf gradle插件

Using the google protobuf gradle plugin

plugins {
   id 'com.google.protobuf' version "0.8.5"
}

您只需要在dependency块中指定您的依赖jar(即,作为"protobuf"依赖,而不是"compile"):

You should just need to specify your dependent jar in the dependencies block (i.e, as a 'protobuf' dependency, not 'compile'):

dependencies {
    protobuf "com.companyname:project-abc:0.0.1"
}

这篇关于如何使用Gradle从依赖项jar中导入.proto文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:49