javaspringmavenspring-bootspring-boot-devtools

Spring Boot Devtools how to reload Maven multiple dependent modules in the independent module?


I have a multiple modules Maven with this structure:

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

The simple pom.xml files for these folders:

<modules>
   <module>app-library</module>
   <module>app-main</module>
</modules>
<packaging>pom</packaging>
<name>app-parent</name>
<parent>
    <groupId>test</groupId>
    <artifactId>app-parent</artifactId>
    <version>1.0.0</version>
</parent>
<dependencies>
   .... Some libraries here ....
</dependencies>
<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>

With NetBeans 8.2 and Spring Boot version 1.5.2, I used Spring Devtools to auto reload changed Java classes file (~few seconds) instead of cold restart (> 10 seconds).

On app-main folder, I run this command to set up the WebApplication which allows NetBeans to attach a debugger to port 5005:

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

Also, in application.properties of app-main/src/main/resources, I added this watch to allow Spring Devtools detect the change from app-library

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

So, whenever I change one java file in app-main or app-library, I can see from the terminal that Spring DevTool does the reload in few seconds.

     .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)

The problem is with some basic change in a method of app-library, Spring DevTools can detect the file has been saved and reload, but from the app-main where called this method, the output is still the same, e.g:

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());
    }
}

With this problem, I cannot just change in app-library and expect the changes will be applied in app-main. Instead, I have to stop the Maven command line, then in NetBeans click on app-main and select Build with dependencies, then runs Maven command line again to set up this Web Application with cold start (total: ~1 minute).

What can I do to make Spring DevTools can apply the changes from app-library to app-main instantly after I saved files in app-library? That would help to reduce the waiting time from NetBeans "Build with Dependencies".


Solution

  • So, in app-main it needs app-library built in local repository (~/.m2/...) and the only way to update this repository is going to app-library folder and run:

    mvn install
    

    After that, stop the running Web application from command line (ctrl + c) and restart it with same maven command on app-main folder:

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

    It still takes time to cold restart app-main, but it is acceptable.