2014-03-24 74 views
5

我在我的POM中配置了協議緩衝區編譯器插件,每當構建項目時都會執行該插件。這個編譯器插件在windows中工作正常,但現在我將我的項目移動到了一臺Ubuntu的PC上,需要使用一個合適的替代方案。協議緩衝區編譯器maven插件

 <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項目時,下面的輸出?

回答

1

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

您的這個here

+0

謝謝..另外我認爲還有一個protobuf編譯器的apt-get包。你知道需要爲那個軟件包安裝哪些軟件包嗎? –

+1

我想是這樣的:'sudo apt-get install libprotobuf-dev protobuf-compiler' [http://devblog.corditestudios.com/blog/2012/08/09/setting-up-protobuf/] –

0

你仍然需要protoc 但我認爲這是更好的使用AA protobuf的Maven插件來編譯原文件

我在我的項目使用的詳細說明

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

+0

不工作。 。! –

+0

你可以添加錯誤信息嗎? – Brendan

4

這一個合作mes與內置的protoc(它最初基於igor-petruk/protobuf-maven-plugin,但附帶protoc二進制文件,捆綁在Linux,Mac/OSX和Windows上)。在運行時檢測平臺,並執行相應的二進制

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

下面是一個例子:

<plugin> 
    <groupId>com.github.os72</groupId> 
    <artifactId>protoc-jar-maven-plugin</artifactId> 
    <version>3.2.0</version> 
    <executions> 
     <execution> 
      <phase>generate-sources</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
       <protocVersion>2.4.1</protocVersion> <!-- 2.4.1, 2.5.0, 2.6.1, 3.2.0 --> 
       <inputDirectories> 
        <include>src/main/protobuf</include> 
       </inputDirectories> 
      </configuration> 
     </execution> 
    </executions> 
</plugin> 
+0

增加了一些例子和一些措辭 – osuciu

2

良好的跨平臺解決方案是使用Maven的中央提供的protoc二進制工件。

需要你的pom.xml的簡單的配置更改在com.google.protobuf.tools描述:如果鏈接消失Maven的protoc-插件文檔"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>