我正在使用maven-antrun-plugin来执行thrift shell命令。我可以使用<exec>
为<arg value="...path/to/file.thrift" />
编译一个文件,但我想在目录中编译所有.thrift
文件。我怎样才能做到这一点?
我尝试使用<arg value="...path/to/*.thrift" />
,但Maven拒绝了这种语法。
有几个选项来编译maven项目中的thrift文件:
Maven Thrift插件支持生成源/测试源,重新编译修改等。基本上,它是在Maven项目中使用thrift最方便的方法。
src/main/thrift
(或src/test/thrift
用于测试节俭来源)。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上找到详细说明,示例等。
如果由于某种原因需要使用antrun插件,最好使用apply
命令而不是exec
来处理一组文件。
我只会写一个关于ant目标的基本概念,因为修改时的条件重新编译可能超出了这个问题的范围:
<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>
如果出于某种原因绝对需要使用Antrun插件和exec
任务,那么就有办法这样做。我建议反对它,因为它很丑陋而且不便携,但它可能会起作用。使用xargs
调用Thrift编译器获取文件列表:
<exec dir="${src.thrift.dir}" executable="bash">
<arg line="ls * | xargs ${thrift.compiler} -o ${thrift.dest.dir} --gen java"/>
</exec>
我正在与节俭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