美文网首页
SpringBoot 多环境配置与打包

SpringBoot 多环境配置与打包

作者: 敲代码的本愿 | 来源:发表于2021-03-09 17:18 被阅读0次

    配置

    1. resources中新增 dev开发、test测试、prod生产三个文件:

    application.yml中是公共配置。

    手动修改

    此时便可以通过手动修改active值切换环境。

    Maven执行

    1. 修改application.ymlactive的值。
    2. pom.xml中新增resourcesprofiles
    <project>
        //省略
        <dependencies>
          ...
        </dependencies>
    
        <build>
            <plugins>
            ...
            </plugins>
            <!--打包过滤文件-->
            <resources>
                <resource>
                    <!-- 指定配置文件所在的resource目录 -->
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>application.yml</include>
                        <include>application-${environment}.yml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    
        <!--多环境打包-->
        <profiles>
            <profile>
                <id>dev</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <environment>dev</environment>
                </properties>
            </profile>
            <profile>
                <id>test</id>
                <properties>
                    <environment>test</environment>
                </properties>
            </profile>
            <profile>
                <id>prod</id>
                <properties>
                    <environment>prod</environment>
                </properties>
            </profile>
        </profiles>
    </project>
    

    activeByDefault 设置为默认,environment环境名称

    1. 点击Reload,会出现Profile,根据选择切换环境,完成。

      5.命令行打包
    mvn clean install -P dev
    mvn clean install -P test
    mvn clean install -P prod
    

    延伸

    获取服务端口


    问题

    1. 无法使用@符号

      解决:在pom.xmlpluns中新增如下,再Reload Maven
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
             <delimiters>@</delimiters>
             <useDefaultDelimiters>false</useDefaultDelimiters>
        </configuration>
    </plugin>
    

    代码

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.3</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>test2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>test2</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
            <skipTests>true</skipTests>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.4</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <delimiters>@</delimiters>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                    </configuration>
                </plugin>
            </plugins>
            <!--打包过滤文件-->
            <resources>
                <resource>
                    <!-- 指定配置文件所在的resource目录 -->
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>application.yml</include>
                        <include>application-${environment}.yml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    
        <!--多配置打包-->
        <profiles>
            <profile>
                <id>dev</id>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <environment>dev</environment>
                </properties>
            </profile>
            <profile>
                <id>test</id>
                <properties>
                    <environment>test</environment>
                </properties>
            </profile>
            <profile>
                <id>prod</id>
                <properties>
                    <environment>prod</environment>
                </properties>
            </profile>
        </profiles>
    </project>
    

    application.yml

    spring:
      profiles:
        active: @environment@
    server:
      servlet:
        context-path: /h
    logging:
      level:
        root: info
      file:
        name: ./logs/test.log
    #mybatis的相关配置
    mybatis:
      #mapper配置文件
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.example.test2.entity
      #开启驼峰命名
      configuration:
        map-underscore-to-camel-case: true
    

    application-dev.yml,方便测试testprod只改了端口

    server:
      port: 8081
    spring:
      datasource:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://ip:3306/test?characterEncoding=utf-8&useSSL=false
          username: root
          password: admin
    

    相关文章

      网友评论

          本文标题:SpringBoot 多环境配置与打包

          本文链接:https://www.haomeiwen.com/subject/ugkbqltx.html