SpringBoot之jar多环境打包

SpringBoot打包成jar是对SpringBoot较好的部署方式,毕竟SpringBoot的优势就是在于前后端分离。如果不对SpringBoot进行任何配置,默认jar打包命令为 mvn clean package。大佬说这是不规范的,然后在pom.xml文件中配置与<parent/>标签同级别

<packaging>jar</packaging>

然后使用mvn clean install 就可以打包成jar包了。但是简简单单的这样是不行的,一般打包部署是要切换环境的,一般分为test(测试)、dev(开发)、prod(生产) 三个环境,当然名称自己开心就好。于是我就按照SpringMVC那一套来配置,发现出现了一系列的问题。网上查了下资料,发现基本都是在运行jar包时这样做的

java -jar xxx.jar --spring.profiles.active=test

当然你还需要三个名为 application-test.properties、application-dev.properties、application-prod.properties的.properties文件,.yml文件道理是一样的,然后还有一个application.properties这个文件就是三个环境都通用的配置。在application.properties可以配置默认启动spring.profiles.active=dev。其实这样就已经可以解决多环境切换的问题了。但是大佬说,这样不好。要在打包的时候就已经切换好环境,也就是 要使用 mvn clean install -P xxx 这样的命令,xxx就是test、dev和prod 你的环境名。但是SpringBoot似乎默认是不支持这样的。

查了半天资料,最后这样解决了。

解决方案:

pom.xml

    <profiles>
		<profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <!-- 默认环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

这是与<parent/>标签同级别的啊,别搞错了。

<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>2.5</version>
         <configuration>
              <skip>true</skip>
         </configuration>
      </plugin>
    </plugins>
 <filters> 
  <filter>src/main/resources/filters/application-${profiles.active}.properties</filter>
 </filters>
 <resources>
      <resource>
           <filtering>true</filtering>
               <directory>src/main/resources</directory>
      </resource>
 </resources>
</build>

我把默认的<build/>标签配置一并复制过来了,偷懒的同学直接复制就行。

然后把上面说的application-test.properties、application-dev.properties、application-prod.properties这三个文件复制到src/main/resource/filters 下面,自己创建filters文件夹啊。可以在上面的<filter/>标签自己配置。还有application.properties

#mysql
spring.datasource.driver-class-name="@db.driver@"
spring.datasource.username="@db.username@"
spring.datasource.password="@db.password@"
spring.datasource.url="@db.url@"

需要这样配置。你application-xxx.properties文件里相关配置是啥名,在这里用@xxx@配置即可,如上所示。我application-test.properties是这样配置的

db.url=jdbc:mysql://xxx:3306/xxx?characterEncoding=utf8
db.username=xxx
db.password=xxx
db.driver=com.mysql.jdbc.Driver

这里的application-test.properties文件是不会被springboot检查的,你想叫就叫啥名。比如db.url改成database.url,spring.datasource.url="@db.url@"改成spring.datasource.url="@database.url@"即可。

最后使用 mvn clean install -P test  打包成jar,大功告成!

总结,springboot还是很灵活的,看你自己觉得那种好用就行。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值