Spring boot maven 插件停止目标无法停止应用程序并使应用程序进程挂起(并且我无法使用同一端口启动另一个进程)。 这是我的插件配置:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.6.RELEASE</version>
<configuration>
<jvmArguments>-DCONFIG_ENVIRONMENT=functionaltest</jvmArguments>
<mainClass>...</mainClass>
<fork>true</fork>
</configuration>
<executions>
<execution>
<id>start-service</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-service</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
我找不到任何导致此行为的错误的参考。我是否以不应该使用的方式使用该插件?
将配置更改为此,如果您想使用远程调试,请取消注释 对于在 Maven 中启动应用程序运行: spring-boot:stop clean spring-boot:start
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
<fork>true</fork>
<addResources>true</addResources>
<!-- <jvmArguments> -->
<!-- -agentlib:jdwp=transport=dt_socket,address=localhost:5005,server=y,suspend=n -->
<!-- </jvmArguments> -->
</configuration>
</plugin>
我发现了问题:spring boot应用程序(在其主线程中)启动了一个新线程,阻止jvm关闭。将“子”线程更改为守护线程解决了该问题。
有问题的代码
private ExecutorService executorService = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r);
return t;
});
解决问题的代码
private ExecutorService executorService = Executors.newSingleThreadExecutor(r -> {
Thread t = new Thread(r);
t.setDaemon(true);
return t;
});
查看这个问题,了解有关 Java 中守护进程线程的更多详细信息。
@glytching 你太棒了。你的指针对我有用。在奋斗了两天多并且几乎放弃后,对我有用的方法是将 false 放入起始目标的配置中,如下所示。非常感谢您的指点。主要的 spring-boot 插件可能将 fork 视为 true,但对于我们想做的实验,它可能只是 false。
``<execution>
<id>pre-integration-test</id>
<configuration>
<fork>false</fork>
<profiles>localhost</profiles>
<jvmArguments>-Dspring.application.admin.enabled=true</jvmArguments>
</configuration>
<goals>
<goal>start</goal>
</goals>
</execution>``