协议缓冲编译器maven插件

时间:2014-03-24 20:49:36

标签: java maven maven-plugin protocol-buffers

我在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 个答案:

答案 0 :(得分:6)

这个内置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.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>

答案 1 :(得分:2)

一个好的跨平台解决方案是使用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>

答案 2 :(得分:1)

你的protoc可执行文件无法在linux上执行。你应该为linux下载和编译protoc,一旦你这样做,你就可以在windows上使用它时使用这个maven插件。

您有关于此here

的详细说明

答案 3 :(得分:0)

你还需要protoc 但我认为使用protobuf maven插件编译原型文件更好[/ p>

我在我的项目中使用它

&#13;
&#13;
<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>
&#13;
&#13;
&#13;