How do I get Eclipse to resolve classes generated with Maven 2?
Asked Answered
T

10

58

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?

Toed answered 28/7, 2009 at 8:13 Comment(2)
I posted this question and a (possible) answer as I was trying things out, partly to share what I learned, partly because, although I did eventually get it to work, I still don't really understand why.Toed
The question https://mcmap.net/q/217364/-m2e-and-having-maven-generated-source-folders-as-eclipse-source-folders/873282 seems to be related. The answer https://mcmap.net/q/217364/-m2e-and-having-maven-generated-source-folders-as-eclipse-source-folders suggests to use the build-helper-plugin eventually together with APT M2E connector. Currently, It does not work at my side with M2E and Eclipse Kepler.Shamanism
T
13

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.

Toed answered 28/7, 2009 at 8:21 Comment(4)
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)Toed
@HannoFietz: I have the same problem and can't find those options in the Maven section. Is there are different way for newer versions?Pasley
@neo - I don't know, I switched to IntelliJ 2 years ago. You might also want to check if any of the other answers help you with a workaround.Toed
I'd want to try it but I do not see these options in Eclipse Oxygen in Project->Properties>Maven window((( Does any one know what can I do?Bergerac
S
35

m2eclipse supports this. First, add the path to your build path:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/java/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Second, add support for that to m2e:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                      <pluginExecutions>
                        <pluginExecution>
                          <pluginExecutionFilter>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>build-helper-maven-plugin</artifactId>
                            <versionRange>[1.0,)</versionRange>
                            <goals>
                              <goal>parse-version</goal>
                              <goal>add-source</goal>
                              <goal>maven-version</goal>
                              <goal>add-resource</goal>
                              <goal>add-test-resource</goal>
                              <goal>add-test-source</goal>
                            </goals>
                          </pluginExecutionFilter>
                          <action>
                            <execute>
                              <runOnConfiguration>true</runOnConfiguration>
                              <runOnIncremental>true</runOnIncremental>
                            </execute>
                          </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

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.

Shamanism answered 27/11, 2013 at 18:0 Comment(3)
I was able to make this work under Luna by adding the Apt M2E Connector in the Eclipse Marketplace and then following step 1 above but omitting step 2. Thanks!Radack
By far the best answer that doesn't require fidgeting with Eclipse settings. To take this one step further I put the configuration in a maven profile that was activated by the property m2e.version.Audrieaudris
I have problem with Hibernate Model Generator, step 1 fixed my issue, I just change /java to /anotationsPainterly
S
27

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.

Salyer answered 28/7, 2009 at 8:36 Comment(3)
Hm, interesting. I was sort of hoping that m2eclipse would just tweak Eclipse's processes and it all magically works. :)Toed
above link needs to login, I have got this link for same search docs.sonatype.org/display/M2ECLIPSE/…Bluebeard
I followed this but added specific instructions for Eclipse Juno in my Answer. +1 for the linkDragonfly
T
13

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.

Toed answered 28/7, 2009 at 8:21 Comment(4)
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)Toed
@HannoFietz: I have the same problem and can't find those options in the Maven section. Is there are different way for newer versions?Pasley
@neo - I don't know, I switched to IntelliJ 2 years ago. You might also want to check if any of the other answers help you with a workaround.Toed
I'd want to try it but I do not see these options in Eclipse Oxygen in Project->Properties>Maven window((( Does any one know what can I do?Bergerac
A
13

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.

Arcboutant answered 29/7, 2009 at 8:36 Comment(2)
I was looking for a better solution to avoid doing this, but it looks like I don't have a choice.Charterhouse
+1 I suppose one advantage of this approach is that you don't auto-generate your classes on a regular basis when cleaning your primary project. In many situations, auto-generated classes remain static for much of a project lifespan.Brewing
D
9

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:

  1. Right-click project1 and select Properties
  2. Choose Java Build Path from the left and select the Libraries tab
  3. Click Add Class Folder
  4. Select the bin directory and click OK (project1/target/generated-sources/bin)
  5. Click OK and Refresh the project

As an added bonus you can also attach the source code:

  1. Click the arrow next to the new class folder you just created
  2. Click on Source attachment
  3. Click the Edit button
  4. Set the Path to /project1/target/generated-sources/axis2/src
  5. Click OK
Dragonfly answered 13/2, 2013 at 17:18 Comment(1)
+1 Note that you either have to enable automatic refresh or you manually have to refresh the project every time you run Maven from the command line. Otherwise, you will get really odd errors in Eclipse.Salyer
E
6
  1. Right-click project and select Properties
  2. Choose Java Build Path from the left and select the Source tab
  3. Click Add Folder
  4. Select the bin directory and click OK
  5. (project/target/generated-sources/xxxx) Click OK and Refresh the project
Enrico answered 30/5, 2016 at 21:40 Comment(1)
You sir solved the problem. None of the other solution worked. Thank you.Predestinate
P
4

How can I get IDE support (code completion etc.) for the generated code?

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.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

To fix eclipse I installed the Apt M2E Connector from the Eclipse Marketplace. I think things started working right after I restarted and then rebuilt all of my projects. I now see the following in my source dirs:

src/main/java
target/generated-sources
...

Feature!

Personalism answered 8/5, 2018 at 18:27 Comment(0)
D
2

Did you try to refresh the Eclipse project?

alt text
(source: oyvindhauge.com)

When an external tool generate new files or updates old ones, Eclipse will not be able to detect the change until the next request.

Another option would be to define a new Custom builder, specifying for that builder to "refresh resources upon completion":

alt text http://www.cs.lth.se/EDA180/2005/Verktyg/eclipse_refresh.gif

Daisey answered 28/7, 2009 at 8:21 Comment(2)
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.Toed
haha it worked for me after few cleans and refresh (on eclipse neon3). thank you! +1Catboat
B
2

Worked for me (But you will to have to follow this every time so you can add this path in pom.xml)

  • Right click on your project > Build Path > Configure Build Path
  • In sources tag, click on [Add Folder] button
  • Check target/generated-sources/annotations

enter image description here

Biogeochemistry answered 16/2, 2022 at 8:59 Comment(0)
S
0

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 system

  • update 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 directory

  • update the project (via Maven -> Update project...)

Example pom.xml:

<project>
  ...
  <build>
    <plugins>
      <!-- Require at least Java 6 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <!-- Generate .java files from .proto definitions -->
      <plugin>
        <groupId>org.xolstice.maven.plugins</groupId>
        <artifactId>protobuf-maven-plugin</artifactId>
        <version>0.5.1</version>
        <configuration>
          <protocExecutable>/usr/local/bin/protoc</protocExecutable>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.5.1</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Some additional notes:

  • if protoc executable is in the PATH the protocExecutable configuration entry can be omitted

  • test-only protobuf message definitions can be put into project's src/test/proto directory

  • I recommend installing Protocol Buffer Descriptor Editor (marketplace link)

Good luck!

Schleiermacher answered 16/5, 2018 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.