Search code examples
javamavenjettymaven-jetty-pluginmaven-module

Maven jetty plugin - automatic reload using a multi-module project


I am developing a Java web application, using a multi-module maven project. The project setup is the following:

  • pom.xml Main maven project, that includes the following modules:
    • persistence: Entity classes and DAOs
    • business: Service definition and implementation
    • webapp: Apache wicket web application

The dependency hierarchy is the following: webapp depends on business, which depends on persistence.

I am also using the Jetty Maven Plugin to run the web application locally using mvn -pl webapp jetty:run inside the directory with the main pom.xml. When developing the application, When making code changes, I want the jetty server to restart and reload the modified code files automatically. This works fine when I am modifying files inside the webapp module, but does not work when I am modifying a file inside another module, such persistence or business.

The Maven Jetty Plugin is configured inside webapp/pom.xml as follows:

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.2.2.v20140723</version>
    <configuration>
        <reload>automatic</reload>
        <scanIntervalSeconds>1</scanIntervalSeconds>
        <webApp>
            <extraClasspath>../business/target/classes/;../persistence/target/classes/</extraClasspath>
        </webApp>
        <scanTargets>
            <scanTarget>../business/target/classes</scanTarget>
            <scanTarget>../persistence/target/classes</scanTarget>
        </scanTargets>
</plugin>

I followed the instructions of this answer. The <scanTarget> tags work fine, since jetty gets restarted when I modify a file inside business or persistence. However, the <extraClasspath> does not work since the modified files are not loaded by jetty. The linked answer uses the <webAppConfig> tag. However, I am using the <webApp> tag as specified in the documentation of the plugin (I also tried the old <webAppConfig> tag, which lead to the same results).

My question is: How to configure the Jetty Maven Plugin for a multi-module project, such that it reloads modified files from other modules?


Solution

  • Using trial and error, I found a solution. The problem is that jetty is executed using from the parent pom using

    mvn -pl webapp jetty:run
    

    The command is called from the directory of the main pom, thus jetty cannot resolve the relative paths inside the extraClasspath correctly. When executing the jetty:run goal inside the webapp directory, all modified classes are loaded correctly.


    I assume the scanTargets are working correctly even when using mvn -pl webapp jetty:run, because the relative paths get resolved during the execution of the plugin (with the correct working directory). Jetty outputs the scan targets on startup:

    [INFO] Added extra scan target:C:\PathToProject\business\target\classes
    [INFO] Added extra scan target:C:\PathToProject\persistence\target\classes
    

    However, the <extraClasspath>property is part of the <webApp> property, which is an instance of the org.eclipse.jetty.webapp.WebAppContext class. I assume that this instance is passed to jetty directly and that the extraClasspath property is accessed by jetty when it is already started.