我正在使用多模块 Maven 项目开发 Java Web 应用程序。项目设置如下:
-
pom.xml
主maven项目,包括以下模块:-
persistence:
实体类和 DAO -
business:
服务定义和实现 -
webapp:
Apache wicket 网络应用程序
-
依赖层次结构如下:webapp
取决于 business
, 这取决于 persistence
.
我也在使用 Jetty Maven Plugin使用 mvn -pl webapp jetty:run
在本地运行 Web 应用程序在主目录内 pom.xml
.在开发应用程序时,在进行代码更改时,我希望 jetty 服务器重新启动并自动重新加载修改后的代码文件。当我修改 webapp
中的文件时,这工作正常模块,但是当我在另一个模块中修改文件时不起作用,例如persistence
或 business
.
Maven Jetty Plugin配置在webapp/pom.xml
里面如下:
<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>
我按照 this answer 的说明进行操作. <scanTarget>
标签工作正常,因为当我修改 business
中的文件时 jetty 会重新启动或 persistence
.然而,<extraClasspath>
不起作用,因为 jetty 未加载修改后的文件。链接的答案使用 <webAppConfig>
标签。但是,我正在使用 <webApp>
documentation of the plugin 中指定的标签(我还尝试了旧的 <webAppConfig>
标签,结果相同)。
我的问题是:如何为多模块项目配置 Jetty Maven 插件,以便它从其他模块重新加载修改后的文件?
最佳答案
要在子模块更改时强制重新加载,您可以使用以下选项
1 - 静态模块名称和扫描目标
您可以将每个模块的目标目录定义为扫描目标
<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 - 动态模块名称和扫描目标(推荐) 这使用 RegEx 来查找其他模块的编译目标,在下面的示例中,每次在任何模块上编译类时我们都会重新加载应用程序
<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>
关于java - Maven jetty 插件 - 使用多模块项目自动重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725552/