聊聊基於maven的springboot的"過時"用法

發表於2023-09-24

接觸過許多工程,發現有一些基於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了,不然總感覺顯得有點古老和過時。

相關文章