Spring boot 分环境部署

2019/02/22 12:40
阅读数 103

一.如果配置文件为:application.properties时 

1.application.properties用于填些公共文件

   以下为不同环境的配置文件需要单独配置

    application-dev.properties

    application-prod.properties

    application-test.properties

 application.yml用于填些公共文件

   以下为不同环境的配置文件需要单独配置

    application-dev.yml

    application-prod.yml

    application-test.yml

 

2.application.properties内配置打包运行的环境

  spring.profiles.active=@profiles.active@

 application.ym配置文件中配置

  # 指定执行环境 

  spring:

   profiles:

    active: @package.environment@

 

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-prod.properties</exclude>
        <exclude>application-dev.properties</exclude>
        <exclude>application-test.properties</exclude>
        <exclude>application.properties</exclude>
      </excludes>
    </resource>
    <resource>
      <directory>src/main/resources</directory>
<!--读取打包命令中指定的环境-->
      <filtering>true</filtering>
      <includes>
        <include>application.properties</include>
        <include>application-${profiles.active}.properties</include>
        <include>log4j2.properties</include>
        <include>jpush.properties</include>
        <include>apiurl.properties</include>
      </includes>
    </resource>
  </resources>
</build>

4.打包命令:打包查看jar打包结果

  mvn clean compile package -Dmaven.test.skip=true -Pdev

 

 

  

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部