刘曙尘

导航

Spring boot 分环境部署

一.如果配置文件为:application.yml时 
1.application.yml用于填些公共文件以下为不同环境的配置文件需要单独配置
application-dev.yml application-prod.yml application-test.yml
2.application.yml内配置打包运行的环境
spring:
    profiles:
        active: @package.environment@
#application.properties配置文件中配置
#spring.profiles.active=@profiles.active@
3.配置pom.xml打包配置
<!-- maven不同环境配置 -->
<profiles>
<!-- 开发环境 -->
  <profile>
    <id>test</id>
    <properties>
      <profiles.active>test</profiles.active>
    </properties>
    <activation>
      <!---指定默认激活->
      <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
<!-- 演示环境 -->
  <profile>
    <id>dev</id>
    <properties>
      <profiles.active>dev</profiles.active>
    </properties>
  </profile>
<!-- 生产环境 -->
  <profile>
    <id>prod</id>
    <properties>
      <profiles.active>prod</profiles.active>
    </properties>
   </profile>
</profiles>

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <fork>true</fork>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.tkgm.api.StartAppApplication</mainClass>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
<!-- resources是对配置文件资源的处理,使用见前言部分 -->
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <includes>
        <include>**/*.class</include>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
      </includes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <excludes>
        <exclude>application*.yml</exclude>
      </excludes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
<!--读取打包命令中指定的环境-->
      <filtering>true</filtering>
      <includes>
        <include>application.yml</include>
        <include>application-${profiles.active}.yml</include>
        <include>log4j2.properties</include>
      </includes>
    </resource>
  </resources>
</build>

4.打包命令:打包查看jar打包结果
mvn clean compile package -Dmaven.test.skip=true -Pdev

 

posted on 2019-02-22 12:40  刘曙尘  阅读(719)  评论(0编辑  收藏  举报