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

相关推荐
wuxuanok43 分钟前
Web后端开发-分层解耦
java·笔记·后端·学习
31535669131 小时前
ClipReader:一个剪贴板英语单词阅读器
前端·后端
ladymorgana1 小时前
【Spring Boot】HikariCP 连接池 YAML 配置详解
spring boot·后端·mysql·连接池·hikaricp
neoooo1 小时前
别慌,Java只有值传递——一次搞懂“为啥我改了它还不变”!
java·后端·spring
用户7785371836961 小时前
一力破万法:从0实现一个http代理池
后端·爬虫
拖孩2 小时前
微信群太多,管理麻烦?那试试接入AI助手吧~
前端·后端·微信
Humbunklung2 小时前
Rust枚举:让数据类型告别单调乏味
开发语言·后端·rust
radient2 小时前
Golang-GMP 万字洗髓经
后端·架构
蓝倾2 小时前
如何使用API接口实现淘宝商品上下架监控?
前端·后端·api
舂春儿2 小时前
如何快速统计项目代码行数
前端·后端