在 Eclipse IDE 中使用 maven/m2e 从 .proto 自动生成 Java

Automatically generate Java from .proto with maven/m2e in Eclipse IDE

对于我的团队,我想配置 maven/eclipse build 以从 *.proto 文件自动生成 Java 代码(在使用 gRPC). Currently one needs to run mvn generate-source or mvn protobuf:compile (as in plugin usage page 的项目中)。或者相同的是添加 运行 配置来调用 Maven 目标 compile.

每当刷新 Eclipse Maven 项目(Alt+F5)或 IDE 重新启动时,都会重建项目但没有target/generated 中应该出现什么,从而将项目变成红色。因此需要生成并刷新项目 (F5)。更新 Eclipse 需要在 .clathpath 文件中配置源文件夹。

据我所知应该是 m2e 连接器,但我只能找到一个 https://github.com/masterzen/m2e-protoc-connector for the oldest Googles plugin com.google.protobuf.tools:maven-protoc-plugin, that is even not mentioned currently at https://github.com/grpc/grpc-java

我们正好使用referenced/recommended

  <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 文件中的更改不会 "automatically" 传播到生成的 .java 文件,您需要手动 运行 Maven 或触发要在 Eclipse 中构建的项目(说明下面),但幸运的是,target/generated-sources 文件没有消失或清空,也没有像你描述的那样出现任何奇怪的情况。

If you want to rebuild the .java files from the .proto classes without using mvn 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>

protobuf-maven-plugin

感谢 sergei-ivanov 在 https://github.com/xolstice/protobuf-maven-plugin/issues/16, that gave link https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides 中的回答:

需要下载os-maven-plugin-x.x.x.Final.jar(你pomx.ml的那个版本),然后放到<ECLIPSE_HOME>/plugins目录下。

之后 Eclipse 将在项目干净时生成源代码,包括在 Maven 更新项目之后... (Alt+F5),但不是在 Project -> Build 之后(或使用默认的 Build Automatically)。同样在 IDE 启动时它不会编译。

是的,这是不合逻辑的:

Project - Clean 将生成并编译 Java source
但是
Project - Build 不会。

P.S。已提出 Bug 507412

eclipse和vscode都可以自动编译proto.

            <plugin>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>detect</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.32.1:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>