I'm using Google Protocol Buffers to generate some Java classes for my project. Using Maven 2 and its "antrun" plugin, these classes are freshly generated before compile, output to target/generated-sources and put on the classpath during the build. So building the project from the POM is no problem.
However, Eclipse doesn't know how to resolve the generated class, because the folder it's in doesn't seem to be on the IDE's classpath during development. I'm using m2eclipse and have it manage dependencies for me, so I had expected Maven to take care of this.
How can I get IDE support (code completion etc.) for the generated code?
What you should see in your project explorer is a container named "Maven Dependencies" in place of the usual "Referenced libraries". This means m2eclipse is managing your build path.
In my case, to achieve this, I checked "Include Modules" and unchecked "Skip Maven compiler plugin when processing resources" on the "Maven" section of Project->Properties.
I had this issue with code generated using Maven and wsdl2java and here's what I did in Eclipse Juno to resolve it. Assume my project is named project1:
As an added bonus you can also attach the source code:
Personally I resolved this problem by setting up the generated classes as a seperate project and made it a dependency in my main (non-generated) project. I was using wsdl2java to generate webservice classes so the "source" in my sub-project was the wdsl and xsds. Worked well even when the wsdl was changing regularly.
Typically I would add the m2e lifecycle-mapping plugin to the pom.xml file as described in @koppor's answer. However adding per-eclipse code to my pom.xml files is not an option at work which is mostly an IntelliJ shop.
My solution first adds the
build-helper-maven-plugin
to the pom.xml which works fine from the command line but not in eclipse.To fix eclipse I installed the Apt M2E Connector from the Eclipse Marketplace. I think things started working right after I rebooted and then rebuilt all of my projects. I now see the following in my source dirs:
Feature!
To generate Java source files from
.proto
files use Protocol Buffers Plugin which works out-of-the-box in eclipse Oxygen.Basic usage (see here for detailed description):
make sure that native
protoc
compiler is installed on your systemupdate your
pom.xml
file:make sure you use at least Java 6 (Java 7+ is recommended)
add plugin invocation
add the corresponding dependency for
com.google.protobuf:protobuf-java
put your .proto files inside project's
src/main/proto
directoryupdate the project (via
Maven -> Update project...
)Example
pom.xml
:Some additional notes:
if
protoc
executable is in the PATH theprotocExecutable
configuration entry can be omittedtest-only protobuf message definitions can be put into project's
src/test/proto
directoryI recommend installing Protocol Buffer Descriptor Editor (marketplace link)
Good luck!
m2eclipse supports this. First, add the path to your build path:
Second, add support for that to m2e:
The second step might not be necessary, if your eclipse installation has installed the "org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml" plugin. This plugin is available via Window -> Preferences -> Maven -> Discovery. Currently, that does not work here at Eclipse Kepler, therefore, I fetched the JAR (linked from the xml shown in the Catalog URL) and extracted the fragments from
org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml
by hand.