简体   繁体   中英

Maven com.google cannot be resolved to type

New to Maven. I built my project using Maven and generate a Protobuf executable and generate the source to my project. I'm using eclipse and compiling in Java. I don't seem to have the library properly imported so I get a lot of errors when viewing the generated code through eclipse.

I am receiving error Maven com.google cannot be resolved to type

I've looked at numerous suggestions and can't seem to find a resolution to this problem. My build path seems to be correct from what I think.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.henosisknot.com</groupId>
<version>0.1.0</version>
<name>ChatBot-Henosisknot</name>
<url>henosisknot.com</url>
<description>Chatbot for henosisknot.com</description>
<artifactId>Chatbot-Henosisknot</artifactId>
<packaging>pom</packaging>
<profiles>
    <profile>
        <modules>
                <module>com</module>
        </modules>
      </profile>
</profiles>
<organization>
    <name>Henosisknot.com</name> 
 </organization>

<properties>

<!-- protobuf paths -->
<protobuf.input.directory>${project.basedir}/src/main/java/com/proto</protobuf.input.directory>
<protobuf.output.directory>${project.basedir}/src/</protobuf.output.directory>
<project.build.dir>${project.basedir}/build</project.build.dir>

<!-- library versions -->
<build-helper-maven-plugin.version>1.9.1</build-helper-maven-plugin.version>
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
<maven-shade-plugin.version>2.4.2</maven-shade-plugin.version>
<os-maven-plugin.version>1.4.1.Final</os-maven-plugin.version>
<protobuf.version>3.0.0</protobuf.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

</properties>


<dependencies>

    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.0.0</version>
    </dependency>

</dependencies>


 <build>
    <extensions>
    <!-- provides os.detected.classifier (i.e. linux-x86_64, osx-x86_64) property -->
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>${os-maven-plugin.version}</version>
        </extension>
    </extensions>

    <directory>${project.build.dir}/classes</directory>
    <outputDirectory>/users/andor/workspace/Chatbot-Henosisknot/build/classes</outputDirectory>

    <plugins>
        <!-- copy protoc binary into build directory -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>${maven-dependency-plugin.version}</version>
            <executions>
                <execution>
                    <id>copy-protoc</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.google.protobuf</groupId>
                                <artifactId>protoc</artifactId>
                                <version>${protobuf.version}</version>
                                <classifier>${os.detected.classifier}</classifier>
                                <type>exe</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.dir}</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- compile proto buffer files using copied protoc binary -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>${maven-antrun-plugin.version}</version>
            <executions>
                <execution>
                    <id>exec-protoc</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <target>
                            <property name="protoc.filename" value="protoc-${protobuf.version}-${os.detected.classifier}.exe"/>
                            <property name="protoc.filepath" value="${project.build.dir}/${protoc.filename}"/>
                            <chmod file="${protoc.filepath}" perm="ugo+rx"/>
                            <mkdir dir="${protobuf.output.directory}" />
                            <path id="protobuf.input.filepaths.path">
                                <fileset dir="${protobuf.input.directory}">
                                    <include name="**/*.proto"/>
                                </fileset>
                            </path>
                            <pathconvert pathsep=" " property="protobuf.input.filepaths" refid="protobuf.input.filepaths.path"/>
                            <exec executable="${protoc.filepath}" failonerror="true">
                                <arg value="-I"/>
                                <arg value="${protobuf.input.directory}"/>
                                <arg value="--java_out"/>
                                <arg value="${protobuf.output.directory}"/>
                                <arg line="${protobuf.input.filepaths}"/>
                            </exec>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.5.0</version>
        <configuration>
          <protocExecutable>/usr/local/bin/protoc</protocExecutable>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
        </plugin>
        <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <includes>
                        <include>**/com/**</include>
                    </includes>
                </configuration>
        </plugin>
           <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>main.java.com.chatbot.Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>com.chatbot.Main</mainClass>
            </configuration>
    </plugin>
    </plugins>
</build>

The one thing to remember with Apache Maven is that a key tenet in it's design is the principle of "convention over configuration". This means that if we stick to the maven conventions then our pom.xml files become a lot smaller and simpler.

And you seem to have configured your way into trouble. The key elements for your build are:

  • You're building a jar file
  • It contains classes that you have written
  • It contains classes that will be generated from .proto files that are in your source tree.

Build the right kind of artifact

As you're building a jar file, your pom should declare the packaging correctly:

<packaging>jar</packaging>

Process the .proto message files

Now, I'm going to focus on processing your .proto files because that seems to be causing you the most trouble right now.

Install protoc if necessary

The first thing it needs is an executable copy of the protoc executable. Your sample pom.xml above is both downloading a version from the maven repository into ${project.build.dir} , aka the target directory, and trying to use /usr/local/bin/protoc for the same purpose. You may or may not actually have the latter in place. If not then:

If your target directory contains protoc-3.5.1-1-osx-x86_64.exe then we can use that, otherwise:

  1. Point your browser at search.maven.org ;
  2. Search for com.google.protobuf ;
  3. Click on the protoc link and directly download the version that you want.

Then:

  1. Copy the downloaded artifact to /usr/local/bin/
  2. Rename it to protoc , and
  3. Make it executable:

    chmod a+x protoc

Set up the protobuf-maven-plugin

You have already discovered the protobuf-maven-plugin . Setting this up is the key to resolving your remaining problems.

By convention, it expects to find your .proto files in:

src/
  main/
    proto/

with a conventional java package structure below that. Therefore:

Move the .proto files into this location.

Your protobuf-maven-plugin configuration is then ok, although I suspect that you do not need the:

 <goal>test-compile</goal>

line.

Last steps

Finally, you should completely remove the maven-compiler-plugin declaration. Remember that maven conventions will be used to set this up properly.

As protoc is now hand installed, we can get rid of some large slabs of XML from your pom file:

<description>ffffff</description>
<artifactId>fffffff</artifactId>

<!-- you're building a jar -->
<packaging>jar</packaging>

<!-- the profiles section was meaningless -->

<organization>
    <name>fffff</name>
</organization>

<properties>

    <!-- protobuf paths -->
    <!-- you were fighting maven with these properties -->

    <!-- library versions -->
    <maven-shade-plugin.version>2.4.2</maven-shade-plugin.version>

    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

</properties>

<dependencies>

    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>3.0.0</version>
    </dependency>

</dependencies>

<build>
    <plugins>

        <plugin>
            <!-- generate java from .proto files -->
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.1</version>
            <configuration>
                <protocExecutable>/usr/local/bin/protoc</protocExecutable>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- maven compiler plugin declaration is usually redundant unless you want a specific version -->

        <!-- I have not addressed this... -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            ...
        </plugin>
    </plugins>
</build>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM