【问题标题】:Protocol buffers compiler maven plugin协议缓冲区编译器 maven 插件
【发布时间】:2014-05-02 10:52:18
【问题描述】:

我在我的 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 中工作?

【问题讨论】:

    标签: java maven maven-plugin protocol-buffers


    【解决方案1】:

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

    你有这个here的详细说明

    【讨论】:

    【解决方案2】:

    你仍然需要 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>

    【讨论】:

      【解决方案3】:

      这个带有内置的 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>
      

      【讨论】:

      • 添加示例和一些措辞
      • 仅供任何想尝试更新版本的 protobuf 的人使用,此插件使用embedded protobuf,因此如果不修改源代码就无法更改。
      • 嗨@user1686407 这并不完全准确。是的,嵌入了一些 protoc 版本,最新的是默认版本。但其他版本将从 maven central 下载
      • 没有冒犯,如果它可以从 maven Central 下载,为什么它需要捆绑 protoc 二进制文件?为什么需要更新插件以支持更新版本?
      • 是的,现在更新嵌入式版本不再那么重要了。该插件的历史可以追溯到没有 protoc 二进制文件可供下载的时代。拥有独立的东西仍然“很好”
      【解决方案4】:

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

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

      万一链接消失,重点是:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        • 1970-01-01
        相关资源
        最近更新 更多