Import a maven web project in eclipse for tomcat

A lot of virtual ink has been spent on using eclipse to deploy web applicaitons in tomcat and, more recently, maven-managed projects. I went through a few days of anguish following an update to Windows 7. I have managed however to get through and make my project run nicely through eclipse.

Note: Although some steps may be relevant for new projects, this article is focused on existing projects managed with Maven.

Prerequisites

Following items are required for this process to be successful:

  • Eclipse – J2EE variant: You can download this from eclipse.org. Note: for some reason, my J2EE install was missing the default update site.
  • A JDK: Download it from Oracle (you’ll need to accept the terms and conditions…). JRE is not enough.
  • Some version control if your code is on version control. Eclipse supports quite a lot.
  • M2E: Maven Eclipse integration
  • Eclipse WTP: The web tools platform. This is normally included in J2EE variant of Eclipse
  • Tomcat: You can download it from apache or use the WTP to lownload it locally. This is required to create a server.
  • Maven, of course

It is useful to add the [MAVEN DIR]/bin to the system path ($PATH or %PATH%) and to define JAVA_PATH to point to your JDK installation directory. It will ease your pain when working with command line.

Create the J2EE Dynamic web project

After you get your maven project downloaded, make sure is clean and doesn’t contain any eclipse-related directories (.settings, .classpath, .project). If it does, remove them.

Use Maven

Maven has the “ability” to generate a eclipse project skeleton. To do that, you need to run the following command in your project’s directory (where your pom.xml is):

Once this is done, you need to import the project into eclipse using the “Import” dialog:

Most of the items should be set up correctly by default. However, I had to go to the “Deployment Assembly” tab in the project propertues and fix the exported sources (main/java, main/resources, WEB-INF …). Don’t worry too much… if the deployment assembly is not configured properly, you’ll get a warning and all you have to do is to re-define your source directories…

Now your project is imported and can be attached to a server.

A server for your project

Creating a server in Eclipse is trivial:

  1. Open “File -> New -> Other” (or just CTRL+N)
  2. Select the “Server” entry:
  3. Select the Tomcat 6 server (or your favourite):


    If you don’t have any server configured, follow the links in the window to download and configure one

Configure yout tomcat server

Now that the server is created, you need to configure it. This is relatively simple:

  • In the “Servers” window in eclipse, double-click on your server. A configuration file will appear:
  • Select to “Publish module context to separate XML files”, because Tomcat 6 prefers separate configuration than inline <Context …> tags.

Your new web project can be added by clicking the “Modules” tab of the configuration file:

…or by right-clicking the desired server in the “Servers” window. Various tutorials prefer the former variant, because it allos the user to disable automatic reload:

I can’t say if it’s better or not to have it enabled; my setup works OK with it enabled…

Launching and troubleshooing

The configuration is finished. Clean the deployment directories and launch the server. Everything should be published correctly nad your site should “just work“. If it doesn’t then you can use the following steps:

  • I get a “Error creating bean with name ‘entityManagerFactory’ defined in ServletContext resource” exception: then your persistence.xml is not correct or not in the right place
  • I get a “ClassNotFound” exception: You forgot to publish something!
  • I get 404 pages for valid JSPs: Your spring dispatcher is not configured properly. You probably have some residual configuration files. Start from the beginning of the tutorial and don’t forget to remove your existing eclipse project files!
  • I get no errors, but my application doesn’t load: Check the log for warning. If you get a Setting property ‘source’ [...] did not find a matching property message, then you need to check the deployment in a separate configuration XML above.

Some useful locations:

  • CATALINA_HOME (with different casing) is the location of your tomcat installation
  • tmpX is the location of your deployment. It’s in $HOME/workspace/.metadata/.plugins/org.eclipse.wst.server.core/

Bibliograpgy

  • Code Ranch: Context Initialization Failed

    The root cause seems to be “Error creating bean with name ‘entityManagerFactory’ defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Invalid XML in persistence unit from URL [jar:file:/C:/jboss/server/default/tmp/deploy/tmp21264titan.jar!/META-INF/persistence.xml"

    So I would first check persistence.xml.

    ...and...

    I've done some research. It looks like that problem was in persistence.xml. From what I read that project was using a container managed factory so the applicationContext.xml has all the pertinent information but persistence.xml had an element within the outer tag. When I removed that, I got past that error at least. The jar files in my class path are having conflicts now though.

  • Code Ranch: Struts 2 tutorial errors

    I found that it is really important to have exact version of jars specified. The easiest way to do this is run the maven script that comes in the download for the tutorial. Run the "mvn install" command first. Be sure you have the jta jar. Delete the test dir from the source code unless you have junit (they do not have it in the pom.xml).

    Then run "mvn package" and maven will get all the dependent jars and then puts the app source and war file in a directory called "target". The jars are in target/quickstartSNAPSHOT/WEB-INF/lib.

  • My Eclipse IDE: Path must include project and resource name: / error

    Switch back to your old workspace and use the -clean command to clean it ans check if it works:-

    http://www.myeclipseide.com/PNphpBB2-viewtopic-t-10280.html

    Incase it doesn't, you should transfer your code to the new project in the new workspace and continue working.

  • Tomcat / Spring integration returning 404

    I followed the instructions provided below and inside the project folder, from the command line I ran mvn eclipse:eclipse -Dwtpversion=2.0 so that eclipse can import the project correctly. I'm working on figuring out how to do this all through eclipse to avoid having to manually do this every time.

  • Spring: MVC framework:
  • Spring Source: Getting Started With JPA in Spring 2.0
  • Unable to “Run on Server” a webapp from Eclipse

    I have stopped using this method of running my webapp. I use mvn tomcat:run instead. Much easier. Though I don’t know how to debug.

  • Code Ranch: HTTP Status 404 error in tomcat

    posted 01 August 2011 09:48:58 private message
    Quote
    Hallo,

    I had resolved my problem. When using Eclise Helios and Tomcat 6 or 7 You have to do the next:

    “Copy the ROOT (Default) Web App into Eclipse.

    Eclipse forgets to copy the default apps (ROOT, examples, etc.) when it creates a Tomcat folder inside the Eclipse workspace. Go to C:\apache-tomcat-7.0.8\webapps, R-click on the ROOT folder and copy it. Then go to your Eclipse workspace, go to the .metadata folder, and search for “wtpwebapps”. You should find something like your-eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps (or …/tmp1/wtpwebapps if you already had another server registered in Eclipse). Go to the wtpwebapps folder, R-click, and paste ROOT (say “yes” if asked if you want to merge/replace folders/files). Then reload http://localhost/ to see the Tomcat welcome page.”

    Source : http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html

    When updating the the used and to be used jar-files my application worked ok. But now when using Eclipse Indigo and Tomcat 6 or 7 on my new computer I get – when typing localhost:8080 after starting Tomcat in the Eclipse IDE- the Tomcat homepage. But when running my application http://localhost:8080/cew/login.faces I got the Tomcat error page http 404 the resource could not be found. My opinion is that it had to do something with the Tomcat configuration, more specifically the place where the war-file and project map are to be found, although mine are found in de webapps file – the cew war-file and the project map cew with all the files-. There are no project errors –it is the same webapplication that runned on my old computer with Eclipse Helios and Tomcat 7. But now it won’t work. Even more when I installed Eclipse Helios and Tomcat 7 and I got also again the HTTP 404 error of Tomcat. I don’t undertand it any more.

  • Frustrating Tomcat 5.5.x Issue Solved (conflicting jars…)

    Don’t know why I had been mostly ignoring the significance of that exception, but it became clear to me that I probably had conflicting jsp/jstl classes on my classpath. Tomcat probably provided one set and my app another. Sure enough, I had included a “jsp-api.jar” in my application’s lib directory, and Tomcat had its own version in $CATALINA_HOME/common/lib. I removed the jar from my application and everything loaded just fine. I could have also just copied the version from Tomcat to my app, but since I didn’t need it there was no sense in keeping it around.

  • How to Set Up Hot Code Replacement with Tomcat and Eclipse

    Why Disable Auto Reloading?

    Auto reloading is a feature of Tomcat that allows you to replace Java classes at runtime without using JPDA. In this mode, Tomcat uses Java classloaders to try to unload classes and reload them; whenever it reloads, it also tries to reinitialize your application, re-launching any servlets that are marked load-on-startup in your web.xml file.

    Disable Auto Reloading but Enable Auto Publishing

    In the screenshot above you can see how to disable auto reloading on the “Modules” tab of the Tomcat server; auto reloading is bad for JPDA debugging. But there’s another setting called “Automatically publish when resources change” on the “Overview” tab of the Tomcat server. It’s hidden by default, collapsed under the “Publishing” section. You can see it if you expand that section; you want to make sure auto publishing is enabled while auto reloading is disabled.

    Finding the tmp0 Fake Tomcat Directory

    Sometimes it can be helpful to look inside the fake Tomcat directory and see what’s going on in there. Eclipse tells you where it put the Tomcat directory in the “Server Locations” section of your “Tomcat” server configuration panel. (Double-click on your Server in the “Servers” tab to open the configuration panel.) Typically, Eclipse says that your server is in .metadata/.plugins/org.eclipse.wst.server.core/tmp0; for this reason I typically call it the tmp0 directory (pronounced “tempo”).

    The .metadata folder is inside your Eclipse workspace directory. (You can find your Eclipse workspace directory by going to File -> Switch Workspace; the default value is your current workspace directory.) In the worst case, you can always just search your hard drive for tmp0. It’s there somewhere!

    Inside, you can see all the folders Eclipse has created. Check out the generated server.xml file in tmp0/conf. Examine generated .java files in tmp0/work. Your tmp0/webapps directory is probably empty; Eclipse has probably generated your webapp in wtpwebapps.

    Exorcising the tmp0 Directory

    Unfortunately, sometimes Eclipse gets a little confused about what to put in your WAR file, and you need to perform various stages of exorcism depending on how badly your tmp0 directory is messed up.

    • Try republishing your tmp0 directory. Open the “Servers” tab, right-click on your server and select “Clean…” (not “Clean Tomcat Work Directory…”). Then select “Publish.” That should completely rebuild your tmp0 directory.
    • Try restarting Eclipse. This works more often than I’d like to admit.
    • Try completely deleting and recreating your server. Follow this ritual:
      • Open the “Servers” tab, right-click on the server and select “Delete”.
      • Make sure “Delete unused server configuration(s)” is checked, then click OK.
      • Look at your “Servers” pseudo-project; make sure the folder for your server is gone. If it isn’t, right-click on it and Delete it.
      • Quit Eclipse.
      • Go find your tmp0 directory (if it’s still present) and delete it from your file system.
      • Launch Eclipse and recreate your server from scratch.
    • Try creating a new workspace. File -> Switch Workspaces: specify an empty directory. Create your server from scratch.
  • Integrating tomcat and eclipse as a hot-deploy environment
  • warning: Setting property ‘source’ [...] did not find a matching property.

    As I mentioned, the warning disappeared when I got rid of the source attribute in my <Context> element. My real problem, where Tomcat didn’t seem to be publishing my web project, appears to have been caused at least partly by Eclipse adding the src/main/webapp folder to the Java build path. I could have sworn that I checked for this earlier, but either I didn’t actually check or else there was another factor which I never properly identified but somehow fixed at some point.

  • How do I run a maven web application in Tomcat from Eclipse?
  • Deploying project, created with Eclipse and Maven, to Tomcat and maven tomcat plugin

HTH