协议缓冲区编译器Maven插件

7

我在我的POM中配置了协议缓冲区编译器插件,每当项目构建时它就会被执行。这个编译器插件在Windows上运行良好,但现在我把我的项目迁移到了Ubuntu PC上,需要使用一个合适的替代品。

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>compile-protoc</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <tasks>
                            <mkdir dir="src/main/resources/protocolBuffers/compiled" />
                            <path id="proto.path">
                                <fileset dir="src/main/proto">
                                    <include name="**/*.proto" />
                                </fileset>
                            </path>
                            <pathconvert pathsep=" " property="proto.files" refid="proto.path" />
                            <exec executable="src/main/resources/protocolBuffers/compiler/protoc" failonerror="true">
                                <arg value="--java_out=src/main/resources/" />
                                <arg value="-I${project.basedir}/" />
                                <arg line="${proto.files}"/>
                            </exec>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

当我在Ubuntu netbeans中尝试构建项目时,我看到了以下输出。
--- maven-antrun-plugin:1.3:run (compile-protoc) @ Px10App ---
Executing tasks
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 5.638s
Finished at: Tue Mar 25
Final Memory: 9M/105M
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.3:run (compile-protoc) on project Px10App: An Ant BuildException has occured: Execute failed: java.io.IOException: Cannot run program "src/main/resources/protocolBuffers/compiler/protoc": error=2, No such file or directory -> [Help 1]

如何在Ubuntu netbeans中使用编译器插件?
4个回答

11

这个工具内置了protoc(最初基于igor-petruk/protobuf-maven-plugin开发,但已经绑定了Linux、Mac/OSX和Windows的protoc二进制文件)。在运行时它会检测平台并执行相应的二进制文件。

https://github.com/os72/protoc-jar-maven-plugin

以下是一个示例:

<plugin>
    <groupId>com.github.os72</groupId>
    <artifactId>protoc-jar-maven-plugin</artifactId>
    <version>3.11.1</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <protocVersion>2.4.1</protocVersion>
                <inputDirectories>
                    <include>src/main/protobuf</include>
                </inputDirectories>
            </configuration>
        </execution>
    </executions>
</plugin>

添加了示例和一些措辞 - osuciu
只是为了想尝试较新版本的protobuf,这个插件使用嵌入式protobuf,因此没有办法在不修改源代码的情况下更改它。 - min
嗨@user1686407,那不完全准确。是的,有几个协议版本被嵌入其中,最新版本是默认的。但其他版本将从Maven中央下载。 - osuciu
没有冒犯之意,如果可以从Maven中央仓库下载,为什么还需要捆绑Protoc二进制文件呢?为什么还需要更新插件以支持更新的版本呢? - min
现在更新嵌入式版本已经不那么重要了。插件的历史可以追溯到没有可供下载的协议二进制文件的时代。而且拥有一些自包含的东西仍然是“好的”。 - osuciu

7
一个好的跨平台解决方案是使用 Maven Central 中可用的 protoc 二进制文件。 pom.xml 需要进行简单的配置更改,具体详见 com.google.protobuf.tools:maven-protoc-plugin 文档中的 "从 Maven Central Repo 解决 Protoc Artifact" 部分 链接
如果链接消失,重要的部分如下:
<project>
  ...
  <build>
    <extensions>
      <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>1.3.0.Final</version>
      </extension>
    </extensions>
    <plugins>
      <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.5.0</version>
        <extensions>true</extensions>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
            <configuration>
              <protocArtifact>com.google.protobuf:protoc:2.6.1:exe:${os.detected.classifier}</protocArtifact>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
    ...
  </build>
  ...
</project>

这在 Jenkins 上失败了(在本地可以正常运行)。 - Ankur

3

您的protoc可执行文件无法在Linux上执行。 您应该下载并为Linux编译protoc,一旦完成,您就可以像在Windows上使用此maven插件一样使用它。

您可以在此处查看详细说明


谢谢。我认为 apt-get 中也有一个 protobuf 编译器的软件包。你知道需要安装哪个软件包吗? - Rajat Gupta
2
我认为这就是:sudo apt-get install libprotobuf-dev protobuf-compiler [http://devblog.corditestudios.com/blog/2012/08/09/setting-up-protobuf/] - Rajat Gupta

0

你仍然需要 protoc,但我认为使用 protobuf maven 插件来编译 proto 文件更好

我在我的项目中使用它

<plugin>
 <groupId>com.github.igor-petruk.protobuf</groupId>
     <artifactId>protobuf-maven-plugin</artifactId>
      <executions>
       <execution>
        <configuration>
         <cleanOutputFolder>false</cleanOutputFolder>
        </configuration>
        <goals>
         <goal>run</goal>
        </goals>
       </execution>
      </executions>
  </plugin>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接