用于构建协议缓冲区文件的 Ant 目标

发布于 2024-09-17 06:59:58 字数 212 浏览 4 评论 0原文

谁能告诉我如何编写 ant 规则(build.xml)来从 .proto 文件创建 .jar 文件?

基本上,我已经编写了一个功能性的example.proto,并且我可以使用命令行protoc来输出java文件。但我想要一个 ant 规则来自动执行上述过程,作为我构建的一部分。有人可以分享一个代码片段作为起点吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热情消退 2024-09-24 06:59:58

我知道这个问题很古老,但它仍然是 Google 上“ant protoc”的热门搜索之一。

我很失望找不到 protoc 的 ant 任务,所以我写了自己的: https://github .com/okorz001/ant-protoc

目前它相当原始,但它支持嵌套文件集,这是我无法弄清楚如何使用 exec hacks 来实现的。 (我绝不是蚂蚁专家。)任务只执行protoc;您将需要使用 ant 的库存 javac 和 jar 任务来生成 jar。

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.

孤独患者 2024-09-24 06:59:58

Proto编译也可以通过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>

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>
窝囊感情。 2024-09-24 06:59:58

听起来您需要 Ant Exec 任务来执行二进制文件以创建来自 .proto 文件的 .java。然后,Javac 任务 将编译这些任务和 Jar 任务 构建您的 .jar 文件。

您如何将所有这些联系在一起? 本教程介绍 Ant以及如何利用上述任务。

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.

失与倦" 2024-09-24 06:59:58

以下是我将 protoc 合并到项目中所做的工作。理想情况下,我会使用 但是将 .proto 文件与其 .java 文件关联起来并不简单,因此与此同时,每次 protoc 运行时我只需触摸一个标记文件。
关键是 任务允许您根据两组文件的相对年龄设置属性(如果 from 早于 to,则为 true,否则为 false)。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文