Packaging of springboot production environment

In the production environment, the packaging method of the java program, if it is a web type, it is usually packaged as a war package. The general service type is packaged into a jar package. In springboot, by default, when packaging into jar, all files of the project will be packaged into a jar package. This bag will be very large. Configuration files are built in. Can not be modified. If modified, the entire project needs to be repackaged. due to the above shortcomings. Now improve the packaging method, the advantages of the improved packaging method:

  1. The jar package and the dependent lib package of the jar package are separated. When no new dependencies are added, republishing the project only needs to release the jar package, which is usually only a few hundred K.
  2. The config file of the project is separated from the jar. If you need to modify it, it will take effect after restarting. No need to repackage.

The following steps are described in detail:

1. Add build to the project's pom file

<build>
        <!-- 打包后文件的最终名字-->
        <finalName>${artifactId}-${version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!-- springboot 的main 启动类 方法的入口 -->
                    <mainClass>cn.com.SpringbootDemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>

            </plugin>
            <!-- The configuration of maven-assembly-plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <!-- The configuration of the plugin -->
                <configuration>
                    <descriptors>
                          <!-- 配置 assembly 的路径 -->
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2. Create a new src/assembly/assembly.xml file and configure the specific packaging method in this file

<assembly>
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>

    <dependencySets>
        <dependencySet>
               <!--不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录-->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!-- 把项目脚本文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.basedir}/src/bin</directory>
            <outputDirectory>bin</outputDirectory>
            <includes>
                <include>*.sh</include>
            </includes>
        </fileSet>

        <!-- 把配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>**/*</include>
                <include>*.xml</include>
                <include>*.properties</include>
                <include>*.yml</include>
                <include>*.key</include>
            </includes>
        </fileSet>

        <!-- 把jar,打进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Create a new src/conf folder, and package the configuration files under src/main/resources into the conf folder of the zip package when packaging is performed.

Create a new src/bin folder, and add scripts such as the startup of the corresponding project in this folder. After packaging, it will be in the bin folder of the zip package.

Finally, execute the maven package command. The corresponding zip package will be found in the target directory.

Upload it to the server and unzip it using the unzip command. will get the following files

Enter image description

Go to the bin directory and find that the start.sh script starts running.

Enter image description

run up hey.

Enter image description

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325064239&siteId=291194637