Skip to main content

sping-boot maven 多环境管理

项目结构

src/
├── main/
│ ├── resources/
│ │ ├── application.yml # 主配置(可选)
│ │ ├── application-dev.yml # 开发环境
│ │ ├── application-test.yml # 测试环境
│ │ └── application-prod.yml # 生产环境

pom.xml 配置 Profiles

<!--<project>/-->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- 默认激活 -->
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>

<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>

<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>

在 application.yml 中引用变量

# src/main/resources/application.yml
spring:
profiles:
active: @spring.profiles.active@ # ← 注意:这是 Maven 资源过滤语法

启用资源过滤

<!--<project>/<build>/-->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 启用变量替换 -->
</resource>
</resources>

打包插件配置

<!--<project>/<build>/<plugins>/-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>qiniu.QiniuApplication</mainClass>
<!--<skip>true</skip>--> <!-- 跳过打包 -->
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

打包方式

  • idea maven插件 有个配置文件选择
mvn clean package -P test