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

Maven & Protobuf compile error: Cannot find symbol in package com.google.protobuf

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

我正在尝试" mvn包"一个包含许多" .proto"文件和一个pom.xml文件的项目……

我正在使用Ubuntu

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

错误

当我运行" mvn软件包"时,出现以下错误:

1
2
3
...
Compiling 11 source files to .../target/classes
...

我收到很多这样的错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<project>
  <modelVersion>4.0.0</modelVersion>
  <parent>
     <groupId>*****</groupId>
     *****</artifactId>
     <version>1.0-SNAPSHOT</version>
  </parent>
  *****</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>
                protobuf-java</artifactId>
                <version>2.4.1</version>
                <scope>compile</scope>
            </dependency>
  </dependencies>
  <build>
    <sourceDirectory>${project.basedir}/src</sourceDirectory>
        <plugins>
            <plugin>
               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>
                    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">
                                         
                                         
                                           
                                    </exec>
                                </tasks>
                                <sourceRoot>${target.gen.source.path}</sourceRoot>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
         </plugins>
     </build>
</project>

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

PROTOBUF安装

我弄完了

1
2
3
4
./configure
make
make check
make install

在protobuf /中,

1
2
3
mvn test
mvn install
mvn package

在protobuf / java中。

我把瓶子放在protobuf / java / target

并通过运行将其添加到我的Maven仓库中:

1
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时,我得到:

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

是的....正如您所知,我对设置$ LD_LIBRARY_PATH毫无头绪

我还跑了:

1
apt-get install protobuf-compiler

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

协议安装

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

1
protoc --version

我懂了

1
libprotoc 2.5.0

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

我的问题类似于:

在Java和Scala中使用protobufs的问题

Maven编译失败

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

可能的关系?

在mvn安装后仍然找不到包

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

有人可以帮忙吗?

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

进展

显然是插件失败:

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

1
[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:


我有同样的问题。
直接从Google构建protobuf源(我使用2.5.0)并执行

1
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以匹配POM。


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


对我来说,在构建脚本中使用以下命令后即可解决

1
<clearOutputDirectory>false</clearOutputDirectory

完整的构建脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                os-maven-plugin</artifactId>
                <version>1.5.0.Final</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- Allow tests to call classes in main folder -->

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    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>