A Developer's Diary

Dec 13, 2012

Configuring build path in Eclipse for Maven projects

The .classpath file generated after executing maven mvn eclipse:eclipse looks like the following

<?xml version="1.0" encoding="UTF-8"?>
    <classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
    <classpathentry including="**/*.java" kind="src" path="src/main/java"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="output" path="target/classes"/>

When the same project is imported into eclipse, the build path is configured accordingly. The parameter including=**/*.java specifies that only java files are included in the class path.
In the example above, no other files are included in the class path and will throw FileNotFoundException if referenced

I observed the same error when I imported a java project into eclipse and converted it into a spring project. Although, the beans.xml file was in the correct folder, it was never part of the classpath

The Error Stacktrace
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)

Ways to correct the classpath
#1. Edit the Included: **/*.java section in eclipse to include **/*.xml

#2. Configure maven-eclipse-plugin in your maven project with sourceIncludes and sourceExcludes as below
 1 <project>
 2   [...]
 3   <build>
 4     [...]
 5     <plugins>
 6       [...]
 7       <plugin>
 8         <groupId>org.apache.maven.plugins</groupId>
 9         <artifactId>maven-eclipse-plugin</artifactId>
10         <version>2.9</version>
11         <configuration>
12           <sourceExcludes>
13             <sourceExclude>**/.svn/**</sourceExclude>
14           </sourceExcludes>
15           <sourceIncludes>
16             <sourceInclude>**/*.myExtension</sourceInclude>
17           </sourceIncludes>
18         </configuration>
19       </plugin>
20       [...]
21     </plugins>
22     [...]
23   </build>
24   [...]
25 </project>

#3. Specify resources separately in maven project
 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     [...]
 4     <build>
 5         <resources>
 6             <resource>
 7                 <directory>src/main/resources</directory>
 8             </resource>
 9         </resources>
10     </build>
11     [...]
12 </project>

The classpathentry for the above includes files with all other extensions except java
<?xml version="1.0" encoding="UTF-8"?>
    <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
    <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
    <classpathentry kind="output" path="target/classes"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>

Maven Eclipse Plugin

No comments :

Post a Comment