Java 协议缓冲区编译器 maven 插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22620300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:54:26  来源:igfitidea点击:

Protocol buffers compiler maven plugin

javamavenmaven-pluginprotocol-buffers

提问by Rajat Gupta

I have protocol buffers compiler plugin configured in my POM that is executed whenever the project is built. This complier plugin works fine in windows but now I moved my project to an ubuntu PC & need to used a suitable alterntive for this.

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

I see the following output when trying to build the project in Ubuntu netbeans

尝试在 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]

How to make compiler plugin work in Ubuntu netbeans ?

如何使编译器插件在 Ubuntu netbeans 中工作?

采纳答案by partlov

Your protoc executable could not be executed on linux. You should download and compile protoc for linux and once you do it you will be able to use this maven plug-in as you are using it on windows.

您的 protoc 可执行文件无法在 linux 上执行。你应该下载并编译 protoc for linux,一旦你这样做了,你就可以像在 Windows 上一样使用这个 maven 插件。

You have detailed instruction for this here

在这里有详细的说明

回答by Brendan

You still need protoc but I think it is nicer to use a a protobuf maven plugin to compile proto files

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

I use this in my projects

我在我的项目中使用它

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

回答by osuciu

This one comes with protoc built-in (it's originally based on igor-petruk/protobuf-maven-plugin, but it comes with protoc binaries bundled for Linux, Mac/OSX, and Windows). At runtime it detects the platform and executes the corresponding binary

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

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

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

Here's an example:

下面是一个例子:

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

回答by Eddy

A good cross-platform solution is to use the protoc binary artifacts available in Maven Central.

一个好的跨平台解决方案是使用 Maven Central 中可用的 protoc 二进制工件。

The simple configuration changes needed to your pom.xml are described in the com.google.protobuf.tools: maven-protoc-plugin documentation "Resolving Protoc Artifact From Maven Central Repo"

com.google.protobuf.tools: maven-protoc-plugin 文档“Resolving Protoc Artifact From Maven Central Repo”中描述了 pom.xml 所需的简单配置更改

In case the link disappears, the salient part is:

如果链接消失,突出的部分是:

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