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

问题描述 投票:21回答:5

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

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

我正在研究Ubuntu

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

ERROR

当我运行“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 INSTALLATION

我弄完了

./configure
make
make check
make install

print protobuf /,

mvn test
mvn install
mvn package

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

我忘记了我做protoc工作的所作所为,但是当我跑步时

protoc --version

我明白了

libprotoc 2.5.0

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

MY QUESTION IS SIMILAR TO:

Problems using protobufs with java and scala

maven compilation failure

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

POSSIBLE RELAVENCE?

still not find package, after 'mvn install'

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

有人可以帮忙吗?

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

PROGRESS

显然这是一个插件失败:

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:
java maven maven-2 maven-3 protocol-buffers
5个回答
12
投票

我有同样的问题。直接从谷歌建立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。

干杯


9
投票

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


9
投票

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


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

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

<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>
© www.soinside.com 2019 - 2024. All rights reserved.