SpringBoot下Maven-多环境打包配置详解

在实际开发过程中,可能需要不断进行环境的切换和打包部署,通常我们会选择在application.properties中修改不同环境对应的配置文件,这种方式不仅效率低,而且很容易发生错误,造成不必要的麻烦降低工作效率。maven提供了多环境配置,可以方便实现不同环境的配置切换和打包。

多环境打包

pom.xml文件加上根据环境来打包.为了不把多余的其它环境的配置打到包里面。

打包命令

mvn clean package
mvn clean package -Pdev
mvn clean package -Ptest
mvn clean package -Pprod

执行命令,指定-P参数,启用指定的profile

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">
  ...
  
  <build>
    <plugins>
      <!-- maven-compiler-plugin用来编译Java代码 -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <!-- maven-resources-plugin用来处理资源文件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version> <!-- @wjw_note: 指定版本号,解决maven.plugin.MojoExecutionException: Input length = 1异常 -->
        <configuration>
          <propertiesEncoding>UTF-8</propertiesEncoding>
        </configuration>
     </plugin>
    </plugins>
    <resources>
      <resource>
        <filtering>true</filtering>
        <!-- 指定配置文件所在的resource目录 -->
        <directory>src/main/resources</directory>
        <includes>
          <include>public/**</include>
          <include>static/**</include>
          <include>templates/**</include>
          <include>tpl/**</include>
          <include>application.*</include>
          <include>application-${spring.profiles.active}.*</include>
          <include>log*-${spring.profiles.active}.xml</include>
        </includes>
      </resource>
    </resources>
  </build>

  <profiles>
    <profile>
      <id>dev</id>
      <properties>
        <spring.profiles.active>dev</spring.profiles.active>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <profile>
      <id>test</id>
      <properties>
        <spring.profiles.active>test</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>prod</id>
      <properties>
        <spring.profiles.active>prod</spring.profiles.active>
      </properties>
    </profile>
  </profiles>
</project>

配置阿里云镜像

为了提升下载速度.

pom.xml例子:

<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">

...   
  <repositories>  <!-- 配置阿里云镜像仓库 -->
    <repository>
      <id>nexus-aliyun</id>
      <name>nexus-aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
...   
</project> 

<<<<<<<<<<<< 🏆 >>>>>>>>>>>>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱游泳的老白

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

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

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

打赏作者

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

抵扣说明:

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

余额充值