8

I'm using maven-antrun-plugin to execute a thrift shell command. I can compile one file per <exec> with <arg value="...path/to/file.thrift" />, but I would like to compile all .thrift files in a directory. How can I do this?

I tried using <arg value="...path/to/*.thrift" />, but Maven rejected this syntax.

4

2 回答 2

15

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

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

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

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

        <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 compilejava 源时,将从 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>
于 2013-09-13T11:06:27.097 回答
10

我正在使用 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
于 2017-07-12T00:54:40.167 回答