Spring Boot中使用profiles 打包
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─example
│ │ │ ├─config
│ │ │ ├─controller
│ │ │ ├─dao
│ │ │ ├─model
│ │ │ ├─rabbitmq
│ │ │ ├─service
│ │ │ │ └─impl
│ │ │ └─util
│ │ └─resources
│ │ └─mapper
配置文件到放到resources目錄中
不同的環(huán)境配置變量要用@@包裹
- application.properties
- spring.datasource.url=@spring.datasource.username@
- server.port=${port:16500}
- push.ios_apns=@push.ios_apns@
不同環(huán)境變零根據(jù)類型直接寫入
- 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>
執(zhí)行生產(chǎn)環(huán)境命令
- mvn clean install -Pprod
在類中對properties取值方法
@Value("${push.ios_apns}")
private boolean IOS_APNS;