Tomcat Maven plugin and multi-module Maven projects
Asked Answered
L

2

7

We have an application that until recently was a single Maven WAR project. We were using the Tomcat Maven plugin to run the application on local developer workstations using:

mvn tomcat:run

We were able to change JSP files while the embedded Tomcat instance was running and the changes would appear in web browsers just fine. I understand (from the plugin documentation) that when using the tomcat:run goal, the WAR is loaded as a dynamic web application and hence changes made to JSP files at source are picked up by Tomcat at runtime without restart.

The application has reached a fairly large size and we needed to reuse a large number of classes in a few different places besides the web project, so we refactored the code base into a multi-module Maven project. The structure is now:

parent Maven POM
    |
     ---- artifact1.jar
    |
     ---- artifact2.jar -> depends on artifact1.jar
    |
     ---- artifact3.jar -> depends on artifact1.jar
    |
     ---- artifact4.jar -> depends on artifact2.jar and artifact3.jar
    |
     ---- artifact5.war -> depends on artifact1.jar, artifact2.jar, artifact3.jar and artifact4.jar

After the refactoring we were unable to use tomcat:run from the project's root directory to run the WAR project as the plugin was unable to detect the JAR artifacts. So, we switched to using the tomcat:run-war-only plugin. The WAR module now launches fine.

However, from the documentation, it seems that the run-war-only goal treats WAR files as packaged web applications. Therefore, any changes we make to JSP files now are not picked up by the embedded Tomcat server while running. For every change to JSP files we have to restart the server.

Is there a way for us in this multi-module Maven set up to run WAR projects as dynamic web applications so that at least changes to JSP files are picked up by Tomcat without restarting?

Librettist answered 7/11, 2012 at 4:25 Comment(1)
With Maven 3.x and Tomcat7 Maven plugin, the command mvn -am -pl artifact5 tomcat7:run works just fine.Librettist
F
3

Just do an mvn install first and then

mvn -pl artifact5 tomcat:run 
Ferrin answered 7/11, 2012 at 11:50 Comment(1)
I know this is a solution, just requires two steps instead of one. However, this is a valid solution, I am happy to accept it as an answer. I have to run the build as mvn -pl artifact5 -am tomcat:run to reduce the number of instances when even this does not work. :)Librettist
T
3

First use new version of the tomcat plugin now located at Apache see http://tomcat.apache.org/maven-plugin-2.0/.

Then if you use maven3, you simply use tomcat6/tomcat7:run from the top. All classes from modules will be added to your webapp classloader (will save some ios as you don't need to install all jars first !) see http://tomcat.apache.org/maven-plugin-2.0/run-mojo-features.html

HTH!

Tadeo answered 8/11, 2012 at 9:56 Comment(2)
any sample to reproduce your issue ? The basic archetype works fine and it's a similar use case.Tadeo
I think with Maven 3 you still need to instruct maven to package the other modules. I use mvn -am package tomcat7:run-war. The -am package is to explictly package all modules and tomcat:run-war only applies to the WAR anyway. Maybe the same applies for tomcat:run.Sliding

© 2022 - 2024 — McMap. All rights reserved.