How to force Eclipse to allow classes created using Maven 2?

I use Google protocol buffers to create some Java classes for my project. Using Maven 2 and its antrun plugin, these classes are just generated before compilation, output to the target / generated sources, and placed in the classpath at build time. Therefore, creating a project with POM is not a problem.

However, Eclipse does not know how to resolve the generated class, because the folder in which it is located does not appear to be in the IDE class path at design time. I use m2eclipse and manage dependencies for me, so I expected Maven to take care of this.

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

+48
eclipse maven-2 code-generation protocol-buffers
Jul 28 '09 at 8:13
source share
9 answers

What you should see in your project explorer is a container named "Maven Dependencies" instead of the usual "Referenced Libraries". This means m2eclipse controls your build path.

In my case, for this I checked "Enable Modules" and unchecked the "Skip Maven compiler plugin when processing resources" checkbox in the "Maven" section in Project-> Properties.

+13
Jul 28 '09 at 8:21
source share

m2eclipse does not support this. You must manually add the target/generated-sources folder as the source folder. When you point m2eclipse to "Update project configuration", it will be overwritten and you must restore it.

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

However, there may be some problems. In the end, you will encounter errors that cannot be compiled by any class, because another class cannot be resolved. However, code completion will work. The main reason for this problem is that Eclipse gets confused when Maven changes the class files in target .

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

+27
Jul 28 '09 at 8:36
source share

m2eclipse supports this. First add the path to the 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> 

Secondly, add support for 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 may not be necessary if the plugin "org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml" is installed in your eclipse installation. This plugin is available through Window -> Preferences -> Maven -> Discovery. This does not currently work here in Eclipse Kepler, so I got the JAR (associated with the xml specified in the directory url ) and extracted the fragments from org.eclipse.m2e.discovery.lifecyclemapping.buildhelper.xml manually.

+22
Nov 27 '13 at 18:00
source share

Personally, I solved this problem by setting up the generated classes as a separate project and making it dependent on my main (non-generated) project. I used wsdl2java to generate webservice classes, so wdsl and xsds were the "source" in my subproject. Worked well even when wsdl was constantly changing.

+12
Jul 29 '09 at 8:36
source share

I had this problem with code generated using Maven and wsdl2java, and here is what I did in Eclipse Juno to solve it. Suppose my project is called project1:

  • Right-click project1 and select Properties
  • Select the Java Build Path on the left and select the Libraries tab
  • Click Add Class
  • Select the bin directory and click OK (project1 / target / generated-sources / bin)
  • Click OK and Refresh Project

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

  • Click the arrow next to the new class folder you created
  • Click the Source link
  • Click the "Edit" button.
  • Set the path to / project 1 / target / generated-sources / axis2 / src
  • Click OK
+9
Feb 13 '13 at 17:18
source share

Have you tried updating the Eclipse project?

alt text
(source: oyvindhauge.com )

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

Another option is to define a new custom linker by specifying “update resources upon completion” for that linker:

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

+2
Jul 28 '09 at 8:21
source share
  • Right-click the project and select "Properties"
  • Select the Java Build Path on the left and select the Source tab
  • Click "Add Folder"
  • Select the bin directory and click OK
  • (project / target / generated-sources / xxxx) Click OK and Refresh Project
+2
May 30 '16 at 21:40
source share

How to get IDE support (code completion, etc.) for generated code?

Normally, I would add the m2e plugin to display the life cycle in the pom.xml file, as described in @koppor's answer. However, adding per-eclipse code to my pom.xml files is not an option for work, which is basically an IntelliJ store.

My solution first adds build-helper-maven-plugin to 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 the eclipse, I installed the Apt M2E Connector on the Eclipse Marketplace. I think that everything worked right after the reboot, and then rebuilt all my projects. Now I see the following in my sources:

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

Feature!

+1
May 08 '18 at 18:27
source share

To generate Java source files from .proto files .proto use the Protocol Buffers plugin that runs in .proto Oxygen.

Main use ( see here for a detailed description ):

  • make sure protoc compiler protoc installed on your system

  • update the pom.xml file:

    • make sure you use at least Java 6 (Java 7+ recommended)

    • add plugin call

    • add the appropriate dependency for com.google.protobuf:protobuf-java

  • put your .proto files in the src/main/proto project 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:

Good luck

0
May 16 '18 at 14:40
source share



All Articles