Spring Boot项目Maven无法识别prod profile问题求助
问题描述
执行mvn clean package -Pprod打包Spring Boot应用时,Maven无法识别prod环境的profile,控制台提示No active profile set, falling back to 1 default profile: "default";但执行mvn spring-boot:run时一切正常。
配置文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.9</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cvillegas.app</groupId> <artifactId>MainApp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MainApp</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-jwt</artifactId> <version>1.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>5.6.1</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.6.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.5.7</version> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>dev</id> <properties> <appProfile>dev</appProfile> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>qa</id> <properties> <appProfile>qa</appProfile> </properties> </profile> <profile> <id>prod</id> <properties> <appProfile>prod</appProfile> </properties> </profile> </profiles> </project>
application.yml
spring: profiles: active: @appProfile@ datasource: driver-class-name: org.postgresql.Driver jpa: database-platform: org.hibernate.dialect.PostgreSQL10Dialect generate-ddl: true hibernate: ddl-auto: update show-sql: true properties: hibernate: format_sql: true
原因及解决办法
核心问题
mvn clean package仅执行Maven打包生命周期,未触发Spring Boot的资源过滤替换逻辑,导致application.yml中的@appProfile@占位符没有被替换为对应profile的值。Spring Boot启动时找不到指定的激活profile,只能默认使用default。
而mvn spring-boot:run会自动触发资源过滤流程,因此能正确识别profile。
解决步骤
- 添加资源过滤配置:在pom.xml的
<build>节点下新增<resources>配置,让Maven在package阶段处理application.yml中的占位符:
<build> <!-- 新增资源过滤配置 --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>application.yml</exclude> </excludes> </resource> </resources> <plugins> <!-- 原有的spring-boot-maven-plugin配置保持不变 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.5.7</version> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build>
- 验证命令:重新执行
mvn clean package -Pprod,此时Maven会在打包过程中将application.yml里的@appProfile@替换为prod,启动jar包时即可正确识别prod profile。
额外优化建议
- 统一Spring Boot插件版本:当前pom.xml中parent版本为2.7.9,但spring-boot-maven-plugin使用的是2.5.7,建议改为与parent一致的2.7.9,避免版本不一致引发兼容问题。
- 灵活切换profile:也可以在启动jar包时通过命令行直接指定profile,无需依赖Maven打包时的资源过滤:
java -jar MainApp-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
内容的提问来源于stack exchange,提问作者Cristhian Villegas García
相关产品推荐
相关产品推荐

