Maven打包Spring Boot主动客户端时进程无法终止致Jar生成失败求助
Hey there, let's tackle this annoying issue you're facing with your Spring Boot client and Maven install command. The root problem here is that your application spins up a persistent connection to the server during the build process, which keeps the JVM running indefinitely—stopping Maven from finishing the Jar packaging and install steps. Here are some practical, actionable fixes you can try:
The spring-boot-maven-plugin might be triggering your app to start during the build lifecycle without you realizing it. You can explicitly tell it to skip starting the app during install, and only let it run when you explicitly trigger it.
Add this configuration to your pom.xml:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- Skip app startup during Maven install/package phases --> <skip>true</skip> </configuration> <!-- Optional: Bind the run goal only to manual execution --> <executions> <execution> <id>spring-boot-run</id> <goals> <goal>run</goal> </goals> <phase>none</phase> <!-- Don't tie to any build phase --> </execution> </executions> </plugin> </plugins> </build>
Now mvn install will just package your Jar without starting the app. To run the client later, use mvn spring-boot:run.
Wrap your client connection initialization code in a Spring profile, so it only activates when you explicitly run the app—not during builds.
First, add the profile to your connection configuration:
@Profile("client-active") @Configuration public class ServerConnectionConfig { @Bean public ServerConnection serverConnection() { ServerConnection connection = new ServerConnection(); connection.connect(); // Your persistent connection logic return connection; } }
By default, Maven builds won't activate this profile, so the connection bean won't be created. When you want to run the client, activate the profile manually:
- Via command line:
mvn spring-boot:run -Dspring.profiles.active=client-active - Or in your IDE's run configuration, add the profile flag.
If your issue is happening because integration tests are starting the app and leaving it running, make sure your test context shuts down properly.
Add the @DirtiesContext annotation to your test class to force context cleanup after tests:
@SpringBootTest @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) public class ClientIntegrationTests { // Your test cases here }
Or manually trigger a shutdown in your test teardown:
@Autowired private ApplicationContext appContext; @AfterAll public static void cleanUp() { SpringApplication.exit(appContext, () -> 0); }
If you're using CommandLineRunner or ApplicationRunner to initialize the connection, add a check to skip that logic during Maven builds.
Modify your runner component:
@Component public class ClientStartupRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // Check if we're in a Maven build environment String isMavenBuild = System.getProperty("maven.build"); if (!"true".equals(isMavenBuild)) { // Only start the connection when not building startServerConnection(); } } }
Then add the system property to your Maven test plugin to flag builds:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <systemPropertyVariables> <maven.build>true</maven.build> </systemPropertyVariables> </configuration> </plugin>
Pick the fix that best fits your setup—most folks find Fix 1 or Fix 2 to be the quickest wins. Let me know if you need more details on any of these!
内容的提问来源于stack exchange,提问作者Centaure

