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

Gaste picture Gaste · Sep 8, 2014 · Viewed 8.2k times · Source

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?

Answer

Valdo Raya picture Valdo Raya · Sep 25, 2014

To force the reload anytime a submodule is changed you can use the following options

1 - Static module names and scan targets

You can define as scan targets the target directory for each module

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.plugin.version}</version>
    <configuration>
        <scanIntervalSeconds>${jetty.scanInterval}</scanIntervalSeconds>
        <scanTargets>
            <scanTarget>module-name/target/classes</scanTarget>
            <scanTarget>module-name2/target/classes</scanTarget>
        </scanTargets>
    </configuration>
</plugin>

2 - Dinamic module names and scan targets (recommended) This uses RegEx to find the compilation target for other modules, on the following example, we are reloading the application everytime a class is compiled on any module

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.plugin.version}</version>
    <configuration>
        <scanIntervalSeconds>${jetty.scanInterval}</scanIntervalSeconds>
        <scanTargetPatterns>
            <scanTargetPattern>
                <directory>${project.basedir}</directory>
                <includes>
                    <include>**/target/classes/**/*.class</include>
                </includes>
            </scanTargetPattern>
        </scanTargetPatterns>
    </configuration>
</plugin>