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

15

对于我的团队,我想配置Maven / Eclipse构建,以自动从使用gRPC的项目中的*.proto文件生成Java代码。目前需要运行mvn generate-sourcemvn protobuf:compile(如插件usage page中所述)。或者相同的操作是添加运行配置以调用Maven目标compile

每当刷新Eclipse Maven项目(Alt+F5)或重新启动IDE时,项目将被重建,但没有出现在target/generated中的内容,从而使项目变为红色。因此,需要生成并刷新项目(F5)。更新:Eclipse已在.clathpath文件中配置所需的源文件夹。

我知道应该是m2e连接器,但我只能找到一个https://github.com/masterzen/m2e-protoc-connector适用于最旧的Googles插件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>

相关文章:


在执行干净/重启操作时,target/generated-sources 文件夹中应该存在 Java 源代码文件,但实际上是空的。 - Paul Verest
这正是protoc Protocol Buffers编译器的作用:从.proto文件中生成Java代码。 - Paul Verest
@Gimby 我很想只使用Java。http://stackoverflow.com/questions/40425211/java-idl-for-protobuf-protocl-buffers-in-java - Paul Verest
1
我觉得奇怪的是,你的Eclipse会删除target/generated文件夹。即使进行了硬刷新,它也不应该这么做。一旦将该文件夹添加到构建路径中(必须手动完成-- 如果未通过buildhelper-maven-plugin完成),Eclipse应始终保留它。你使用的Eclipse版本是哪个?也许可以升级一下,连同m2e一起使用。 - Tunaki
1
你能发布你当前的POM吗?另外,你能回答我之前留下的评论吗?我在插件的源代码中看到它正确地将生成的文件添加到了maven项目中(https://github.com/xolstice/protobuf-maven-plugin/blob/master/src/main/java/org/xolstice/maven/plugin/protobuf/AbstractProtocCompileMojo.java#L72),所以在Eclipse中,一旦你将其添加为源文件夹,并且不要在没有重新编译的情况下进行“clean”,它应该仍然可以工作。 - Tunaki
显示剩余4条评论
3个回答

12

我的团队使用 com.github.os72:protoc-jar-maven-plugin 代替了 org.xolstice.maven.plugins:protobuf-maven-plugin 来生成消息类。我相信它们是一样的,因为在幕后它们似乎都在使用来自 Google 的工具。

对于此插件,我没有使用任何 m2e 连接器 (编辑: protoc-jar-maven-plugin 的 m2e 连接器已经捆绑在其中,因此不需要额外安装,这就是为什么看起来我没有使用连接器,但是从技术上讲我确实使用了一个,但这并不重要)。不幸的是,.proto 文件的更改不会“自动”传播到生成的 .java 文件中,您需要手动运行 Maven 或在 Eclipse 中触发项目构建 (以下是说明),但幸运的是,target/generated-sources 文件不会消失、清空或出现任何奇怪的情况,就像您所描述的那样。

如果您想要重新从 .proto 类重建 .java 文件而又不想使用命令行的 mvn clean compile 命令,您可以清除 Eclipse 项目。 项目 → 清除... → 选择您的项目 → 选择构建选项(只有在从“项目”菜单中取消选中“自动构建”时才会显示)。

我能够在最新的 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>

点赞。谢谢。但是这并没有回答“如何自动化”的问题。 - Paul Verest
@PaulVerest 我告诉过你这只是一个部分回答,但你还是要我回答 :) -- 至于第二条评论,我原以为它不会,但实际上确实可以。我正在更新答案(它仍然不如你想象的那么“自动化”)。 - Captain Man
1
@PaulVerest 已更新。但是如果我运行 mvn clean 然后打开 Eclipse,.java 文件不会在初始工作区构建时生成,但是如果我通过 Eclipse 清理项目,则可以正常工作。 - Captain Man
我已经找到了关于 protobuf-maven-plugin 的答案。如果能够为 protoc-jar-maven-plugin 提供相同的功能就太好了。示例代码可以在 https://github.com/paulvi/com.example.grpc.protoc-jar-maven-plugin 找到。 - Paul Verest
@PaulVerest,因为有人点赞了这个回答,所以我又回来了。我知道这是一个古老的答案。过去一年左右我没有使用gRPC/Protobuf,但自从看到这个答案后,我感到非常自信,我已经使用了一个能够正确构建target/generated-sources并在编辑proto文件时将target/generated-sources作为源文件夹的Protobuf插件。Paul,如果你已经解决了这个问题,我认为添加这个信息会很好,因为似乎没有多少人使用这些东西,而且这可能仍然是搜索引擎中的热门结果。:)无论如何都可以。 - Captain Man
显示剩余2条评论

3

针对protobuf-maven-plugin

感谢sergei-ivanov在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-update project后... (Alt+F5),但不会在Project -> Build (或默认的Build Automatically)之后。也不会在启动IDE时编译。

是的,这很不合逻辑:

Project - Clean会生成和编译Java源代码
但是
Project - Build 将不会。

P.S. 提出Bug 507412


Eclipse的Maven集成实在是有点奇怪,因为它并没有将pom.xml作为构建的权威来源,而是扫描它以创建一个权威的Eclipse构建配置,这在某些情况下似乎总是“有点偏差”。因此,在进行干净的构建时,Eclipse会深度扫描所有文件夹以“找到”新的内容,但在“构建”中,它往往会信任其备用的Maven配置宇宙,这经常会错过生成的项目。m2e插件不过是一个“通知Eclipse重新扫描”的插件。 - Edwin Buck
我的答案同时以相同的方式、相同的限制条件构建,我是否漏掉了什么? - Captain Man
1
一个问题已添加为评论的答案。无论如何,悬赏都是你的。 - Paul Verest

1

无论是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>

请参见:https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接