springboot下maven分环境打包

时间:2023-01-17 09:08:15

1 项目结构如下图:local,test,prod是具体的环境

 springboot下maven分环境打包

2 配置profile,默认为local

 <profiles>
        <profile>
            <id>local</id>
            <properties>
                <profiles.active>local</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

3 根据环境打包:

 <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>application-${profiles.active}.properties</include>
                    <include>application.properties</include>
                    <include>logback-spring.xml</include>
                    <include>static/**/*</include>
                </includes>
                <filtering>true</filtering>//必须设置为true,springboot那里的设置才有效果
            </resource>
        </resources>

4:打包的插件   woff,woff2,ttf的配置是因为这种方式会对二进制资源损坏。所以将这几种排除

         <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                        <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                        <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>

 

5 application.properties中的配置  这里的profiles.active要与第二步和第三步的名称相同

spring.profiles.active= @profiles.active@

 

6 打包命令   这里打包的是测试环境 命令格式:clean install -P环境名    

springboot下maven分环境打包