【问题标题】:Maven & Protobuf compile error: Cannot find symbol in package com.google.protobufMaven & Protobuf 编译错误:在包 com.google.protobuf 中找不到符号
【发布时间】:2013-03-03 06:43:56
【问题描述】:

我是 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

这里是 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 中。

我在 protobuf/java/target 中拿了 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 它时,我得到:

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

是的....正如您所说,我不知道设置 $LD_LIBRARY_PATH

我也跑了:

apt-get install protobuf-compiler

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

协议安装

我忘记了我为使 protoc 工作所做的工作,但是当我运行时

protoc --version

我明白了

libprotoc 2.5.0

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

我的问题类似于:

Problems using protobufs with java and scala

maven compilation failure

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

可能的相关性?

still not find package, after '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:

【问题讨论】:

  • +1 很好的问题组合。

标签: java maven maven-2 maven-3 protocol-buffers


【解决方案1】:

我遇到了同样的问题。 直接从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 版本和/或手动复制 jarfile。

干杯

【讨论】:

  • 在我的 pom 中将 2.4.1 更改为 2.5.0 也解决了我的问题
【解决方案2】:

当安装的 protoc 版本与 pom.xml 中列出的版本不匹配时,我遇到了这个问题。匹配版本解决了问题。就我而言,我必须将我的 protoc 版本切换回 2.4.1 以匹配 POM。

【讨论】:

  • 我使用 3.9.0 作为“protobuf.version”,使用 3.10.0 作为“protoc.version”并遇到了这个问题。将它们都更改为 3.9.0 修复了它。谢谢! --jsp
【解决方案3】:

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

【讨论】:

  • 这对我来说是最简单的解决方案!
【解决方案4】:

我的问题是主文件夹中的一个单元测试扩展类。 我解决了它:

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

【讨论】:

    【解决方案5】:

    对我来说,在构建脚本中使用后解决了

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

    【讨论】:

      【解决方案6】:

      问题在于您的 protoc 和 protobuf jar 之间的版本不匹配,保持相同的版本即可工作。对于依赖关系,您需要检查 pom.xml 文件。

      【讨论】:

        猜你喜欢
        • 2015-11-06
        • 2019-05-31
        • 1970-01-01
        • 2016-02-17
        • 1970-01-01
        • 2023-03-30
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多