I work from home for a company where we write enterprise Java web applications using Spring, Hibernate for Tomcat and use Maven. A couple of months ago, when I started working from home, my manager agreed to buy me a copy of IntelliJ IDEA so I could be more productive than when I was when I was using Eclipse (which is an IDE I love and still use for other projects, but is also a memory hog).
While I have been using IntelliJ for sometime now, I haven’t had a lot of success in configuring Tomcat to run through the IDE. I followed the steps on the official help page, but it didn’t seem to work with my Maven project. So I added a configuration descriptor to Tomcat to load the target directory of my Maven project each time it loads and when I want to run my code, I produce an exploded war through IntelliJ and then run Tomcat externally.
When I had to debug my code, I would print out debugging statements which would help me narrow down the places in my code where the bug was, and then I would just inspect it manually. Not really the best way to debug code, but I really didn’t have the time to integrate Tomcat into IntelliJ.
Last night, I had a NullPointerException in a Java Spring project I’ve been working on for a couple of months now, and this approach didn’t work, and I set out to integrate Tomcat with IntelliJ once and for all.
When I had first tried to integrate Tomcat with IntelliJ, I had followed the steps on the Jetbrains Wiki and that worked just how it was described, but when I tried to run or debug my Maven project through IntelliJ, it just would not work.
After a lot of Googling and Binging *wink*, I had found no single source that explained how this should be done, and I spent a major part of last night just playing with different combinations of the different settings and was finally able to set it up correctly (I hope). Now, I am able to run and debug Maven projects directly through Tomcat and trust me, life is better.
I’m not sure if there’s other people who’ve been through the same experience as I have, so I’ve decided to put together this guide. Hope someone who is in the same position as I was yesterday will get something out of it.
1. Add your Tomcat configuration to IntelliJ through Settings -> Application Servers -> Add. Both Tomcat Home and Tomcat Base Directory should be set to your tomcat installation directory (e.g. C:\Tomcat6.0.33)
2. Add a Run/Debug Configuration to your IntelliJ project.
3. Click the + and add a new Tomcat local default configuration. Give it a name and you can have it start your browser automatically and go to a startup page.
4. On the Before Launch frame, check Make.
5. Click the ellipses (….) button next to Build Artifact and check exploded war.
6. Similarly for Maven, click the ellipses (…) button next to Run Maven Goal and select package.
7. On the Deployment tab, in the Deploy at Server Startup section, add a new Exploded war Artifact.
(Steps below show how to configure the IntelliJ debugger to connect to Tomcat)
8. On the Startup/Connection tab, Select Debug from the left pane and set the port to 8000.
And you’re set. Now when you click Run or Build, you should see maven packaging the project and work it’s ‘magic’, and then Tomcat should run with the new app!
While now it seems simple, this was not at all intuitive when I was first tried to set all this up. But now that I have got this set up, my workflow is a lot improved and I can go back to focusing on the code!
Alternatively, if you wanted to run your project outside of IntelliJ, you can put a configuration descriptor into your tomcat/conf/Catalina/localhost directory to automatically look in a directory and deploy it every time it starts. For my QEF project, I have a file qef.xml that looks like this:
<?xml version="1.0" encoding="UTF-8"?> <Context docBase="/programming/appQef/target/qef"> <Manager className="org.apache.catalina.session.PersistentManager" maxInactiveInterval="600" saveOnRestart="false"/> <ResourceLink name="jdbc/qef" global="jdbc/qef" type="javax.sql.DataSource" /> <ResourceLink global="config/machine" name="config/machine" type="java.lang.String"/> <ResourceLink global="config/envType" name="config/envType" type="java.lang.String"/> <ResourceLink global="config/envLocation" name="config/envLocation" type="java.lang.String"/> </Context>
Now whenever I want to execute my program, I build an exploded war through IntelliJ and start Tomcat, which looks for the exploded war in the given directory and automatically deploys it.
Hope this will help somebody out as well.