如何为spring-boot-maven-plugin配置不同的执行配置文件?
为Spring Boot Maven插件配置多执行场景
方法1:配置多个execution块
直接在spring-boot-maven-plugin里定义多个<execution>节点,每个节点对应不同的主类、JVM参数或系统属性,给每个execution设置唯一ID,运行时通过ID指定执行哪个配置。
示例pom配置:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <!-- 执行配置1:运行主类AppMainA --> <execution> <id>run-app-a</id> <goals> <goal>run</goal> </goals> <configuration> <mainClass>com.example.AppMainA</mainClass> <jvmArguments>-Xmx512m</jvmArguments> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>dev</value> </systemProperty> </systemProperties> </configuration> </execution> <!-- 执行配置2:运行主类AppMainB --> <execution> <id>run-app-b</id> <goals> <goal>run</goal> </goals> <configuration> <mainClass>com.example.AppMainB</mainClass> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>test</value> </systemProperty> </systemProperties> </configuration> </execution> </executions> </plugin> </plugins> </build>
运行命令:
- 执行配置1:
mvn spring-boot:run@run-app-a - 执行配置2:
mvn spring-boot:run@run-app-b
方法2:结合Maven Profiles
如果需要按环境或场景做更彻底的配置隔离,用Maven Profiles,每个Profile对应一套spring-boot-maven-plugin的配置。
示例pom配置:
<profiles> <profile> <id>profile-a</id> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.AppMainA</mainClass> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>dev</value> </systemProperty> </systemProperties> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>profile-b</id> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.example.AppMainB</mainClass> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>test</value> </systemProperty> </systemProperties> </configuration> </plugin> </plugins> </build> </profile> </profiles>
运行命令:
- 启用profile-a运行:
mvn spring-boot:run -Pprofile-a - 启用profile-b运行:
mvn spring-boot:run -Pprofile-b
方法3:命令行临时指定参数
如果只是临时切换主类或配置,不需要修改pom,直接通过命令行参数指定:
- 指定主类:
mvn spring-boot:run -Dspring-boot.run.main-class=com.example.AppMainC - 指定Spring配置文件:
mvn spring-boot:run -Dspring.profiles.active=prod - 同时指定主类和配置文件:
mvn spring-boot:run -Dspring-boot.run.main-class=com.example.AppMainC -Dspring.profiles.active=prod
内容的提问来源于stack exchange,提问作者morpheus
相关产品推荐
相关产品推荐

