How do I update my classpath with an Eclipse User Library via the maven eclipse plugin?
Author: Deron Eriksson
Description: This tutorial describes how to add an Eclipse User Library to a project's classpath using the maven eclipse plugin.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1) || Tomcat 6.0.14


In another tutorial, I showed how we can create a TomcatSW User Library in EclipseSW. A User Library is a set of jarW files. The TOMCAT_6.0.14_LIBRARY that I created is shown below.

TOMCAT_6.0.14_LIBRARY

I'd like to update my project's pom.xml file so that if I execute the eclipse:eclipse goal on my project, it will update the project's classpathW to include the Tomcat user library.

Here is the "mywebapp" project's original pom.xml file.

original pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.maventest</groupId>
  <artifactId>mywebapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mywebapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>mywebapp</finalName>
  </build>
</project>

I'll update the "mywebapp" project's pom.xml file to include a plugin reference to the maven-eclipse-plugin. I add a configuration section that specifies that the "org.eclipse.jdt.launching.JRE_CONTAINER" and "org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY" classpath containers will be added to the classpath when eclipse:eclipse is executed. The JRE_CONTAINER is specified since it is there already. Notice the format in which the Tomcat user library is specified (it begins with "org.eclipse.jdt.USER_LIBRARY/").

updated pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.maventest</groupId>
	<artifactId>mywebapp</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mywebapp Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>mywebapp</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<inherited>true</inherited>
				<configuration>
					<classpathContainers>
						<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
						<classpathContainer>org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY</classpathContainer>
					</classpathContainers>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Now, I'll run "mvn eclipse:eclipse" on the "mywebapp" project via an Eclipse external tool configuration.

Executing 'mvn eclipse:eclipse' on 'mywebapp' project

Before running eclipse:eclipse, the "mywebapp" .classpath file looked like this:

.classpath file before pom.xml update

<classpath>
  <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"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
</classpath>

After running eclipse:eclipse, the "mywebapp" .classpath file was updated to look like this:

.classpath file after pom.xml update

<classpath>
  <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"/>
  <classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar"/>
</classpath>

The "mywebapp" project now has the TOMCAT_6.0.14_LIBRARY user library included in its classpath!