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

Pau*_*est 6 java eclipse protocol-buffers maven m2e

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

每当刷新Eclipse Maven项目(Alt+ F5)或重新启动IDE时,都会重建项目,但不会出现任何内容target/generated,从而将项目变为红色.所以需要生成并刷新project(F5).更新Eclipse需要在.clathpath文件中配置源文件夹.

据我所知,应该是m2e连接器,但我只能为最老的Google 找到一个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>
Run Code Online (Sandbox Code Playgroud)

那是:

<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>
Run Code Online (Sandbox Code Playgroud)

有关:

Cap*_*Man 8

而不是使用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是该文件没有消失或清空或任何奇怪的东西你所描述的。

如果您想.java.proto类重建文件而不使用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>
Run Code Online (Sandbox Code Playgroud)


Pau*_*est 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-或其他-ides

需要下载 os-maven-plugin-xxxFinal.jar (与你的 pomx.ml 中的版本)并将其放入目录中<ECLIPSE_HOME>/plugins

之后,Eclipse 将在项目 clean 上生成源代码,包括在 Maven -update 项目... ( Alt+ F5) 之后,但不会在 Project -> Build (或使用默认的自动构建)之后生成。同样在 IDE 启动时也无法编译。

是的,这是不合逻辑的:

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

PS 提出错误 507412