Maven & Protobuf 编译报错:Cannot find symbol in package com.google.protobuf

新手上路,请多包涵

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

我正在尝试“mvn package”一个包含许多“.proto”文件的项目,当然还有一个 pom.xml 文件……

我在 Ubuntu 上工作

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

错误

当我运行“mvn package”时,我收到这个错误:

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

在协议缓冲区/,

mvn test
mvn install
mvn package

在 protobuf/java 中。

我把罐子放在 protobuf/java/target

并通过运行将其添加到我的 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 --version

我得到

libprotoc 2.5.0

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

我的问题类似于:

在 java 和 scala 中使用 protobufs 的问题

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:

原文由 Katie 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

我有同样的问题。直接从谷歌构建 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 和/或手动复制 jarfile。

干杯

原文由 thesonix 发布,翻译遵循 CC BY-SA 3.0 许可协议

当安装的 protoc 版本与 pom.xml 中列出的版本不匹配时,我遇到了这个问题。匹配版本解决了问题。在我的例子中,我不得不将我的协议版本切换回 2.4.1 以匹配 POM。

原文由 Nathan 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题