Linking to generated Java protobuf code in Eclipse

2019-02-26 13:07发布

问题:

Here's the workflow that I'm trying to build

  • compile my proto files using a script, putting the generated src in a specified directory
  • link to the generated classes in Eclipse
  • compile my project

I am easily able to do this for C++ using Eclipse CDT: In my project I choose File->New->Other and choose File under General. Then I click on Advanced and select link to system file.

I haven't discovered how to do this for Java, though. Once answer here suggests creating another project with the generated code and make that a dependency to my project. This works but seems redundant.

Is there a way to directly link the generated protobuf Java classes to my project?

回答1:

Well Java doesn't really have a "link" phase. Your two options are really:

  • Build the generated code outside Eclipse, and then add a reference to the relevant directory or jar file
  • Include the generated code within Eclipse (e.g. by having a source path which includes the directory containing your generated code) and get Eclipse to build it along with the rest of your code.

I suspect that the first option will make it easier to keep the generated code well away from your real source, but the second option may make it easier to browse the generated source, and package everything up.