Java Maven com.google无法解析为键入
马文是新来的。我使用Maven构建了我的项目,生成一个Protobuf可执行文件,并将源代码生成到我的项目中。我正在使用eclipse并用Java进行编译。我似乎没有正确导入库,因此在通过eclipse查看生成的代码时会出现很多错误 我收到错误Maven com.google无法解析为键入 我看了很多建议,似乎找不到解决这个问题的办法。从我的想法来看,我的构建路径似乎是正确的Java Maven com.google无法解析为键入,java,maven,protocol-buffers,Java,Maven,Protocol Buffers,马文是新来的。我使用Maven构建了我的项目,生成一个Protobuf可执行文件,并将源代码生成到我的项目中。我正在使用eclipse并用Java进行编译。我似乎没有正确导入库,因此在通过eclipse查看生成的代码时会出现很多错误 我收到错误Maven com.google无法解析为键入 我看了很多建议,似乎找不到解决这个问题的办法。从我的想法来看,我的构建路径似乎是正确的 <?xml version="1.0" encoding="UTF-8"?> <project xml
<?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>
ApacheMaven需要记住的一点是,其设计中的一个关键原则是约定优先于配置的原则。这意味着如果我们坚持maven约定,那么pom.xml文件就会变得更小、更简单 而你似乎已经陷入了困境。构建的关键元素包括: 您正在构建一个jar文件 它包含您编写的类 它包含将从源代码树中的.proto文件生成的类。 构建正确类型的工件 在构建jar文件时,pom应该正确声明打包:
<packaging>jar</packaging>
下面是一个传统的java包结构。因此:
将.proto文件移动到此位置
您的protobuf maven插件配置正常,但我怀疑您不需要:
<goal>test-compile</goal>
经过一番周旋,我发现有两个问题:正如你正确指出的,我的惯例是错误的。具体来说,我是在另一个目录/main/com/proto中查找我的proto文件。虽然这是错误的,但这并不是给我带来巨大问题的原因。让我头疼的是需要清除.m2/repository目录,这导致了与java代码的冲突。幸运的是,我遇到了一个帖子,引导我进入写作方向。我需要所有依赖项来重新下载。我将此标记为答案,因为它最终解决并清楚地解释了我在原始pom.xml文件中遇到的一些关键问题。
<goal>test-compile</goal>
<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>