run a maven web application in Tomcat from Eclipse
Contents
run a maven web application in Tomcat from Eclipse
An Eclipse User Library can be used to represent a set of jar files. This user library can be added to a project’s classpath. Thus, a user library can be a convenient way to add a set of jar files (rather than individual jar files) to a project’s build path. Here, I’ll create a user library for a set of Tomcat jar files.
To create a user library, you can go to Window → Preferences and go to Java → Build Path → User Libraries. I’ll click the New button to create a new user library.
I named my new user library “TOMCAT_6.0.14_LIBRARY” and clicked OK.
I selected TOMCAT_6.0.14_LIBRARY and clicked the “Add JARs” button.
I browsed to my Tomcat bin directory and selected 3 jar files, including the bootstrap.jar file. I added these to my user library.
Next, I clicked Add JARs again and this time browsed to my Tomcat lib directory and selected several jar files, shown below.
When done, my TOMCAT_6.0.14_LIBRARY consisted of several jar files, shown below. I clicked OK to save this library.
After doing this, if I have a web application project that will run in Tomcat in Eclipse, I can add the TOMCAT_6.0.14_LIBRARY to the classpath to have all the necessary Tomcat jar files automatically added to the project’s classpath.
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
4.0.0 com.maventest mywebapp war 1.0-SNAPSHOT mywebapp Maven Webapp http://maven.apache.org junit junit 3.8.1 test mywebapp
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
4.0.0 com.maventest mywebapp war 1.0-SNAPSHOT mywebapp Maven Webapp http://maven.apache.org junit junit 3.8.1 test mywebapp org.apache.maven.plugins maven-eclipse-plugin true org.eclipse.jdt.launching.JRE_CONTAINER org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY
Now, I’ll run “mvn eclipse:eclipse” on the “mywebapp” project via an Eclipse external tool configuration.
Before running eclipse:eclipse, the “mywebapp” .classpath file looked like this:
.classpath file before pom.xml update
After running eclipse:eclipse, the “mywebapp” .classpath file was updated to look like this:
.classpath file after pom.xml update
The “mywebapp” project now has the TOMCAT_6.0.14_LIBRARY user library included in its classpath!
So, let’s get started. First of all, I need to add a Context entry to my Tomcat’s server.xml file for the “mywebapp” project. In Eclipse, I have a “_stuff” project that contains a link to my Tomcat’s server.xml file for convenience. So, I’ll double-click the server.xml link to open up the server.xml file in Eclipse’s XMLWeditor.
I’ll add the following Context element to my Tomcat server.xml file. The docBase attribute specifies my web context directory (ie, web, public_html, webapp, etc..) for my project. The path attribute specifies the path that is used to hit the project via a web browser (ie, http://localhost:8080/mywebapp).
The server.xml file is shown below. Notice that the element is within the element.
Now, I’ll create an EclipseSW Debug Configuration to start up my project in TomcatSW. I named the Debug Configuration “mywebapp tomcat”. I specified the project to be “mywebapp”. The Tomcat main class from the bootstrap.jar file is “org.apache.catalina.startup.Bootstrap”.
On the Arguments tab, I specified the working directory to be my Tomcat home directory, which for me is C:devapache-tomcat-6.0.14.
I clicked the Debug button to start up my project in Tomcat via Eclipse.
I went to a web browser and attempted to hit my "mywebapp" web application via http://localhost:8080/mywebapp. It worked!
http://www.avajava.com/tutorials/lessons/how-do-i-run-a-maven-web-application-in-tomcat-from-eclipse.html?page=1
Author -
LastMod 2012-06-02