开发者

Ant target for building protocol buffer files

Can anyone show me how to write ant rules (build.xml) for creating a .jar file from a .proto file?

Basically, I have written a functiona开发者_运维百科l example.proto and I can use the command line protoc to output java files. But I want an ant rule to automate the above process as part of my build. Can someone share a code snippet for a starting point?


I know this question is ancient, but it is still one of the top hits on Google for "ant protoc".

I was disappointed that I could not find an ant task for protoc, so I wrote my own: https://github.com/okorz001/ant-protoc

It is fairly primitive at the moment, but it supports nested filesets which was something I could not figure out how to do with exec hacks. (I am by no means an ant expert.) The task only executes protoc; you will need to use ant's stock javac and jar tasks in order to produce a jar.


Proto compilation can also be done through ant exec.

  <!-- Generates protocol buffers. -->
  <property name="proto.srcs" value="../protos" />
  <target name="proto">
    <exec executable="protoc" failonerror="true">
        <arg value="--java_out=." />
        <arg value="--proto_path=${proto.srcs}" />
        <arg line="${proto.srcs}/my.proto" />
    </exec>
  </target>


It sounds like you need the Ant Exec task to execute the binary to create the .java from the .proto files. The Javac task will then compile these and the Jar task builds your .jar file.

How do you tie all this together ? This tutorial introduces Ant and how to make use of tasks like the above.


Here is what I've done to incorporate protoc into a project. Ideally I'd use a <mapper> but associating .proto files with their .java files isn't trivial, so in the meantime I just touch a marker file every time protoc runs. The key is the <uptodate> task which lets you set a property based on the relative ages of two sets of files (true if from is older than to, false otherwise).

<property name="proto.messages" value="proto"/>
<property name="proto.src" value="src-proto"/>
<property name="proto.markerfile" value="${proto.src}/markerfile"/>

<target name="init">
  <uptodate property="skip.protogen" targetfile="${proto.markerfile}">
    <srcfiles dir="${proto.messages}" />
  </uptodate>
  ...
</target>

<target name="clean">
  <delete dir="${proto.src}"/>
  ...
</target>

<target name="protoc" depends="init" unless="skip.protogen">
  <!-- delete first to ensure clean build -->
  <delete dir="${proto.src}"/>
  <mkdir dir="${proto.src}" />
  <apply executable="protoc" failonerror="true">
    <arg prefix="--java_out=" file="${proto.src}" />
    <arg prefix="--proto_path=" file="${proto.messages}" />
    <fileset dir="${proto.messages}" includes="**/*.proto" />
  </apply>
  <touch file="${proto.markerfile}" />
</target>

<target name="build" depends="init,protoc,...">
  <javac ...>
    <src path="${proto.src}"/>
    ...
  </javac>
</target>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜