maven-antrun-plugin:为protobuf生成源不会生成Java文件

时间:2016-08-03 14:19:28

标签: maven protocol-buffers maven-antrun-plugin

我的pom.xml中有以下插件,它应该生成编译其他项目所需的java文件:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
        <source>${jdk.version}</source>
        <target>${jdk.version}</target>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <archive>
            <manifestFile>META-INF/MANIFEST.MF</manifestFile>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <tasks>
                    <mkdir dir="src-gen" />
                    <exec executable="protoc">
                        <arg value="--java_out=src-gen" />
                        <arg value="proto/ros/RosTime.proto" />
                        <arg value="proto/ros/RosHeader.proto" />
                        <arg value="proto/ros/RosPoint.proto" />
                        <arg value="proto/ros/RosPose.proto" />
                        <arg value="proto/ros/RosPoseStamped.proto" />
                        <arg value="proto/ros/RosQuaternion.proto" />
                        <arg value="proto/U.proto" />
                        <arg value="proto/S.proto" />
                        <arg value="proto/Z.proto" />
                        <arg value="proto/algorithm/A.proto" />
                        <arg value="proto/algorithm/B.proto" />
                        <arg value="proto/algorithm/C.proto" />
                        <arg value="proto/algorithm/D.proto" />
                        <arg value="proto/algorithm/E.proto" />
                        <arg value="proto/algorithm/F.proto" />
                        <arg value="proto/env/G.proto" />
                        <arg value="proto/env/H.proto" />
                        <arg value="proto/env/J.proto" />
                        <arg value="proto/env/K.proto" />
                        <arg value="proto/env/L.proto" />
                    </exec>
                </tasks>
                <sourceRoot>src-gen</sourceRoot>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

但是,在src-gen文件夹下,这根本不会生成任何内容。然后我不断收到错误,例如:

    cannot find symbol
[ERROR] symbol:   class AMsg
[ERROR] location: class project.messages.AFormatter

package project.messages.ros.RosHeader does not exist
package project.messages.ros.RosPoint does not exist
package project.messages.ros.RosPose does not exist
package project.messages.ros.RosPoseStamped does not 

基本上它找不到必要的文件,当然,并给出错误。我已经安装了2.5.0版本,并且正确定义了依赖关系:

<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>2.5.0</version>
</dependency>

我已经没想完了。是什么让这个无效?

编辑:以下注意力集中在mvn install -X的调试输出中:

Execute:Java13CommandLauncher: Executing 'protoc' with arguments:

'--java_out=src-gen'
'proto/ros/RosTime.proto'
'proto/ros/RosHeader.proto'
'proto/ros/RosPoint.proto'
'proto/ros/RosPose.proto'
'proto/ros/RosPoseStamped.proto'
'proto/ros/RosQuaternion.proto'
 .
 .
 .
 . 
The ' characters around the executable and arguments are
not part of the command.
     [exec] ros/RosTime.proto: File not found.
     [exec] proto/ros/RosHeader.proto: Import "ros/RosTime.proto" was not found or had errors.
     [exec] proto/ros/RosHeader.proto:6:14: "project.messages.ros.RosTimeMsg" seems to be defined in "proto/ros/RosTime.proto", which is not imported by "proto/ros/RosHeader.proto".  To use it here, please add the necessary import.
  .
  .  
  .
     [INFO] --- maven-antrun-plugin:1.3:run (generate-sources) @ X.messages ---
[DEBUG] org.apache.maven.plugins:maven-antrun-plugin:jar:1.3:
[DEBUG]    org.apache.maven:maven-plugin-api:jar:2.0.4:compile
[DEBUG]    org.apache.maven:maven-project:jar:2.0.4:compile
[DEBUG]       org.apache.maven:maven-settings:jar:2.0.4:compile
[DEBUG]       org.apache.maven:maven-profile:jar:2.0.4:compile
[DEBUG]       org.apache.maven:maven-model:jar:2.0.4:compile
[DEBUG]       org.apache.maven:maven-artifact-manager:jar:2.0.4:compile
[DEBUG]          org.apache.maven:maven-repository-metadata:jar:2.0.4:compile
[DEBUG]       org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:compile
[DEBUG]          junit:junit:jar:3.8.1:compile
[DEBUG]          classworlds:classworlds:jar:1.1-alpha-2:compile
[DEBUG]    org.apache.maven:maven-artifact:jar:2.0.4:compile
[DEBUG]    org.codehaus.plexus:plexus-utils:jar:1.5.6:compile
[DEBUG]    org.apache.ant:ant-launcher:jar:1.7.1:runtime
[DEBUG]    org.apache.ant:ant:jar:1.7.1:compile
[DEBUG] Created new class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG] Importing foreign packages into class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG]   Imported:  < project>runtime_shared:art.util.robot:1.0.0
[DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3
[DEBUG]   Included: org.apache.maven.plugins:maven-antrun-plugin:jar:1.3
[DEBUG]   Included: junit:junit:jar:3.8.1
[DEBUG]   Included: org.codehaus.plexus:plexus-utils:jar:1.5.6
[DEBUG]   Included: org.apache.ant:ant-launcher:jar:1.7.1
[DEBUG]   Included: org.apache.ant:ant:jar:1.7.1
[DEBUG]   Excluded: org.apache.maven:maven-plugin-api:jar:2.0.4
[DEBUG]   Excluded: org.apache.maven:maven-project:jar:2.0.4
[DEBUG]   Excluded: org.apache.maven:maven-settings:jar:2.0.4
[DEBUG]   Excluded: org.apache.maven:maven-profile:jar:2.0.4
[DEBUG]   Excluded: org.apache.maven:maven-model:jar:2.0.4
[DEBUG]   Excluded: org.apache.maven:maven-artifact-manager:jar:2.0.4
[DEBUG]   Excluded: org.apache.maven:maven-repository-metadata:jar:2.0.4
[DEBUG]   Excluded: org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9
[DEBUG]   Excluded: classworlds:classworlds:jar:1.1-alpha-2
[DEBUG]   Excluded: org.apache.maven:maven-artifact:jar:2.0.4
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-antrun-plugin:1.3:run from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-antrun-plugin:1.3, parent: sun.misc.Launcher$AppClassLoader@5c647e05]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-antrun-plugin:1.3:run' with override configurator -->
[DEBUG]   (f) pluginArtifacts = [org.apache.maven.plugins:maven-antrun-plugin:maven-plugin:1.3:, junit:junit:jar:3.8.1:compile, org.codehaus.plexus:plexus-utils:jar:1.5.6:compile, org.apache.ant:ant-launcher:jar:1.7.1:runtime, org.apache.ant:ant:jar:1.7.1:compile]
[DEBUG]   (f) project = MavenProject: runtime_X:X.messages:1.0.0 @ /home/usr/workspace_runtime_X/X.messages/pom.xml
[DEBUG]   (f) sourceRoot = /home/usr/workspace_runtime_X/X.messages/src-gen
[DEBUG]   (f) tasks = 
[DEBUG] -- end configuration --

1 个答案:

答案 0 :(得分:1)

如果对未创建的文件有疑问,请确保使用绝对路径。在这种情况下,${basedir}允许我们获取Maven基目录位置的绝对路径(POM的位置):

<mkdir dir="${basedir}/src-gen" />
<exec executable="protoc">
    <arg value="--java_out=${basedir}/src-gen" />
    <arg value="--proto_path=${basedir}" /> <!-- proto_path needs to point to where the proto files are -->
    <arg value="proto/ros/RosTime.proto" />
    <!-- rest of proto files... -->
</exec>

然后,请注意maven-antrun-plugin的{​​{3}}参数已弃用:

  

已过时。使用build-helper-maven-plugin绑定源目录

根据建议,您应该使用<sourceRoot>目标将源文件夹添加到Maven项目中。配置要使用该目标的build-helper-maven-plugin:add-source参数添加的文件夹。

话虽如此,您可能更有可能使用sources而不是依赖Ant任务:

  

Maven Protocol Buffers Plugin使用协议缓冲区编译器(protoc)工具从指定项目的.proto(协议缓冲区定义)文件生成Java源文件。

org.xolstice.maven.plugins:protobuf-maven-plugin目标可用于生成Java源文件。将.proto文件放在src/main/proto下(可使用compile参数配置),插件将在${project.build.directory}/generated-sources/protobuf/java内生成Java源文件(可使用protoSourceRoot参数进行配置)。

如果protoc可执行文件不在PATH环境变量中,您可以使用outputDirectory参数进行设置。