SpringBoot之分环境打包

假设项目需要分3个环境部署:dev、test、prd,  对于springboot的项目, 可以使用以下方式:

1, 使用springboot自己的profile

springboot的多环境配置文件名必须约定满足application-{profile}.properties的格式, 如:

然后需要设置spring.profiles.active的值,才可以激活对应的profile.

我们可以在application.properties文件中激活, 如:

spring.profiles.active=dev

也可以在运行jar包时激活, 会覆盖上面的, 如:

java -jar hello.jar --spring.profiles.active=test
或者
java -Dspring.profiles.active=test -jar hello.jar 

注解@Profile介绍:

在springboot中开发java组件时,可以通过@Profile决定该类是否实例化, @Profile(“dev”)表示只有spring.profiles.active=dev时,才会去实例化当前类 .可以用在一些场景中, 比如: 在开发测试环节, 需要屏蔽掉发短信接口. 请看代码:

@Service
@Profile({"dev","test"})
public class DevTestSmsServiceImpl implements SmsService {
    @Override
    public void send() {
        System.out.println("开发测试阶段不需要发短信");
    }
}

@Service
@Profile("prd")
public class PrdSmsServiceImpl implements SmsService {

    @Override
    public void send() {
        System.out.println("调用真实短信接口");
    }
}

2, 使用打包工具的profile

对于springboot的profile,有人会觉得对配置文件的路径和命名约定会带来不灵活, 也有人会觉得把所有配置文件不分环境青红皂白地都打进jar包里是不是有点粗暴?

回到传统的做法上来, 我们可以考虑使用打包工具的profile, 在打包阶段即划清各个环境的配置.

maven profilehttp://maven.apache.org/guides/introduction/introduction-to-profiles.html )

官网介绍maven profile的使用方式有很多,  这里选取比较常用的一种, 在pom文件里配置好profile, 然后在mvn 命令打包时进行激活.

pom文件, 如:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wx</groupId>
    <artifactId>hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.12.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!--定义3个profile-->
    <profiles>
        <profile>
            <id>dev-profile</id>
            <activation>
                <property>
                    <name>maven.profile.env</name>
                    <value>dev</value>
                </property>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>

        <profile>
            <id>test-profile</id>
            <activation>
                <property>
                    <name>maven.profile.env</name>
                    <value>test</value>
                </property>
            </activation>
            <properties>
                <env>test</env>
            </properties>
        </profile>


        <profile>
            <id>prd-profile</id>
            <activation>
                <property>
                    <name>maven.profile.env</name>
                    <value>prd</value>
                </property>
            </activation>
            <properties>
                <env>prd</env>
            </properties>
        </profile>

    </profiles>

    <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <!--定制打包时的资源拷贝-->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>./</targetPath>
                <includes>
                    <include>application.properties</include>
                    <include>application-${env}.properties</include>
                </includes>
            </resource>
        </resources>

    </build>

</project>

然后在mvn命令中设置maven.profile.env的值, 如:

mvn clean package -Dmaven.skip.test=true -Dmaven.profile.env=prd

则最终的打进jar包里的配置文件就是这样的: 

猜你喜欢

转载自my.oschina.net/wangxu3655/blog/1807821