首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将所有.thrift文件(*.thrift)编译为Maven阶段?

如何将所有.thrift文件(*.thrift)编译为Maven阶段?
EN

Stack Overflow用户
提问于 2013-09-12 23:08:00
回答 2查看 11.7K关注 0票数 8

我正在使用maven-antrun-plugin执行一个thrift shell命令。我可以用<arg value="...path/to/file.thrift" />为每个<exec>编译一个文件,但我想编译一个目录中的所有.thrift文件。我该怎么做呢?

我尝试使用<arg value="...path/to/*.thrift" />,但是Maven拒绝了这种语法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-13 19:06:27

在maven项目中,有几个选项可以编译thrift文件:

选项1:使用maven thrift插件(最好的)

Maven Thrift插件支持生成源代码/测试源,修改时重新编译等。基本上,这是在Maven项目中使用thrift的最方便的方式。

  1. 将您的源码放入src/main/thrift (或将thrift二进制文件放到/usr//
    1. /src/main/thrift(或您喜欢的任何其他位置)的src/test/thrift中)
    2. 将插件添加到您的pom.xml的plugins部分:

org.apache.thrift.tools maven-thrift-plugin 0.1.11 /usr/local/bin/thrift thrift-sources generate-sources编译 -test-sources generate-test-sources

就是这样:下次调用mvn compile时,将从thrift生成java源代码。生成的源码会放在target/generated-sources/thrift/目录下,这个目录会被添加到java编译器的编译路径中。

您可以在Github:https://github.com/dtrott/maven-thrift-plugin上找到详细的说明、示例和更多内容。

选项2:使用Maven Antrun插件

如果出于某种原因需要使用antrun插件,最好使用apply命令而不是exec来处理一组文件。

我将只写一个ant目标的基本概念,因为修改时的条件重新编译可能超出了这个问题的范围:

代码语言:javascript
复制
<target name="compile-thrift">
    <!-- Define fileset of thrift files -->
    <fileset id="thrift.src.files" dir="${src.thrift.dir}">
        <include name="**/*.thrift"/>
    </fileset>

    <!-- Invoke thrift binary for each of these files -->
    <apply executable="${thrift.compiler}" resultproperty="thrift.compile.result"
    failifexecutionfails="true" failonerror="true"
    searchpath="true" dir="${src.thrift.dir}">
        <arg value="-o"/>
        <arg value="${thrift.dest.dir}"/>
        <arg value="--gen"/>
        <arg value="java"/>
        <srcfile/>
        <fileset refid="thrift.src.files"/>
    </apply>
</target>

选项3:将antrun与exec ant任务一起使用

如果出于某种原因,绝对有必要使用Antrun插件和exec任务,有一种方法可以做到这一点。我建议不要使用它,因为它很难看,而且不能移植,但它可能会工作。使用xargs调用Thrift编译器以获取文件列表:

代码语言:javascript
复制
<exec dir="${src.thrift.dir}" executable="bash">
  <arg line="ls * | xargs ${thrift.compiler} -o ${thrift.dest.dir} --gen java"/>
</exec>
票数 15
EN

Stack Overflow用户

发布于 2017-07-12 08:54:40

我搁浅了thrift 0.10.0,发现为了使用maven-thrift-plugin,我必须提供generator选项:

代码语言:javascript
复制
        <plugin>
            <groupId>org.apache.thrift.tools</groupId>
            <artifactId>maven-thrift-plugin</artifactId>
            <version>0.1.11</version>
            <configuration>
                <thriftSourceRoot>${basedir}/src/main/resources/thrift</thriftSourceRoot>
                <generator>java</generator>
            </configuration>
            <executions>
                <execution>
                    <id>thrift-sources</id>
                    <phase>generate-sources</phase>
                    <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>

否则它会抱怨“未知的选项java:hashcode”。事实上,在java生成器中似乎不再有这样的选项。thrift --help提供了以下选项:

代码语言:javascript
复制
  java (Java):
beans:           Members will be private, and setter methods will return void.
private-members: Members will be private, but setter methods will return 'this' like usual.
nocamel:         Do not use CamelCase field accessors with beans.
fullcamel:       Convert underscored_accessor_or_service_names to camelCase.
android:         Generated structures are Parcelable.
android_legacy:  Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
option_type:     Wrap optional fields in an Option type.
java5:           Generate Java 1.5 compliant code (includes android_legacy flag).
reuse-objects:   Data objects will not be allocated, but existing instances will be used (read and write).
sorted_containers:
                 Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.
generated_annotations=[undated|suppress]:
                 undated: suppress the date at @Generated annotations
                 suppress: suppress @Generated annotations entirely
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18767986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档