因此,我的问题有两个方面:
1. 不太可能:是否有"Maven 2 Protocol Buffers插件"可以自动完成上述功能?有一个Google Code上的分支, 该作者似乎已经尝试实现这样的插件。不幸的是,它未通过代码审查或合并到protobuf主干。因此,该插件的状态是未知的。
2. 可能更现实:如果没有真正的插件,我该如何从我的Maven 2构建中调用
protoc
?我想我可能能够将现有的shell脚本连接到antrun
调用或类似的东西中。最好分享个人经验。
protoc
?我想我可能能够将现有的shell脚本连接到antrun
调用或类似的东西中。antrun
插件(从上述主题中剪切片段)。 <build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="target/generated-sources"/>
<exec executable="protoc">
<arg value="--java_out=target/generated-sources"/>
<arg value="src/main/protobuf/test.proto"/>
</exec>
</tasks>
<sourceRoot>target/generated-sources</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>
<exec failonerror="true" executable="protoc">
。 - George Hawkins.proto
文件,我是否必须像您在test.proto
中所做的那样一个一个地将它们添加到此脚本中?就像如果我有两个proto文件,我应该这样写,对吗? <arg value="target/proto/Empty.proto"/> <arg value="target/proto/ComponentState.proto"/>
- user4964330[exec] /home/usr/workspace_runtime_x/x.messages/proto/ros/RosTime.proto: 文件未在使用--proto_path(或-I)指定的任何路径中。您必须指定一个包含此文件的--proto_path。请注意,proto_path必须是.proto文件名称的精确前缀--protoc太蠢了,无法确定两个路径(例如绝对和相对)何时等效(这比您想象的更难)。
- Schütze被接受的答案鼓励我使用谷歌提供的插件。我将问题中提到的分支合并到2.2.0源代码的检出中,构建、安装/部署了插件,并能够按以下方式在我的项目中使用它:
<build>
<plugins>
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<version>0.0.1</version>
<executions>
<execution>
<id>generate-sources</id>
<goals>
<goal>compile</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<protoSourceRoot>${basedir}/src/main/protobuf/</protoSourceRoot>
<includes>
<param>**/*.proto</param>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<protocExecutable>/usr/local/bin/protoc</protocExecutable>
</configuration>
</plugin>
</plugins>
</build>
被接受的解决方案在处理多个.proto文件时无法扩展。我必须自己想出解决方法:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile-protoc</id>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="${generated.sourceDirectory}" />
<path id="proto.path">
<fileset dir="src/main/proto">
<include name="**/*.proto" />
</fileset>
</path>
<pathconvert pathsep=" " property="proto.files" refid="proto.path" />
<exec executable="protoc" failonerror="true">
<arg value="--java_out=${generated.sourceDirectory}" />
<arg value="-I${project.basedir}/src/main/proto" />
<arg line="${proto.files}" />
</exec>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
还有一个由Igor Petruk创建的出色插件叫做protobuf-maven-plugin,该插件现在已经在中央仓库中,并且与Eclipse(建议使用m2e-1.1)非常兼容。
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocExecutable>/usr/local/bin/protoc</protocExecutable>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
我刚刚更新了maven插件,使其与2.2.0版本兼容——更新后的pom文件已附在代码审查bug中。
以下是自行构建插件的说明:
svn co http://protobuf.googlecode.com/svn/branches/maven-plugin/tools/maven-plugin
cd maven-plugin
wget -O pom.xml 'http://protobuf.googlecode.com/issues/attachment?aid=8860476605163151855&name=pom.xml'
mvn install
我刚刚尝试了一个非官方但非常新的(v 0.1.7)分支,来自 https://github.com/dtrott/maven-protoc-plugin ,幸运的是它很好用,这要感谢 David Trott。我测试了几个 Maven 模块,其中一个包含 DTO 风格的消息,另一个依赖于它们的服务。我借鉴了 MaxA 在2009年10月16日发布的插件配置,我的 PATH 上有 protoc,然后我添加了
<temporaryProtoFileDirectory>${basedir}/target/temp</temporaryProtoFileDirectory>
紧接在
<protocExecutable>protoc</protocExecutable>
.
真正好的是,我所要做的就是在服务模块上声明一个普通的依赖项,指向 DTO 模块。该插件能够通过找到与 DTO 模块一起打包的 proto 文件来解析 proto 文件的依赖关系,将它们提取到临时目录并在为服务生成代码时使用。而且,它聪明地不会在服务模块中打包生成的 DTO 类的第二份副本。
我认为使用antrun
来调用非Maven步骤是普遍接受的解决方案。
您也可以尝试maven-exec-plugin。
antrun
时遇到了两个问题:(1)它的目的是为了方便从Ant迁移或集成。但我既不打算迁移,也不需要集成。因此,在这种情况下使用它几乎会被视为误用。(2)我只是想要生成一个外部操作系统进程。仅为此目的而拉入整个Ant似乎有些过度。在这种情况下,maven-exec-plugin
听起来更加轻量级和合适。 - Max A.