【问题标题】:How can I compile all .thrift files (*.thrift) as a Maven phase?如何将所有 .thrift 文件 (*.thrift) 编译为 Maven 阶段?
【发布时间】:2019-04-28 09:31:53
【问题描述】:

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

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

【问题讨论】:

  • 是否可以生成python/其他语言文件,还是只能生成java文件?

标签: maven ant thrift


【解决方案1】:

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

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

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

  1. 将您的来源放入 src/main/thrift(或 src/test/thrift 用于测试节俭来源)。
  2. 将 thrift 二进制文件安装到 /usr/local/bin/thrift(或您喜欢的任何其他位置)
  3. 将插件添加到 pom.xml 的 plugins 部分:

        <plugin>
            <groupId>org.apache.thrift.tools</groupId>
            <artifactId>maven-thrift-plugin</artifactId>
            <version>0.1.11</version>
            <configuration>
                <thriftExecutable>/usr/local/bin/thrift</thriftExecutable>
            </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>
    

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

您可以在 Github 上找到详细说明、示例等:https://github.com/dtrott/maven-thrift-plugin

选项 2:使用 Maven Antrun 插件

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

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

<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 编译器获取文件列表:

<exec dir="${src.thrift.dir}" executable="bash">
  <arg line="ls * | xargs ${thrift.compiler} -o ${thrift.dest.dir} --gen java"/>
</exec>

【讨论】:

  • 我想 Xlint 我的手写 .java 代码,而不是 Xlint 我的节俭生成的 .java 代码。你能调整你的解决方案来完成这个吗?
  • 在使用 maven-thrift 插件时,我收到了这个错误:nknown option java:hashcode(thrift: 0.11.0)。还有其他人面临同样的问题吗?我在网上搜索,说:它没有 java:hashcode 选项了。降级到 thrift 0.9.3 对人们有用。但我只想使用这个或最新版本,有什么建议吗?
  • @Wildfire 有没有办法避免引用 thriftExecutable ?我希望 Maven 在启动时下载该生成器。 stackoverflow.com/questions/52044010/…
【解决方案2】:

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

        <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 提供以下选项:

  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

【讨论】:

  • 哇!惊人。这解决了我的问题。添加 java
猜你喜欢
  • 2014-04-29
  • 1970-01-01
  • 2014-03-31
  • 2012-03-12
  • 1970-01-01
  • 2019-03-02
  • 1970-01-01
  • 2011-02-04
  • 2012-01-14
相关资源
最近更新 更多