聊聊基于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 小时前
Springboot3.5.x结构化日志新属性
spring boot·后端·spring
涡能增压发动积1 小时前
一起来学 Langgraph [第三节]
后端
sky_ph2 小时前
JAVA-GC浅析(二)G1(Garbage First)回收器
java·后端
涡能增压发动积2 小时前
一起来学 Langgraph [第二节]
后端
TTDreamTT2 小时前
SpringBoot十二、SpringBoot系列web篇之过滤器Filte详解
spring boot
hello早上好2 小时前
Spring不同类型的ApplicationContext的创建方式
java·后端·架构
roman_日积跬步-终至千里2 小时前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
00后程序员3 小时前
提升移动端网页调试效率:WebDebugX 与常见工具组合实践
后端
HyggeBest3 小时前
Mysql的数据存储结构
后端·架构
TobyMint4 小时前
golang 实现雪花算法
后端