Maven에서 *.thrift 컴파일 하기

thrift

서로 다른 플랫폼 사이에 통신할 때 사용하면 편리하다.

물론 thrift를 사용하지 않고 간단하게 json으로 통신해도 좋지만, 

thrift는 바이너리 형태로 통신할 수 있어,네트워크 트래픽을 줄일 수 있고,

컨테이너 자료형(List, Map, Set)이 있어 더 편리하다.

알아보기


Maven

Maven으로 프로젝트 빌드시, maven antrun과 bat파일을 이용해 *thrift 파일을 컴파일 해보자.

Maven알아보기

MavenPropertiesGuide 알아보기

antrun 알아보기


테스트 방법

1) pom.xml에 다음과 같이 추가

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>build-thrift</id>
<phase>install</phase>
<goals><goal>run</goal></goals>
<configuration>
<target>
<echo>thrift gen java</echo>
<!-- always use properties if available -->
<mkdir dir="${project.build.directory}/generated-sources/thrift" />
<mkdir dir="${project.build.directory}/generated-sources/thrift/gen-java" />
<delete dir="${project.build.directory}/generated-sources/thrift/gen-java" includeemptydirs="true"/>
<mkdir dir="${project.build.directory}/generated-sources/thrift/gen-java" />
<exec executable="G:\project\Vert.x\thrift\thrift.cmd" failonerror="true">
<arg value="-gen" />
<arg value="java" />
<arg value="-o" />
<arg value="${project.build.directory}/generated-sources/thrift" />
<!-- set the dir there are thrift files. -->
<arg value="${basedir}/src/main/thrift/" />
</exec>
<!-- You never ever copy generated stuff back into src/* -->
<!-- use Build Helper Maven Plugin to add the generated source -->
<copy todir="src/main/java" overwrite="true">
<fileset dir="target/generated-sources/thrift/gen-java" />
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</plugin>
</plugins>
<build>
view raw pom.xml hosted with ❤ by GitHub

2) thrift.cmd에 다음과 같이 추가

for /R %5 %%i in (*.thrift) do G:\project\Vert.x\thrift\thrift-0.9.2 %1 %2 %3 %4 %%i
view raw thrift.cmd hosted with ❤ by GitHub

3) thrift 파일을 src/main/thrift/에 만들기

4) 이클립스 프로젝트 -> R Click -> Run As -> Maven install

5) src/main/java폴더에 자동으로 파일들이 생성되었는지 확인

6) src/main/java폴더에 자동으로 생성된 파일에 컴파일 오류가 발생했다면, Java Complier를 1.7 이상으로 설정해 준다.

빌드 로그 확인

로그에 아래 내용이 포함되어 있어야 한다.

[INFO] --- maven-antrun-plugin:1.8:run (build-thrift) @ HelloWorld ---

[INFO] Executing tasks