簡體   English   中英

協議緩沖區編譯器 maven 插件

[英]Protocol buffers compiler maven plugin

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

這個內置了 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>

一個好的跨平台解決方案是使用 Maven Central 中可用的 protoc 二進制工件。

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

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

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

您的 protoc 可執行文件無法在 linux 上執行。 你應該下載並編譯 protoc for linux,一旦你這樣做了,你就可以像在 Windows 上一樣使用這個 maven 插件。

在這里有詳細的說明

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM