views:

618

answers:

4

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?

+1  A: 
VonC
Yes, I did. I also did Project->Clean. It seems, the maven "compile" goal has to be invoked (see my own answer below), but I'm still very uncertain about how Maven-Eclipse integration really works.
Hanno Fietz
+3  A: 

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.

Hanno Fietz
m2eclipse: right, good point. +1 My answer was more general
VonC
I'm now wondering if m2eclipse ever managed any dependencies before I made those settings because my dependencies do look very different now. Weird. (I had only Maven-ized this project some days ago, maybe that was part of the problem)
Hanno Fietz
+2  A: 

m2eclipse doesn't support this. You must manually add the folder target/generated-sources as a source folder. When you tell m2eclipse to "Update Project Configuration", this will be overwritten and you have to restore it.

Also, make sure that Eclipse looks for changes in the workspace.

There might be some issues, though. Eventually, you'll run into errors that some class can't be compiled because some other class can't be resolved. Code completion will work, though. The root cause of this issue is that Eclipse gets confused when Maven changes class files in target.

To solve this, you must tell Eclipse to compile to a different place than Maven.

Aaron Digulla
Interesting link. +1
VonC
Hm, interesting. I was sort of hoping that m2eclipse would just tweak Eclipse's processes and it all magically works. :)
Hanno Fietz
+1  A: 

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.

Michael Rutherfurd
That's a good idea, too, I think.
Hanno Fietz