SpringBoot根据Maven动态选择profile打包的几种方式

常用的几种激活profile的方式

  1. 在配置文件中直接指定
    spring.profiles.active=test
    
  2. 使用占位符,在打包时替换,以mavne为例
    spring.profiles.active=@spring.profiles.active@
    
    maven中加入
    <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>
    
    打包时执行以下命令,或者
     mvn package -Ptest  
    
    IDEA右边直接勾选要选择的环境在这里插入图片描述
  3. JVM参数方式
    java命令行:
     java -jar app.jar --spring.profiles.active=dev
    
    或者IDEA在这里插入图片描述
  4. ENV方式(建议使用此方式)

具体使用哪个环境由服务器决定,通过在服务器(例如Tomcat)上的启动脚本里增加:-Dspring.profiles.active=prod来决定使用哪个环境。

优点是不再需要上线测试、生产时手动改环境了,只需要打一个包就可以在所有环境运行,可以避免因打错包或部署错包造成的一系列问题。

这是我项目启动Tomcat的脚本的配置,通过在启动脚本里指定要环境,可以动态选择要启动哪种配置文件在这里插入图片描述
具体maven配置和上述第二种差不多,先在maven配置profile

<build>
    <resources>
        <resource>
            <!-- 指定配置文件所在的resource目录 -->
            <directory>src/main/resources</directory>
            <includes>
                <include>public/**</include>
                <include>static/**</include>
                <include>templates/**</include>
            	<!-- 选择要打包的配置文件和日志,当然也可以不加打包所有配置文件,然后由具体服务器来选择使用哪个 -->
                <include>application-${spring.profiles.active}.yml</include>
               <include>logback-${spring.profiles.active}.xml</include>
            </includes>
            <filtering>true</filtering>
        </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>

配置文件变成如下格式
在这里插入图片描述

好的,那我来为你介绍一下 Spring BootMaven 和 Proguard 打包程序的流程。 首先,Spring BootMaven 都是针对 Java 应用程序的工具。Spring Boot 是一个快速开发框架,可以帮助开发人员快速构建基于 Spring 的应用程序。Maven 是一个构建工具,可以帮助开发人员自动化构建、测试和部署 Java 应用程序。 Proguard 则是一个 Java 字节码混淆和压缩工具,可以将 Java 应用程序中的类、方法、变量等混淆和压缩,从而增加程序的安全性和运行速度。 接下来,我们来讲一下如何使用 Spring BootMaven 和 Proguard 打包程序。 1. 首先,在 pom.xml 文件中添加 Proguard 插件的配置: ```xml <build> <plugins> <plugin> <groupId>com.github.wvengen</groupId> <artifactId>proguard-maven-plugin</artifactId> <version>2.0.14</version> <executions> <execution> <phase>package</phase> <goals> <goal>proguard</goal> </goals> </execution> </executions> <configuration> <injar>${project.build.finalName}.jar</injar> <outjar>${project.build.finalName}-pro.jar</outjar> <obfuscate>true</obfuscate> <options> <option>-keep class com.example.** { *; }</option> </options> </configuration> </plugin> </plugins> </build> ``` 这里简单介绍一下配置: - `<injar>`:输入的 jar 包名称,这里使用了 Maven 的变量 `${project.build.finalName}.jar` 表示生成的 jar 包名称。 - `<outjar>`:输出的 jar 包名称,这里使用了 `${project.build.finalName}-pro.jar` 表示混淆后的 jar 包名称。 - `<obfuscate>`:是否开启混淆,默认为 true。 - `<options>`:Proguard 的配置选项,这里我们只保留了 com.example 包下的所有类和方法。 2. 执行 Maven 打包命令: ```bash mvn clean package ``` 执行完成后,会在 target 目录下生成一个混淆后的 jar 包,名称为 `${project.build.finalName}-pro.jar`。 3. 运行混淆后的 jar 包: ```bash java -jar ${project.build.finalName}-pro.jar ``` 这样,就可以运行混淆后的程序了。 以上就是 Spring BootMaven 和 Proguard 打包程序的简单流程。需要注意的是,混淆后的程序可能会出现运行异常,需要进行相应的调试和排查。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值