Spring boot multi environment separation of resources and lib for packaging

Keywords: Java Maven Spring Apache xml

In order to facilitate local development, testing and production packaging are occasionally involved. The configuration of each environment is different. Configuration files of multiple environments need to be configured to avoid having to modify the configuration files when packaging

The default configuration file of the SpringBoot project is application.properties under main/resources and the multi environment properties are set through the configuration item spring.profiles.active. Therefore, the application.properties of multiple environments can be created for this file. Only spring.profiles.active distinguishes specific environment values, and then configures and packs them through maven

First, create the configuration files of each environment. The directory is as follows

Dev is the development environment, spring.profiles.active=dev

Test is the test environment, spring.profiles.active=test

Prod is the production environment, spring.profiles.active=prod

pom.xml file add

<profiles>
    <!-- development environment -->
    <profile>
        <id>dev</id>
        <properties>
            <package.environment>dev</package.environment>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!-- testing environment -->
    <profile>
        <id>test</id>
        <properties>
            <package.environment>test</package.environment>
        </properties>
    </profile>
    <!-- production environment -->
    <profile>
        <id>prod</id>
        <properties>
            <package.environment>prod</package.environment>
        </properties>
    </profile>
</profiles>

Split resources and lib package zip file

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>dev/*</exclude>
                <exclude>test/*</exclude>
                <exclude>prod/*</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources/${package.environment}</directory>
        </resource>
    </resources>
    <plugins>
        <!-- Separation of lib -->
        <plugin>
            <!--Copy the dependent jar package and put it into the compiled target/lib directory, and exclude the internal dependency during packaging-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Detach resource files -->
        <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</directory>
                            </resource>
                        </resources>
                        <outputDirectory>${project.build.directory}/resources</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--Pack jar-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <!-- Specify the resource file directory, which is the same level as the packaged jar file -->
                    <manifestEntries>
                        <Class-Path>resources/</Class-Path>
                    </manifestEntries>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.schy.transfer.api.ApiApplication</mainClass>
                    </manifest>
                </archive>
                <!-- The files ignored during packaging (that is, the files not entered into the jar package). In this example, all the. yml and. properties files under resources are excluded -->
                <excludes>
                    <exclude>**/*.xml</exclude>
                    <exclude>**/*.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
        <!-- spring boot repackage -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.0.1.RELEASE</version>
            <configuration>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>non-exists</groupId>
                        <artifactId>non-exists</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!--Package as compressed-->
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.artifactId}-${project.version}-${package.environment}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptors>
                            <descriptor>src/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

assembly plug-in configuration

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>transfer-api-assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>target/classes</directory>
            <outputDirectory>/resources</outputDirectory>
            <includes>
                <include>*.properties</include>
                <include>*.xml</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>target/lib</directory>
            <outputDirectory>/lib</outputDirectory>
            <excludes>
                <exclude>${project.groupId}:${project.artifactId}</exclude>
            </excludes>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>target</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.artifactId}-*.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
</assembly>

 

Finally, use the command to package the development environment

mvn clean install -Dmaven.test.skip=true -Pdev

The packaged file structure is

Use java-jar test-1.0.jar to run

Posted by riespies on Tue, 31 Mar 2020 02:38:28 -0700