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

2022-09-04 21:08:33

对于我的团队,我想将maven/eclipse构建配置为从文件自动生成Java代码(在使用gRPC的项目中)。目前需要运行或(如插件使用页面)。或者是什么相同的添加运行配置来调用maven目标。*.protomvn generate-sourcemvn protobuf:compilecompile

每当 Eclipse Maven 项目刷新 (+) 或重新启动 IDE 时,都会重新生成项目,但没有应出现在 中的内容,从而将项目转换为红色。因此,需要生成并刷新项目()。更新 Eclipse 需要在文件中配置的源文件夹。AltF5target/generatedF5.clathpath

据我所知,这应该是m2e连接器,但我只能找到一个最古老的Google https://github.com/masterzen/m2e-protoc-connector,目前甚至没有提到 https://github.com/grpc/grpc-javas plugin com.google.protobuf.tools:maven-protoc-plugin

我们使用精确引用/推荐

  <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>

相关:


答案 1

我没有使用我的团队,而是使用com.github.os72:protoc-jar-maven-plugin来生成消息类。我相信他们是一样的,因为在引擎盖下,他们似乎都在使用谷歌的工具。org.xolstice.maven.plugins:protobuf-maven-plugin

我没有为此插件使用任何m2e连接器(编辑:的m2e连接器与它捆绑在一起,因此不需要额外的安装,这就是为什么我似乎没有使用一个,但从技术上讲,我是,但这并不重要)。不幸的是,文件中的更改不会“自动”传播到生成的文件,您需要手动运行Maven或触发要在Eclipse中构建的项目(下面的说明),但幸运的是,该文件不会消失或清空或任何像您描述的那样奇怪的东西。protoc-jar-maven-plugin.proto.javatarget/generated-sources

如果要从类中重建文件而不使用命令行,则可以清理 Eclipse 项目 。项目→清洁...→选择项目→选择生成选项(仅当未选中“项目”菜单中的“自动生成”时才显示)。.java.protomvn clean compile

我能够在最新的Eclipse Neon中做到这一点(它可能也适用于以后的Next 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>

答案 2

for protobuf-maven-plugin

感谢谢尔盖-伊万诺夫在 https://github.com/xolstice/protobuf-maven-plugin/issues/16 的答案,这给了链接 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更新项目之后...(+),但不能在 Project ->“生成”(或使用默认的“自动生成”)之后。同样在IDE启动时,它不会编译。AltF5

是的,这是不合逻辑的:

项目 - 清理将生成和编译 Java 源代码
,但
项目 - 生成不会。

附言:提出错误507412


推荐