How can I compile all .thrift files (*.thrift) as a Maven phase?

apennebaker picture apennebaker · Sep 12, 2013 · Viewed 10.8k times · Source

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.

Answer

Wildfire picture Wildfire · Sep 13, 2013

The are several options to compile thrift files in maven project:

Option 1: Use maven thrift plugin (the best one)

Maven Thrift plugin supports generation of sources/test sources, recompile on modify, etc. Basically, it's the most convenient way to use thrift in Maven project.

  1. Put your sources in src/main/thrift (or src/test/thrift for test thrift sources).
  2. Install the thrift binary to /usr/local/bin/thrift (or any other place, you prefer)
  3. Add the plugin to the plugins section of your pom.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>
    

That's it: next time you call mvn compile java sources will be generated from thrift. Generated sources will be placed into target/generated-sources/thrift/ directory, and this directory will be added to the compile path for java compiler.

You can find the detailed instructions, samples and more on the Github: https://github.com/dtrott/maven-thrift-plugin.

Option 2: Use Maven Antrun plugin

If it's necessary for some reason to use antrun plugin, it's better to use apply command instead of exec to process a set of files.

I'll write only a basic idea of ant target, since conditional recompilation on modification is probably out of scope of this question:

<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>

Option 3: Use antrun with exec ant task

If it's absolutely necessary for some reason to use Antrun plugin and exec task, there's a way to do so. I'd advise against it, since it's ugly and not portable, but it might work. Use xargs to invoke Thrift compiler for a list of files:

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