Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat Maven plugin and multi-module Maven projects

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?

like image 670
manish Avatar asked Nov 07 '12 04:11

manish


People also ask

What is the difference between Maven module and Maven project?

There is very little difference between Maven Module and Maven Project. When we create a Maven module it is mandatory to specify a parent project. As soon as we specify the parent project, <modules> … </module> is added in pom.

What is Maven Multi Module project?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

Can a Maven project have multiple pom files?

Yes you can use Maven Profiles to manage this. Obviously you can tweak this approach to suit your needs however works best.

What is the use of multi module project?

Advantages of a Multi-Module ProjectIt provides a great ability to build all sub-modules only with a single command. We can run the build command from the parent module. While building the application, the build system takes care of the build order.


2 Answers

Just do an mvn install first and then

mvn -pl artifact5 tomcat:run 
like image 168
khmarbaise Avatar answered Oct 10 '22 23:10

khmarbaise


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!

like image 25
Olivier Lamy Avatar answered Oct 11 '22 00:10

Olivier Lamy