Spring Boot中使用profiles 打包
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─example
│ │ │ ├─config
│ │ │ ├─controller
│ │ │ ├─dao
│ │ │ ├─model
│ │ │ ├─rabbitmq
│ │ │ ├─service
│ │ │ │ └─impl
│ │ │ └─util
│ │ └─resources
│ │ └─mapper
配置文件到放到resources目录中
不同的环境配置变量要用@@包裹
- application.properties
- spring.datasource.url=@spring.datasource.username@
- server.port=${port:16500}
- push.ios_apns=@push.ios_apns@
不同环境变零根据类型直接写入
- application-dev.properties
- spring.datasource.username=admin
- spring.redis.port=${port:6379}
- push.ios_apns=false
- application-prod.properties
- spring.datasource.username=admin
- spring.redis.port=${port:6379}
- push.ios_apns=true
pom文件更改
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<build.profile.id>prod</build.profile.id>
</properties>
</profile>
</profiles>
<!-- Package as an executable jar -->
<build>
<finalName>push</finalName>
<!--配置Maven 对resource文件 过滤 -->
<filters>
<filter>src/main/resources/application-${build.profile.id}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
执行生产环境命令
- mvn clean install -Pprod
在类中对properties取值方法
@Value("${push.ios_apns}")
private boolean IOS_APNS;