0

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

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

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

  • 应用父/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>
  • 应用程序主/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 中,我添加了这个 watch 以允许 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 分钟)。

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

4

1 回答 1

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仍然需要时间,但可以接受。

于 2018-05-01T10:10:55.327 回答