搭建Spring Boot的开发、测试、产品环境的两种方式

目的:通常情况下,开发环境、测试环境、产品环境都是不一样的,为了方便部署时不用修改配置文件直接部署,这里提供两种策略。
你需要有一定的maven和Spring Boot的基础。

  • 基于使用maven实现
  1. 创建maven项目
  2. 添加Spring Boot依赖,pom.xml
<properties>
    <spring-boot.version>2.1.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring-boot.version}</version>
    </dependency>
</dependencies>
  1. 配置开发、测试、产品环境
profiles>
    <profile>
		<!-- 这个ID很有用 -->
        <id>dev</id>
        <properties>
            <environment>dev</environment>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <profile>
        <id>test</id>
        <properties>
            <environment>test</environment>
        </properties>
    </profile>

    <profile>
        <id>pro</id>
        <properties>
            <environment>pro</environment>
        </properties>
    </profile>
</profiles>
  1. maven相关插件以配置文件路径等
<build>
    <finalName>demo1</finalName>
    <filters>
        <!-- src/main/filters这个目录需要新建 -->
		<filter>src/main/filters/config-${environment}.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <!-- 编译插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <!-- 打包时跳过测试 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
        <!-- spring boot 打包插件 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring-boot.version}</version>
            <configuration>
                <fork>true</fork>
                <addResources>true</addResources>
				<!-- 这里是启动类 -->
                <mainClass>com.demo.Application</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>demo1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <spring-boot.version>2.1.0.RELEASE</spring-boot.version>
    </properties>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <environment>dev</environment>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>test</id>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>

        <profile>
            <id>pro</id>
            <properties>
                <environment>pro</environment>
            </properties>
        </profile>
    </profiles>

    <build>
        <finalName>demo1</finalName>
        <filters>
            <filter>src/main/filters/config-${environment}.properties</filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <!-- 编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- 打包时跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!-- spring boot 打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                    <mainClass>com.platform.PlatfomApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
    </dependencies>

</project>
  1. 新建目录src/main/filters,并在该目录下新建文件

    config-dev.properties
_server.port = 9999

config-test.properties

_server.port = 8888

config-pro.properties

_server.port = 80
  1. 添加启动类,com.demo.Application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
public class Application {

    @ResponseBody
    @RequestMapping("/test")
    public String test() {
        return "hello world";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
  1. src/main/resources下新建application.yml
server:
  port: ${_server.port} # ${},这里面的内容是config-xxx.properties里面的key
  1. 右键 >> debug,浏览器访问http://127.0.0.0:9999/test,返回“hello world”表示成功。
  2. mvn打包,打包完成后在target目录下会存在一个可执行jar
# 开发环境包,默认
mvn clean package -Pdev
# 测试包
mvn clean package -Ptest
# 产品包
mvn clean package -Ppro
  1. Java运行,不一样的命令打出来的使用的是不用的配置文件。
java -jar demo.jar
  • 基于Spring Boot实现,其实也属于maven项目
  1. 新建maven项目
  2. 引入Spring Boot依赖
  3. maven插件,这里只需要引入插件就行,就是里面的内容
  4. 在src/main/resources下新建文件

    application.yml
server:
  port: 9999

application-dev.yml

server:
  port: 9999

application-test.yml

server:
  port: 8080

application-pro.yml

server:
  port: 80
  1. maven打包
mvn clean package
  1. 执行
java -jar xxx.jar --spring.profiles.active=dev
java -jar xxx.jar --spring.profiles.active=test
java -jar xxx.jar --spring.profiles.active=pro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流年ln

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值