聊聊基于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了,不然总感觉显得有点古老和过时。

相关推荐
技术小丁17 小时前
使用 PHP 和 PhpSpreadsheet 在 Excel 中插入图片(附完整代码)
后端·php
SimonKing17 小时前
消息积压、排查困难?Provectus Kafka UI 让你的数据流一目了然
java·后端·程序员
考虑考虑17 小时前
点阵图更改背景文字
java·后端·java ee
晴殇i17 小时前
千万级点赞系统架构演进:从单机数据库到分布式集群的完整解决方案
前端·后端·面试
ldmd28417 小时前
Go语言实战:入门篇-5:函数、服务接口和Swagger UI
开发语言·后端·golang
ZHE|张恒17 小时前
Spring Boot 3 + Flyway 全流程教程
java·spring boot·后端
隔壁阿布都18 小时前
Spring Boot中的Optional如何使用
开发语言·spring boot·python
Mintopia18 小时前
🧠 Next.js × GraphQL Yoga × GraphiQL:交互式智能之门
前端·后端·全栈
林太白18 小时前
rust16-职位管理模块
后端·rust
canonical_entropy18 小时前
Nop平台到底有什么独特之处,它能用在什么场景?
java·后端·领域驱动设计