Maven和Protobuf编译错误:在com.google.protobuf包中找不到符号

25

我是 Linux 和 Protobuf 的新手.. 我需要帮助。

我正在尝试对包含多个 ".proto" 文件和一个 pom.xml 文件的项目执行 "mvn package"。

我在 Ubuntu 上工作。

=======================================

错误

当我运行 "mvn package" 时,我收到以下错误:

after

...
Compiling 11 source files to .../target/classes
...

我收到了很多这样的错误:

[ERROR] .../target/generated-sources/...java:[16457,30] cannot find symbol
[ERROR] symbol  : class Parser
[ERROR] location: package com.google.protobuf
[ERROR] 
[ERROR] .../target/generated-sources/...java:[17154,37] cannot find symbol
[ERROR] symbol  : class Parser
[ERROR] location: package com.google.protobuf
[ERROR] 
[ERROR] .../target/generated-sources/...java:[17165,30] cannot find symbol
[ERROR] symbol  : class Parser
[ERROR] location: package com.google.protobuf
[ERROR] 
[ERROR] .../target/generated-sources/...java:[17909,37] cannot find symbol
[ERROR] symbol  : class Parser
[ERROR] location: package com.google.protobuf
[ERROR]

=======================================

POM

这里是 pom.xml 文件,已删除 groupId 和 artifactId:

<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
     <groupId>*****</groupId>
     <artifactId>*****</artifactId>
     <version>1.0-SNAPSHOT</version>
  </parent>
  <artifactId>*****</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
      <proto.cas.path>${project.basedir}/src</proto.cas.path>
      <target.gen.source.path>${project.basedir}/target/generated-sources</target.gen.source.path>
  </properties>
 <dependencies>
      <dependency>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
                <version>2.4.1</version>
                <scope>compile</scope>
            </dependency>
  </dependencies>
  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
        <plugins>
            <plugin>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>2.0.2</version>
               <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    <includes><include>**/commonapps/**</include></includes>
                </configuration>            
             </plugin>
             <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <tasks>
                                    <mkdir dir="${target.gen.source.path}" />    
                                    <path id="proto.path.files">
                                        <fileset dir="${proto.cas.path}">
                                            <include name="*.proto" />
                                        </fileset>  
                                    </path>
                                    <pathconvert pathsep=" " property="proto.files" refid="proto.path.files" />

                                    <exec executable="protoc">
                                         <arg value="--java_out=${target.gen.source.path}" />
                                         <arg value="--proto_path=${proto.cas.path}" />
                                            <arg line="${proto.files}" />
                                    </exec>
                                </tasks>
                                <sourceRoot>${target.gen.source.path}</sourceRoot>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
         </plugins>
     </build>
</project>

PROTOBUF安装

我已经完成了。

./configure
make
make check
make install
在protobuf/中,以及。
mvn test
mvn install
mvn package

我在protobuf/java目录下找到了一个jar文件。

然后通过运行以下命令将其添加到我的maven仓库:

mvn install:install-file -Dpackaging=jar -DgeneratePom=true  -DgroupId=com.google.protobuf   -DartifactId=protobuf-java   -Dfile=protobuf-java-2.4.1.jar -Dversion=2.4.1

请注意,我已经乱搞了$LD_LIBRARY_PATH。当前运行echo it时,我得到以下输出:

/usr/local/lib/:/usr/:/usr/lib/:/usr/local/

是的...正如你所看到的,我对于设置$LD_LIBRARY_PATH毫无头绪。

我也运行了:

apt-get install protobuf-compiler

=======================================

PROTOC 安装

我忘记了我是如何让 protoc 正常工作的,但当我运行以下命令时:

protoc --version

我得到了

libprotoc 2.5.0

=======================================

我的问题类似于:

Problems using protobufs with java and scala

Maven编译失败

=======================================

可能的相关性?

'mvn install' 后仍然找不到包

http://www.scriptol.com/programming/protocol-buffers-tutorial.php

有人能帮忙吗?

=======================================

进展

显然是插件失败:

https://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project casprotobuf: Compilation failure: Compilation failure:
7个回答

15

当安装的版本与pom中列出的版本不一致时,我遇到了这个问题。匹配版本可以解决问题。在我的情况下,我不得不将我的protoc版本切换回2.4.1以匹配POM。


1
我使用的是“protobuf.version” 3.9.0 和“protoc.version” 3.10.0,遇到了这个问题。将它们都更改为3.9.0即可解决。谢谢!--jsp - J Steven Perry
设置为匹配的版本解决了我的问题!谢谢。 - Stella

13

我遇到了同样的问题。直接从Google构建protobuf源代码(我使用的是2.5.0),并执行

mvn install:install-file -Dpackaging=jar -DgeneratePom=true  -DgroupId=com.google.protobuf   -DartifactId=protobuf-java   -Dfile=protobuf-java-2.5.0.jar -Dversion=2.5.0

对我来说解决了问题。

在早期的尝试中,我注意到在 /root/.m2/repository/com/google/protobuf/protobuf-java/2.5.0/ 中缺少jar文件。

也许尝试在pom.xml中使用版本2.5.0和/或手动复制jar文件。

干杯


将我的pom中的2.4.1更改为2.5.0也解决了我的问题。 - gnielsen

11

protoc --version 的版本必须与 pom.xml 文件中设置的版本相同(即 protobuf-java-2.5.0.jar 的版本)。


这对我来说是最简单的解决方案! - santon

1

对我来说,在构建脚本中使用以下内容后,问题得到了解决。

<clearOutputDirectory>false</clearOutputDirectory

完整的构建脚本

<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.5.0.Final</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.19.0:exe:${os.detected.classifier}</pluginArtifact>
                    <clearOutputDirectory>false</clearOutputDirectory>
                    <outputDirectory>${basedir}/src/main/java/</outputDirectory>

                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

1
我的问题是一个单元测试从主文件夹继承了类。我通过以下方式解决:
<!-- Allow tests to call classes in main folder -->

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/test/java</source>
                    <source>src/main/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

1
问题出在您的protoc和protobuf jar之间存在版本不匹配,保持相同的版本即可解决。对于依赖关系,您需要在pom.xml文件中进行检查。

0

检查protobuf编译器版本和maven依赖版本是否兼容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接