Skip to content

Instantly share code, notes, and snippets.

@tomazk
Created May 24, 2011 14:28
Show Gist options
  • Save tomazk/988802 to your computer and use it in GitHub Desktop.
Save tomazk/988802 to your computer and use it in GitHub Desktop.
Thrift compiler via Maven
# we use antrun:run goal and bind it to the generate-sources lifecycle
# to generate java source files via thrift compiler
$ mvn generate-sources
# or just use
$ mvn compile
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tomazkovacic</groupId>
<artifactId>boilerpipe-thrift</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>boilerpipe-thrift</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<thrift.exe>/usr/local/bin/thrift</thrift.exe>
<thrift.interface>${basedir}/thrift/Service.thrift</thrift.interface>
<gendir.target>${basedir}/target/generated-sources</gendir.target>
<gendir.namespace>com/yourdomain/yourlib/thrift/gen</gendir.namespace>
<gendir.src>${basedir}/src/main/java/com/yourdomain/yourlib/thrift/gen</gendir.src>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<configuration></configuration>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<target>
<echo>
create (or clear) output directory for generated files
</echo>
<mkdir dir="${gendir.target}" />
<delete>
<fileset dir="${gendir.target}" includes="**/*" />
</delete>
<echo>
generate java source files from ${thrift.interface}
</echo>
<exec executable="${thrift.exe}">
<arg value="--gen" />
<arg value="java:beans"/>
<arg value="-o"/>
<arg value="${gendir.target}"/>
<arg value="${thrift.interface}"/>
</exec>
<echo>
copy generated files to ${gendir.src}
</echo>
<mkdir dir="${gendir.src}" />
<delete>
<fileset dir="${gendir.src}" includes="**/*"/>
</delete>
<copy todir="${gendir.src}">
<fileset dir="target/generated-sources/gen-javabean/${gendir.namespace}"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libfb303</artifactId>
<version>0.6.1</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
assuming the following directory structure
|-- pom.xml
|-- src
| |-- main
| | `-- java
| | `-- com
| | `-- yourdomain
| | `-- yourlib
| | `-- thrift
| | |-- App.java
| | `-- gen
| | `-- ...
| `-- test
| `-- ...
`-- thrift
`-- Service.thrift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment