Maven的新手。我使用Maven构建了我的项目并生成了Protobuf可执行文件并为我的项目生成了源代码。我在Java中使用eclipse和编译。我似乎没有正确导入库,所以当通过eclipse查看生成的代码时我遇到了很多错误。
我收到错误Maven com.google cannot be resolved to type
我看了很多建议,似乎无法找到解决这个问题的方法。从我的想法来看,我的构建路径似乎是正确的。
<?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>
答案 0 :(得分:1)
Apache Maven要记住的一件事是,它的设计中的一个关键原则是&#34;约定优于配置&#34;的原则。这意味着如果我们坚持使用maven约定,那么我们的pom.xml文件会变得更小更简单。
而你似乎已经陷入困境了。构建的关键元素是:
在您构建jar文件时,您的pom应该正确声明包装:
<packaging>jar</packaging>
现在,我将专注于处理你的.proto文件,因为这似乎是你现在最麻烦的。
它需要的第一件事是protoc
可执行文件的可执行副本。
上面的示例pom.xml都是将maven存储库中的版本下载到${project.build.dir}
,即target
目录,和尝试使用/usr/local/bin/protoc
目的。
您可能会或可能不会实际使用后者。如果没有,那么:
如果您的target
目录包含protoc-3.5.1-1-osx-x86_64.exe
,那么我们可以使用它,否则:
com.google.protobuf
; protoc
链接,然后直接下载所需的版本。然后:
/usr/local/bin/
protoc
和使其可执行:
chmod a+x protoc
您已经发现了protobuf-maven-plugin。 设置此项是解决剩余问题的关键。
按照惯例,它希望在以下位置找到您的.proto文件:
src/
main/
proto/
下面有一个传统的java包结构。因此:
将.proto文件移动到此位置。
然后您的protobuf-maven-plugin
配置正常,但我怀疑您不需要:
<goal>test-compile</goal>
线。
最后,您应该完全删除maven-compiler-plugin
声明。
请记住,maven约定将用于正确设置。
现在手动安装了protoc
,我们可以从你的pom文件中删除一些大块的XML:
<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>