Recently, there was a requirement to put the SpringBoot project configuration file outside the Jar package. The Jar package contains no additional configuration files, and the Jar package runs with the external configuration file boot specified. My configuration files in the SRC/main/resources/config, packing to copy the Jar packages at the same level under the config directory (default support SpringBoot read Jar package configuration in the config folder at the same level), the specific solutions are as follows:

  • Use the Maven-jar-plugin to remove the configuration file from the JAR package
  • Use the Maven-resources-plugin to copy the files that need to be copied to the specified path (example: extract configuration files to the specified path)

Maven configuration:

<! -- Declare multiple environments -->
<profiles>
    <profile>
        <! -- Local development environment -->
        <id>dev</id>
        <properties>
            <profiles.active>dev</profiles.active>
            <modifier></modifier>
        </properties>
        <activation>
            <! -- By default, this profile is executed with no arguments -->
            <activeByDefault>true</activeByDefault>      
        </activation>
    </profile>
    <profile>
        <! -- Test environment -->
        <id>test</id>
        <properties>
            <profiles.active>test</profiles.active>
            <modifier>-test</modifier>
        </properties>
    </profile>
    <profile>
        <! -- Production environment -->
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
            <modifier>-prod</modifier>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <! Copy the specified environment configuration file to the specified directory.
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/resources/config</directory>
                                <includes>
                                    <include>application.yml</include>
                                    <include>application-${profiles.active}.yml</include>
                                </includes>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/config</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <! -- Jar package removes all configuration files -->
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <! Jar file type or path -->
                <excludes>
                    <exclude>config/**</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
Copy the code

Finally, execute MVN clean package -p prod