简体   繁体   English

Maven Thrift插件-如何包含Thrift定义?

[英]Maven thrift plugin - How can I include thrift definitions?

I'm currently using the maven-thrift-plugin to generate jars and jsnode automatically during build. 我目前正在使用maven-thrift-plugin在构建过程中自动生成jar和jsnode。 I'm successfully able to compile and deploy the definitions and use them in another project. 我可以成功地编译和部署定义,并在另一个项目中使用它们。 But now I need to have that other project define its own thrift type that includes the other definitions, and I can't see how to accomplish this in a nice automated way. 但是现在我需要让其他项目定义自己的节俭类型,其中包括其他定义,而我看不到如何以一种很好的自动化方式来实现这一目标。 Example: 例:

I have a project Foo. 我有一个项目Foo。 It's in it's own git repo FooGitRepo and produces maven artifact FooMvnArtifact in com.example.foo as well. 它在它自己的git repo FooGitRepo中,并在com.example.foo中生成Maven工件FooMvnArtifact。 In Foo I have something like "src/thrift/foo.thrift" which defines FooType in the com.example.foo package. 在Foo中,我有类似“ src / thrift / foo.thrift”的内容,它在com.example.foo包中定义了FooType。 So far so good. 到现在为止还挺好。

The pom for the plugin looks like this: 插件的pom如下所示:

       <plugin>
            <groupId>org.apache.thrift.tools</groupId>
            <artifactId>maven-thrift-plugin</artifactId>
            <version>0.1.11</version>
            <configuration>
                <thriftExecutable>${project.basedir}/compiler/thrift-0.9.1</thriftExecutable>
                <thriftSourceRoot>${basedir}/src</thriftSourceRoot>
            </configuration>
            <executions>
                <execution>
                    <id>generate-thrift-java</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <generator>java</generator>
                        <outputDirectory>
                            gen-java
                        </outputDirectory>
                        <compileOutput>true</compileOutput>
                    </configuration>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>generate-thrift-jsnode</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <generator>js:node</generator>
                        <outputDirectory>
                            gen-jsnode
                        </outputDirectory>
                        <compileOutput>false</compileOutput>
                    </configuration>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>thrift-test-sources</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

This produces the outputs in artifacts appropriate for the specified languages. 这将产生适合于指定语言的工件输出。

In my second project, Bar which is in its own git repo has its own maven repo I use Foo in java. 在我的第二个项目中,在自己的git repo中的Bar拥有自己的maven repo,我在Java中使用Foo。 This is very simple. 这很简单。 I just put FooMvnArtifact in the dependency section of Bar's pom. 我只是将FooMvnArtifact放在Bar的pom的依赖项中。 Then in the java code all I have to do is import com.example.foo.FooType . 然后,在Java代码中,我要做的就是import com.example.foo.FooType But now I want to add a Bar.thrift with a BarType that contains a FooType as a member. 但是现在我想添加一个Bar.thrift,其BarType包含一个FooType作为成员。 That means that Bar.thrift needs to include Foo.thrift . 这意味着Bar.thrift需要include Foo.thrift But that isn't visible from Bar because the maven repo only contains the language-specific artifacts of the thrift compilation, not the definition itself. 但这在Bar中是不可见的,因为Maven存储库仅包含节俭编译的特定于语言的工件,而不包含定义本身。

How can I cause the Foo maven repo to contain the thrift definition itself? 如何使Foo Maven存储库包含节俭定义本身? How can I get Bar to depend on this definition in such a way that I can include it in another thrift definition? 如何使Bar以这种方式依赖于此定义,以便可以将其包含在其他旧式定义中? Preferably I can do this in such a way as to avoid naming collisions if in the future I have multiple CommonType.thrift I need to import from different projects. 如果将来我需要从不同项目导入多个CommonType.thrift,则最好以避免命名冲突的方式执行此操作。

In the future this is likely going to come up in the context of protocol buffers as well. 将来在协议缓冲区的上下文中也可能会出现这种情况。 I would expect the solutions would be similar. 我希望解决方案将是相似的。

When the maven-thrift-plugin executes the generate-sources process, maven-thrift-plugin执行generate-sources流程时,
1. it copies all dependent thrift files to a temporary directory 1.将所有相关的节俭文件复制到一个临时目录
2. the dependent thrift files are found from the project's compile-dependency artifacts (which may be a jar, zip or other type artifact file) 2.从项目的编译依赖工件(可以是jar,zip或其他类型的工件文件)中找到依赖的节俭文件。

You just need to add the project which contains the dependency thrift file into current project's compile dependency. 您只需要将包含依赖项转换文件的项目添加到当前项目的编译依赖项中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM