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

I am using maven-antrun-plugin to execute the trift shell command. I can compile one file on <exec> using <arg value="...path/to/file.thrift" /> , but I would like to compile all the .thrift files in the directory. How can i do this?

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

+6
source share
2 answers

There are several options to compile economical files in a maven project:

Option 1: use the maven trift plugin (best)

The Maven Thrift plugin supports creating sources / test sources, recompiling to modifications, etc. Basically, this is the most convenient way to use frugality in a Maven project.

  • Put your sources in src/main/thrift (or src/test/thrift for sources of savings).
  • Set the shore binary bit to / usr / local / bin / thrift (or any other place you prefer)
  • 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> 

What it is: the next time you call mvn compile , java sources will be generated from the savings. The generated sources will be placed in the target/generated-sources/thrift/ directory, and this directory will be added to the compilation path for the java compiler.

Detailed instructions, samples, etc. can be found on Github: https://github.com/dtrott/maven-thrift-plugin .

Option 2: use the Maven Antrun plugin

If for some reason you need to use the antrun plugin, it is better to use the apply command instead of exec to process the set of files.

I will write only the basic idea of ​​the ant target, since conditional recompilation during modification probably goes beyond the 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 for some reason you need to use the Antrun plugin and the exec task, then there is a way to do this. I would advise doing this as it is ugly and not portable, but it can work. Use xargs to call the 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> 
+13
source

I play aground with thrift 0.10.0 and found that to use maven-thrift-plugin I had to provide the generator option:

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

Otherwise, he will complain that the "unknown parameter is java: hashcode". In fact, it seems that this option is no longer in the java generator. thrift --help provides the following features:

  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 
+5
source

Source: https://habr.com/ru/post/1501878/


All Articles