Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: How to use jetty:run in a multi-module Maven project, without needing to install

Tags:

I'm new to Maven. I have a multi-module maven 2 project that has the following structure (somewhat simplified):

Project (POM packaging)   |   +-- Module1 (JAR)   |     |   |     +-- src   |          |   |          +-- main   |               |   |               +-- java   |               +-- resources   |   +-- Module2 (JAR)   |      |   |     ...   |   +-- Web Module (WAR)          |         ... 

I've configured the Web Module to include the Maven Jetty plugin. This works great for building the production artifacts. For development, I discovered that I need to call mvn install on any module that I change, followed by stoping jetty and calling jetty:run again.
It would be much more productive if there was a way for the plugin to pick changes directly from each module's target directories. According to the jetty plugin documentation there seems to be such a feature, but it appears this only applies to the WAR module.
Even more important to me is to be able to make changes to resource files, without the need to restart jetty. That's because most of the resources are HTML template files, and it's enormously more productive to design and update the templates during development without needing to restart to see the changes.

So, is there a way to set the classpath of the jetty plugin to include the target/classes and resources directories of each JAR module, instead of the actual JARs in the local repository?

Thanks!
Yaniv

like image 917
yby Avatar asked Dec 06 '10 21:12

yby


People also ask

How do I run Mvn jetty?

You need to add [jetty maven plugin]( eclipse.org/jetty/documentation/9.4.x/jetty-maven-plugin.html) in your pom. xml file so you use jetty:run to launch your webapp in maven.

What does Mvn jetty run do?

Jetty continues to run until you stop it. While it runs, it periodically scans for changes to your project files, so if you save changes and recompile your class files, Jetty redeploys your webapp, and you can instantly test the changes you just made.

How do I skip a module in Maven build?

Maven version 3.2. 1 added this feature, you can use the -pl switch (shortcut for --projects list) with ! or - (source) to exclude certain submodules. Be careful in bash the character ! is a special character, so you either have to single quote it (like I did) or escape it with the backslash character.


2 Answers

This isn't possible with a multi-module Maven project. A cardinal rule of Maven projects is that each project should be able to stand alone. That doesn't preclude them from being built together, but any one project should be able to be built by itself so long as all it's dependencies are satisfied.

In this case, it means that the WAR project cannot look out to the other projects to see if they need to be updated, the POM for those other projects are the definitive declaration of what needs to be done to build the artifact. And once the artifact is built, it is put in the local repository. There is no association between the source files and the artifact at that point, so there's no way to tell what source files would trigger a rebuild of the artifact that the WAR depends on.

like image 97
Brian Topping Avatar answered Sep 23 '22 21:09

Brian Topping


In Eclipse, you can use "Run Jetty" plugin to achieve this.
In IDEA's Run Configuration, there is an option 'Resolve Workspace artifacts', check it.

like image 30
Kaibin Avatar answered Sep 26 '22 21:09

Kaibin