M2E and having maven generated source folders as eclipse source folders

EclipseMavenM2eclipse

Eclipse Problem Overview


I have a maven project in eclipse and have maven goals that run annotation processors to generate code. The output folder for this code is target/generated-sources/apt.

In order for Eclipse to see this generated code I need to add target/generated-sources/apt as a source folder to the Eclipse project.

However, this causes there to be an error of type "Maven Configuration Problem" saying

> Project configuration is not up-to-date with pom.xml. Run project > configuration update

I think I understand why this is the case as Eclipse has a different set of source folders to Maven's set. But I need this different set, as I need Eclipse to be able to see the generated source folders...

When doing a pure Maven built, these source folders will be included in the build, by Maven.

BTW, I have upgraded to the official Eclipse release of the Maven Eclipse plugin, m2e 1.0 - what used to be m2eclipse. I'd like to see if I can find a work around/solution to this with the m2e plugin before I have to go back to the old m2eclipse version.

Eclipse Solutions


Solution 1 - Eclipse

You need to attach the source directory with the build-helper-plugin.

Like so:

 <plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<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>

You will also need to:

Solution 2 - Eclipse

Right-click the Error message:

> Project configuration is not up-to-date with pom.xml Run project > configuration update

in the Problems View and select Quick Fix and click Finish to select the default Update project configuration. This fixes it.

Solution 3 - Eclipse

After switching to new versions of m2e/maven/apt,... i had builderrors because of the duplicated files, caused by the added buildpath by the buildhelper, so i needed to remove the "apt-generated"-Folders from the buildhelper.

To fix the Problem in Eclipse, not adding the "apt-generated"-folder via Update Maven Configuration in M2E, i've written a M2E Plugin to fix this problem. It adds the outputDirectories configured in the maven-apt-plugin to the buildpath of the Project.

https://apt-m2e.googlecode.com

Solution 4 - Eclipse

In m2e 1.0 the handling of Maven plugins has changed. You might be lacking a specific m2e extension for your code generating plugin. Here is all the documentation I managed to find.

This bug report may also be relevant.

Solution 5 - Eclipse

https://bugs.eclipse.org/bugs/show_bug.cgi?id=350081

request on CXF JIRA (see 1) to add lifecycle mappings in the cxf-codegen-plugin itself. This would require m2e 1.1 but I believe it is better approach than having connectors built outside of cxf project, assuming that lifecycle mapping API would change less frequently than cxf-codegen-plugin and cxf.

Solution 6 - Eclipse

You can also use the buildhelper m2e connector available in the discovery catalog. I'm using Eclipse 3.7

Solution 7 - Eclipse

Eclipse Java EE IDE for Web Developers. Version: Juno Service Release 1

mvn archetype:generate \
   -DarchetypeGroupId=org.codehaus.mojo \
   -DarchetypeArtifactId=gwt-maven-plugin \
   -DarchetypeVersion=2.5.0

mvn clean install

work perfectly.

But in eclipse I have the same error on Asinc class.

Just press F5 on project. Fix this problem.

Solution 8 - Eclipse

This was what I found that worked good using spring 3.1.1 which does have the 3.0.6 version as well in it. Once I got the plugins setup and put into the correct area of the pom and included the argline and endorseddirs to have the java sources put out into the target/generated-sources/cxf folder then maven generated the sources ok.

....

 <properties>...

   <dependencyManagement>
      <dependencies>.....
   </dependencyManagement>

<dependencies>
   <dependency>....

</dependencies>



<!-- *************************** Build process ************************************* -->
<build>
    <finalName>eSurety</finalName>
    <plugins>
        <!-- Force Java 6 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- Deployent on AS from console
        <plugin>
            <groupId>org.jboss.as.plugins</groupId>
            <artifactId>jboss-as-maven-plugin</artifactId>
            <version>${version.jboss.as.maven.plugin}</version>
        </plugin>
        -->
        
        <!-- wildbill added tomcat plugin -->
        <plugin>
    		<groupId>org.apache.tomcat.maven</groupId>
    		<artifactId>tomcat7-maven-plugin</artifactId>
    		<version>2.0</version>        		
  		</plugin>
        
        <!-- Surefire plugin before 2.9 version is buggy. No need to declare here,
              it's being referenced below w/ the version
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12</version>
        </plugin>
        -->

		<!-- developer added these -->   
		<plugin>
        	<groupId>org.apache.maven.plugins</groupId>
        	<artifactId>maven-compiler-plugin</artifactId>
        	<configuration>
            	<compilerArguments>
               		<endorseddirs>target/generated-sources/cxf</endorseddirs>
            	</compilerArguments>
        	</configuration>
    	</plugin>
    	<plugin>
        	<groupId>org.apache.maven.plugins</groupId>
        	<artifactId>maven-surefire-plugin</artifactId>
        	<version>2.12</version>
        	<configuration>
            	<forkMode>once</forkMode>
            	<argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
        	</configuration>
		</plugin>    		
     	<plugin>
			<groupId>org.apache.maven.plugins</groupId>
		    <artifactId>maven-compiler-plugin</artifactId>
		    <configuration>
		    	<compilerArguments>
		        	<endorseddirs>target/generated-sources/cxf</endorseddirs>
		        </compilerArguments>
		    </configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
		    <artifactId>maven-surefire-plugin</artifactId>
		    <configuration>
		    	<forkMode>once</forkMode>
		        <argLine>-Djava.endorsed.dirs=target/generated-sources/cxf</argLine>
		    </configuration>
		</plugin>                	    
	    <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>				       
			<configuration>
				<artifactItems>
			    	<artifactItem>
			        	<groupId>javax.xml.bind</groupId>
			        	<artifactId>jaxb-api</artifactId>
			        	<version>2.2</version>
			    	</artifactItem>
			    	<artifactItem>
			    		<groupId>javax.xml.ws</groupId>
			        	<artifactId>jaxws-api</artifactId>
			        	<version>2.2</version>
			    	</artifactItem>
			   	</artifactItems>
			    <outputDirectory>target/generated-sources/cxf</outputDirectory>
			</configuration>				      
		</plugin>					    	    				  
    </plugins>
</build>

  

<!-- *********************** Profiles ************************************ -->
<profiles>
    <profile>
        <!-- When built in OpenShift the 'openshift' profile will be 
            used when invoking mvn. -->
        <!-- Use this profile for any OpenShift specific customization 
            your app will need. -->
        <!-- By default that is to put the resulting archive into the 
            'deployments' folder. -->
        <!-- http://maven.apache.org/guides/mini/guide-building-for-different-environments.html -->
        <id>projName</id>
        <build>
            <plugins>                	                                
            	<plugin>
				    <groupId>org.apache.cxf</groupId>
				    <artifactId>cxf-codegen-plugin</artifactId>
				    <version>2.5.2</version>					    
				    <executions>
			        	<execution>
			          		<id>process-sources</id>
			             	<phase>generate-sources</phase>				            				            				      					  
				    		<configuration>
				        		<fork>once</fork>
				        		<additionalJvmArgs>-Djava.endorsed.dirs=target/generated-sources/cxf</additionalJvmArgs>					        		      
				    		</configuration>
				     		<goals>				                
			                	<goal>wsdl2java</goal>
			             	</goals>
				    	</execution>
				    </executions>					    
				    <dependencies>
				        <dependency>
				           <groupId>com.sun.xml.bind</groupId>
				           <artifactId>jaxb-impl</artifactId>
				           <version>2.2</version>
				        </dependency>
				        <dependency>
				           <groupId>com.sun.xml.bind</groupId>
				           <artifactId>jaxb-xjc</artifactId>
				           <version>2.2</version>
				        </dependency>
				     </dependencies>
				</plugin>
				
				<!-- Actual war created in default target dir -->
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.2</version>                                               
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

If your wsdl folder is in ${basedir}/src/main/resources it'll find it automatically

Hope this helps! ~wildbill

Solution 9 - Eclipse

In case for some reason you can't use the build helper plugin the easiest way (albeit not as convenient and somewhat tedious) I have found to deal with this is:

  1. Separate the generated source code into its own project or sub module.
  2. You will want to keep this project predominately closed or not imported into Eclipse when you are working on the parent project.
  3. In the parent project that needs the generated code make sure to now depend on the generated source code project via Maven pom dependency.
  4. When you need to update the generated code go to the generated code project and run mvn install. Now refresh the parent project by right clicking and selecting Maven->Update Project...

This generally works well for projects that use a semi static source for code generation such as SOAP WSDLs (Apache CXF) or code generated from a database (jOOQ). For APT and other AspectJ-like-code it doesn't work as well because you are editing the source frequently.

Solution 10 - Eclipse

For new visitors to this question -

build helper maven plugins and m2e connectors go hand in hand. Older versions (before 2.0) of build helpers have moved into an eclipse archive link

build helper versions archived

Pick the correct link from the list and add it as an eclipse update site. It should ask you for a bunch (seriously.. a huge bunch ) of eclipse updates . Please accept and you are good to go.

Solution 11 - Eclipse

the configuration to the build helper plugin did work for us.

but be aware, that the destination folder always has to be equal to the configuration of the plugin u're using for the annotation processing itself.

for example the maven-processor-plugin uses the target folder ${project.build.directory}/generated-sources/apt as default. if you wish another destination for your generated source files you can set it by the tag as shown below.

<plugin>
<groupId>org.bsc.maven</groupId>
				<artifactId>maven-processor-plugin</artifactId>
				<version>2.1.1</version>
				<executions>
					<execution>
						<id>process</id>
						<goals>
							<goal>process</goal>
						</goals>
						<phase>process-sources</phase>
						<configuration>
							<defaultOutputDirectory>apt_generated</defaultOutputDirectory>
							<processors>
								<processor>com.any.processor.invoker</processor>
							</processors>
						</configuration>
					</execution>
				</executions>
			</plugin>

Solution 12 - Eclipse

Here is the solution

  1. Open Marker View (Window > Show View
  2. Right-click on the Error message
  3. Select Quick Fix
  4. Click Finish

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael WilesView Question on Stackoverflow
Solution 1 - EclipseMichael-OView Answer on Stackoverflow
Solution 2 - EclipsepeterView Answer on Stackoverflow
Solution 3 - EclipseStefan WoView Answer on Stackoverflow
Solution 4 - EclipseNicola MusattiView Answer on Stackoverflow
Solution 5 - EclipseMike GrandmaisonView Answer on Stackoverflow
Solution 6 - EclipseCédric VidalView Answer on Stackoverflow
Solution 7 - EclipseburtsevygView Answer on Stackoverflow
Solution 8 - EclipseWildBillView Answer on Stackoverflow
Solution 9 - EclipseAdam GentView Answer on Stackoverflow
Solution 10 - EclipseGautamView Answer on Stackoverflow
Solution 11 - EclipseHannes KoglerView Answer on Stackoverflow
Solution 12 - EclipseimeshView Answer on Stackoverflow