对于我的团队,我想配置 maven/eclipse build 以自动从 *.proto
生成 Java 代码文件(在使用 gRPC 的项目中)。当前需要运行 mvn generate-source
或 mvn protobuf:compile
(如插件 usage page )。或者相同的添加运行配置以调用 maven 目标 compile
.
每当刷新 Eclipse Maven 项目 (Alt+F5) 或重新启动 IDE 时,项目都会重建,但不会出现在 target/generated
中。 ,从而将项目变为红色。因此需要生成并刷新项目 (F5)。更新 Eclipse 需要在 .clathpath
中配置的源文件夹文件。
据我所知应该是 m2e 连接器,但我只能找到一个 https://github.com/masterzen/m2e-protoc-connector对于最老的谷歌 s plugin com.google.protobuf.tools:maven-protoc-plugin
,目前甚至没有在 https://github.com/grpc/grpc-java 中提及
我们使用完全引用/推荐
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
即:
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
相关:
最佳答案
我的团队没有使用 org.xolstice.maven.plugins:protobuf-maven-plugin
,而是使用了 com.github.os72:protoc-jar-maven-plugin
生成消息类。我相信它们是相同的,因为在幕后它们似乎都在使用 Google 的工具。
我没有为此插件使用任何 m2e 连接器(编辑: protoc-jar-maven-plugin
的 m2e 连接器与它捆绑在一起,因此无需额外安装需要,这就是为什么我似乎没有使用它,但技术上我是,但这并不重要)。不幸的是,.proto
文件中的更改不会“自动”传播到生成的 .java
文件,您需要手动运行 Maven 或触发要在 Eclipse 中构建的项目(下面的说明),但幸运的是,target/generated-sources
文件没有消失或清空,也没有像您描述的那样出现任何奇怪的情况。
If you want to rebuild the
.java
files from the.proto
classes without usingmvn clean compile
from the command line you can clean the Eclipse project . Project → Clean... → select your project → Select build option (only shows if you have "Build Automatically" from the Project menu is unchecked).
我能够在最新的 Eclipse Neon 中执行此操作(它可能也适用于以后的版本,但我不确定)。
下面是我正在使用的 POM。我认为不需要任何特殊解释,我的解决方案是简单地使用与您正在使用的插件不同的插件。 (如果需要一些解释,我会很乐意提供。)
<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>io.github.jacksonbailey</groupId>
<artifactId>protobuf-m2e-sample</artifactId>
<version>0.1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.github.os72</groupId>
<artifactId>protoc-jar-maven-plugin</artifactId>
<version>3.1.0.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<protocVersion>3.1.0</protocVersion>
<inputDirectories>
<include>src/main/resources</include>
</inputDirectories>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
关于java - 在 Eclipse IDE 中使用 maven/m2e 从 .proto 自动生成 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40426366/