聊聊基于maven的springboot的“过时“用法

接触过许多工程,发现有一些基于maven的springboot工程还是使用maven的profile,有些"过时"了,下面简单介绍一下。

示例1

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>staging</id>
            <properties>
                <profiles.active>staging</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 正式环境 -->
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

    <build>
    	<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <configuration>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-service-properties</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <copy todir="target/classes/config" overwrite="true">
                                    <fileset dir="${basedir}/src/main/resources/deploy/${profiles.active}">
                                        <include name="*.yml"/>
                                    </fileset>
                                </copy>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    	</plugins>
    </build>

src/main/resources

复制代码
├── config
│   ├── application.yml
│   └── bootstrap.yml
├── deploy
│   ├── Dockerfile
│   ├── dev
│   │   ├── application.yml
│   │   └── bootstrap.yml
│   ├── prod
│   │   ├── application.yml
│   │   └── bootstrap.yml
│   ├── staging
│   │   ├── application.yml
│   │   └── bootstrap.yml
│   └── test
│       ├── application.yml
│       └── bootstrap.yml

这种方法呢,感觉是多此一举,用application-{profile}.yml不香吗,感觉是没有用上springboot之前的maven工程的用法

示例2

pom.xml

复制代码
    <profiles>
        <profile>
            <!-- 开发环境 -->
            <id>dev</id>
            <properties>
                <app.active>dev</app.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <!-- 测试环境 -->
            <id>test</id>
            <properties>
                <app.active>test</app.active>
            </properties>
        </profile>
        <profile>
            <!-- 正式预生产环境 -->
            <id>staging</id>
            <properties>
                <app.active>staging</app.active>
            </properties>
        </profile>
        <profile>
            <!-- 正式环境 -->
            <id>prod</id>
            <properties>
                <app.active>prod</app.active>
            </properties>
        </profile>
    </profiles>

application.yml

复制代码
spring:
  profiles:
    active: @app.active@

这种用法呢,src/main/resources下面只有一个application.yml,把profile的差异放到了maven的profile中,在application.yml引用maven的profile变量,有点"少见多怪",直接application-{profile}.yml用不香吗。

小结

springboot工程已经提供了profile的特性了,其实大部分场景可以替换掉maven的profile,没必要在使用maven的profile了,不然总感觉显得有点古老和过时。

相关推荐
舒一笑1 小时前
PandaCoder:致敬MyBatis Log Plugin,但我们做得更极致!
后端·程序员·intellij idea
简单点了3 小时前
go前后端项目的启动 、打包和部署
开发语言·后端·golang
雨夜之寂4 小时前
mcp java实战 第一章-第一节-MCP协议简介.md
java·后端
摇滚侠4 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 核心语法 笔记39
spring boot·笔记·后端·thymeleaf
九丶弟5 小时前
SpringBoot的cache使用说明
java·spring boot·spring·cache
Code blocks6 小时前
GB28181视频服务wvp搭建(二)
后端·音视频
码事漫谈6 小时前
C++与C#布尔类型深度解析:从语言设计到跨平台互操作
后端
程序新视界6 小时前
在MySQL中,一条SQL语句的执行全流程是怎样的?
数据库·后端·mysql
lang201509286 小时前
打造专属Spring Boot Starter
java·spring boot·后端
码事漫谈7 小时前
解决Python调用C++ DLL失败的问题:extern "C"的关键作用
后端