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

十年热恋 提交于 2019-12-03 22:18:12

I had the same problem. building the protobuf sources from google directly (I used 2.5.0) and doing

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

fixed the problem for me.

In my earlier trials I noticed, that the jar-file in /root/.m2/repository/com/google/protobuf/protobuf-java/2.5.0/ was missing.

Maybe try to use version 2.5.0 in the pom.xml and/or copying the jarfile manually.

cheers

I had this problem when there was a mismatch between the protoc version installed and the version listed in the pom. Matching the versions fixed the problem. In my case, I had to switch my protoc version back to 2.4.1 to match the POM.

The protoc --version has to be the same version to as set in pom.xml file (protobuf-java-2.5.0.jar).

Valdis Vitolins

My problem was that one unit test extended class from main folder. I solved it with:

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

For me, it is resolved after using below in build script

<clearOutputDirectory>false</clearOutputDirectory

Complete build script

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!