Maven多环境打包配置

一、启动时指定环境配置文件

在启动springboot应用的jar包时,我们可以指定配置文件,通常把配置文件上传到linux服务器对应jar包的同级目录,或者统一的配置文件存放目录

   java -jar your-app.jar --spring.config.location=/opt/softs/applications-prod.yaml

实际可以把配置文件路径spring.config.location后面的替换成你自己的存放路径,把配置文件放到服务器上可以很方便的随时修改内容。
以上说的是在启动jar包的时候指定配置文件,那么如何在maven打jar包的时候同时把指定配置文件打包进去,直接启动就好了?请看接下来的分析

二、maven多环境打包配置

首先创建一个springboot应用(如果是微服务,且每个服务都是springboot应用的话,都按照同样的配置即可,笔者这里是为了举例说明),大概的结构如下
在这里插入图片描述
代码结构不重要,重点是pom文件和配置文件怎么配置

多个配置文件创建

假设现在有三个环境,分别是开发环境、测试环境、生产环境,这也是标准的开发、测试、上线流程所需要的软件环境。笔者在src/main/resources下创建了四个配置文件
在这里插入图片描述

application.yaml

这个是主配置文件,里面可以配置一些通用和基础的配置,比如连接数据,mybatis等等

spring:
  profiles:
    active: @environment@
  datasource:
    # 数据库连接信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/usertest?useSSL=false&serverTimezone=UTC
    username: root
    password: root
mybatis:
  # MyBatis配置
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.execute.batch.executebatch.mapper
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

也可以只配置如下

spring:
  profiles:
    active: @environment@

笔者这里只配置了如下内容,active是使用哪个环境的配置,这里是一个变量,先不管后面再说明
在这里插入图片描述

application-dev.yaml

这个是开发环境配置,配置如下
在这里插入图片描述
端口号,和数据库配置,mybatis配置。数据库配置一般在各个环境使用的都不同,所以应该分别配置到对应环境下的配置文件中。mybatis配置这个一般是不变的,一般可以放在上面的application.yaml主配置文件中,不过笔者这里没有动了。
开发环境端口号是4455

application-test.yaml

这个是测试环境配置,内容如下
在这里插入图片描述
内容同上,只是端口不一样,为6677

application-prod.yaml

生产环境配置文件
在这里插入图片描述
生产环境的端口号是7788

pom文件配置

首先是profiles配置

<profiles>
        <profile>
            <!--不同环境Profile的唯一id-->
            <id>dev</id>
            <!--默认激活dev 环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!--environment是自定义的字段(名字随便起),自定义字段可以有多个,确保与配置文件一致-->
                <environment>dev</environment>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>prod</environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>
    </profiles>

有三组profile,分别对应三个环境dev、prod、test
注: < e n v i r o n m e n t > d e v < / e n v i r o n m e n t > <environment>dev</environment> <environment>dev</environment>这个environment标签可以随意定义,只要application.yaml里面能对应上就行
在这里插入图片描述
build配置

    <!-- 打包需要引入对应环境的配置文件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--打包时跳过测试-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes</outputDirectory>
                            <useDefaultDelimiters>false</useDefaultDelimiters>
                            <delimiters>
                                <delimiter>@</delimiter>
                            </delimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/java</directory>
                                    <includes>
                                        <include>**/*.xml</include>
                                    </includes>
                                    <filtering>true</filtering>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>true</filtering>
                                    <includes>
                                        <include>**/*.yaml</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <excludes>
                                        <exclude>**/*.yaml</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>print-custom-info</id>
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <!--suppress UnresolvedMavenProperty -->
                                <echo message="[INFO] ${environment} 环境打包执行成功"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

有两个需要注意的点
首先第一个打包时跳过测试的配置
在这里插入图片描述
这样我们在打包生产环境时的命令

mvn clean package -Pprod

而不是下面的,去掉了 -Dmaven.test.skip=true

mvn clean package -Pprod -Dmaven.test.skip=true

第二个分割符解析
在这里插入图片描述
这里的配置实际上是为了识别下面配置的@符号,从环境变量中读取具体哪个环境
在这里插入图片描述
注:上面的@符号可以换成其他的符号分割比如*或者&,一般还是不换比较好

三、IDEA启动调整

以上配置好后,IDEA启动就无法启动了,发现无法识别主配置里的自定义变量
在这里插入图片描述
这个是我们打包时候用到的,IDEA启动自然无法识别,打开右侧的maven配置,会发现多了几个配置文件(native 和nativeTest这两个是maven自己生成的不用管),我们勾选上dev
在这里插入图片描述
再次点击启动,发现启动成功了,这个主要是我们自己开发本地测试时使用
在这里插入图片描述
当你有使用其他环境,比如使用test环境启动的需求时,把dev勾选取消,仍然还有灰色的暗勾选,此时maven没有自动刷新。勾选上test后,点击左上角的刷新按钮,就会变成如下界面
在这里插入图片描述
再次启动,就会使用测试环境配置启动了
在这里插入图片描述

四、指定环境打包

有了以上的环境配置准备,我们就可以在打包时指定配置,而不是运行时指定配置环境了,笔者使用的时idea开发,进入terminal控制台输入如下命令,打包开发环境配置的jar包

mvn clean package -Pprod

在这里插入图片描述
在这里插入图片描述
看到打包成功了

五、启动jar包

windows下使用cmd命令进入打包后的项目target目录下执行

java  -jar execute-batch-0.0.1-SNAPSHOT.jar

在这里插入图片描述
启动成功了,我们的生产环境端口号正是7788
在这里插入图片描述
postman调用下接口
在这里插入图片描述
到这里maven多环境打包配置完成了

六、完整pom文件

笔者这里附上完整的pom文件内容,可供复制参考,主要是 < b u i l d > < / b u i l d > <build></build> <build></build> < p r o f i l e s > < / p r o f i l e s > <profiles></profiles> <profiles></profiles>中的配置

<?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>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.execute.batch</groupId>
    <artifactId>execute-batch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>execute-batch</name>
    <description>execute-batch</description>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>3.0.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</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-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/classes</outputDirectory>
                            <useDefaultDelimiters>false</useDefaultDelimiters>
                            <delimiters>
                                <delimiter>@</delimiter>
                            </delimiters>
                            <resources>
                                <resource>
                                    <directory>src/main/java</directory>
                                    <includes>
                                        <include>**/*.xml</include>
                                    </includes>
                                    <filtering>true</filtering>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <filtering>true</filtering>
                                    <includes>
                                        <include>**/*.yaml</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/</directory>
                                    <excludes>
                                        <exclude>**/*.yaml</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <id>print-custom-info</id>
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <!--suppress UnresolvedMavenProperty -->
                                <echo message="[INFO] ${environment} 环境打包执行成功"/>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!--不同环境Profile的唯一id-->
            <id>dev</id>
            <!--默认激活dev 环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <!--environment是自定义的字段(名字随便起),自定义字段可以有多个,确保与配置文件一致-->
                <environment>dev</environment>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>prod</environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>
    </profiles>
    
</project>

  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值