Spring Boot Devtools如何在独立模块中重新加载Maven多个相关模块?

时间:2018-05-01 07:32:10

标签: java spring maven spring-boot spring-boot-devtools

我有一个具有这种结构的多个模块Maven:

app-parent
  -------app-library (Hibernate data entities layer)
  -------app-main (contains app-library as dependency), Spring Boot web application.

这些文件夹的简单pom.xml文件:

  • APP-父/ pom.xml的
<modules>
   <module>app-library</module>
   <module>app-main</module>
</modules>
<packaging>pom</packaging>
<name>app-parent</name>
  • 应用程序库/ pom.xml的
<parent>
    <groupId>test</groupId>
    <artifactId>app-parent</artifactId>
    <version>1.0.0</version>
</parent>
<dependencies>
   .... Some libraries here ....
</dependencies>
  • APP-主/ pom.xml的
<parent>
    <groupId>test</groupId>
    <artifactId>app-parent</artifactId>
    <version>1.0.0</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>            
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>petascope-core</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

使用NetBeans 8.2和Spring Boot 1.5.2版,我使用Spring Devtools自动重新加载已更改的Java类文件(〜几秒钟)而不是冷启动(> 10秒)。

在app-main文件夹上,我运行此命令来设置WebApplication,它允许NetBeans将调试器附加到端口5005:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

另外,在app-main / src / main / resources的application.properties中,我添加了这个手表,允许Spring Devtools检测app-library的变化

spring.devtools.restart.additional-paths=../app-library

因此,每当我在app-main或app-library中更改一个java文件时,我都可以从终端看到Spring DevTool会在几秒钟内重新加载。

     .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)

  INFO [08:57:20] ApplicationMain@48: Starting ApplicationMain on localhost.localdomain with PID 19645 (/home/rasdaman/rasdaman_community/build/applications/petascope/target/petascope_main/classes started by rasdaman in /home/rasdaman/rasdaman_community/rasdaman/applications/petascope/petascope_main)
 DEBUG [08:57:20] ApplicationMain@51: Running with Spring Boot v1.5.2.RELEASE, Spring v4.3.7.RELEASE
  INFO [08:57:20] ApplicationMain@637: No active profile set, falling back to default profiles: default
[2018-05-01 08:57:22.341] - 19645 INFO [restartedMain] --- org.apache.catalina.core.StandardService: Starting service Tomcat
[2018-05-01 08:57:22.341] - 19645 INFO [restartedMain] --- org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.11
[2018-05-01 08:57:22.361] - 19645 INFO [localhost-startStop-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/rasdaman]: Initializing Spring embedded WebApplicationContext
  INFO [08:57:24] ApplicationMain@57: Started ApplicationMain in 3.612 seconds (JVM running for 84.418)

问题在于app-library方法的一些基本变化,Spring DevTools可以检测到文件已被保存并重新加载,但是从app-main这里调用此方法,输出仍然是相同的,例如:

app-library
public class Service {
     public String returnValue() {
          // return "Value before Spring DevTools reload.";

          // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
          // After app-main was set up from Maven command lines, I changed this line here and saved it to notice Spring DevTool to reload in the console.
          return "Value after Spring DevTools reload."
     }
}

app-main
public class TestService {

    public TestService() {
         Service service = new Service();
         // !!!!!!!!!!!!!!!!!!!
         // It can only print "Value before Spring DevTools reload."
         // even though the Service file has been changed to return different value and Spring DevTools reloaded.
         System.out.println(service.returnValue());
    }
}

有了这个问题,我不能只改变app-library,并期望更改将应用​​于app-main。相反,我必须停止Maven命令行,然后在NetBeans中单击app-main并选择 Build with dependencies ,然后再次运行Maven命令行以冷启动设置此Web应用程序(总计: ~1分钟。

我可以做些什么来使Spring DevTools在app-library中保存文件后立即将更改从app-library应用到app-main?这将有助于减少NetBeans“Build with Dependencies”的等待时间。

1 个答案:

答案 0 :(得分:0)

因此,在app-main中需要内置本地存储库(〜/ .m2 / ...)的app-library,更新此存储库的唯一方法是访问app-library文件夹并运行:

mvn install

之后,从命令行( ctrl + c )停止正在运行的Web应用程序,并在app-main文件夹上使用相同的maven命令重新启动它:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

冷启动app-main仍需要时间,但这是可以接受的。