How to use Maven project with integrated Tomcat

How to use Maven project with integrated Tomcat inside Eclipse without STS (SpringSource Tool Suite).

In STS aka SpringSource Tool Suite you can create a Maven web application and then simply drag and drop it to integrated server and everything works as expected. 

If you don't want to use STS, then you must first install plugin "Maven Integration for Eclipse" from Marketplace.

Next create a web project, I will use "webapp-jee5" archetype.

Now if you want to drag and drop this project into a server, Eclipse won't allow deployment. Why? Because this project is a Java project, not a web application. Right click on this project -> Properties -> choose Project Facets, Convert to faceted form and select a facet "Dynamic Web Module". This requires some configuration, you must tell Eclipse where are public web files. By default Eclipse uses WebContent directory, but we have them somewhere else -> in src/main/webapp directory.

Now Eclipse will allow us deployment of this project. But there's one major problem. If your project depends on some dependencies, integrated Tomcat won't take them into account. I will show you.

For example let's add dependency to commons-lang. 

Inside index.jsp I will add:

user name: <%=SystemUtils.USER_NAME%>

Now you see that this doesn't work.

To correct this problem, you must right click your project, choose Preferences -> Deployment Assembly and add Java Build Path Entries -> Maven Dependencies.

Now everything should work as expected.

comments powered by Disqus


Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects: