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

For my team, I would like to configure the maven / eclipse build to automatically generate Java code from *.proto files (in a project that uses gRPC ). Currently, you need to run mvn generate-source or mvn protobuf:compile (as in the plugin use page ). Or something the same, add a launch configuration to invoke the maven compile target.

Whenever an Eclipse Maven project is updated ( Alt + F5 ) or the IDE is restarted, the project is rebuilt, but without what should appear in target/generated , thereby turning the project into red. Therefore, you need to create and update the project ( F5 ). UPDATE Eclipse required that the source folders be configured in a .clathpath file.

As I know, this should be the m2e connector, but I could only find the https://github.com/masterzen/m2e-protoc-connector for the oldest google s plugin com.google.protobuf.tools:maven-protoc-plugin , which is not even mentioned currently at https://github.com/grpc/grpc-java

We use the exact / recommended

  <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> 

i.e:

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

Connected:

+4
java eclipse maven m2e protocol-buffers
Nov 04 '16 at 15:27
source share
2 answers

for protobuf-maven-plugin

Thanks to sergei-ivanov answer in https://github.com/xolstice/protobuf-maven-plugin/issues/16 , which gave the link https://github.com/trustin/os-maven-plugin#issues-with-eclipse -m2e-or-other-ides :

You need to download os-maven-plugin-xxxFinal.jar (the version as in your pomx.ml), and place it in the <ECLIPSE_HOME>/plugins directory.

After that, Eclipse will generate the project source code clean, including after the Maven project -update ... ( Alt + F5 ), but not after Project → Build (or with the built-in default function automatically). Also, when starting the IDE, it will not compile.

Yes, this is illogical:

Project - Clean will generate and compile Java source code
but
Project - assembly will not.

PS Raised Error 507412

0
Nov 11 '16 at 16:01
source share

Instead of using org.xolstice.maven.plugins:protobuf-maven-plugin my team used com.github.os72:protoc-jar-maven-plugin to generate message classes. I believe that they are the same, since under the hood they all seem to use Google tools.

I do not use any m2e connectors for this plugin ( Edit: protoc-jar-maven-plugin The m2e connection cable comes with it, so no additional installation is required, so it seemed to me that I did not use one, but technically I was, but it doesn't really matter). Unfortunately, the changes in the .proto file .proto not automatically apply to the generated .java files, you need to manually start Maven or run the project that will be created in Eclipse (instructions below), but, fortunately, the target/generated-sources file does not disappear or not empty, or something strange, like what you describe.

If you want to rebuild .java files from .proto classes without using mvn clean compile from the command line, you can clean up the Eclipse project. Project → Clean ... → select your project → Choose an assembly option (only displayed if you have “Build automatically” in the “Project” menu).

I was able to do this in the latest Eclipse Neon (it will probably work in later versions too, but I don’t know for sure).

Below is the POM I am using. I do not think this requires any special explanation, my solution is to just use a different plugin than the one you are using. (If you need some explanation, I will be happy to provide it, though.)

 <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> 
+3
Nov 09 '16 at 15:47
source share



All Articles