Quick packaging and deployment of springboot

Key knowledge points:

Maven's assembly plug-in implements custom packaging deployment (including dependent jar packages)

At present, there are several common deployment methods of spring boot project.

1. Use the docker container to deploy, build the application of springboot into a docker image, and then start the image through the container. This method is very convenient when large-scale application and application expansion need to be deployed. It belongs to the current industrial level deployment scheme, but it needs to master the ecosystem technology of docker.

    

2. Use fatjar to deploy and start directly, which is a simple application deployment method for many beginners or small-scale situations.  

Maven shade plugin, which is used to print executable JAR package, also known as fat JAR package;

3. Packaging with Maven assembly plugin plug-in is also one of the most widely deployed operation and maintenance methods at present, because there are many shell scripts, SQL scripts,. properties and. xml configuration items in big data projects. Using assembly plugin can make the output structure clear and standardized.

 

So go straight to the subject

To configure

pom.xml

 

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <!-- Add to index Not from mainfest Read in classpath,But from Index.list Read in -->
                        <!--<index>true</index> -->
                        <manifest>
                            <!--Startup class main Method-->
                            <mainClass>com.xl.xxx.XXXApplication</mainClass>
                            <!-- Whether to specify classthpath-->
                            <addClasspath>true</addClasspath>
                            <!--For the following<Class-Path>add prefix,The default is empty.,'./'Is the current directory-->
                            <classpathPrefix>./</classpathPrefix>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>../conf/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <exclude>*</exclude>
                        <exclude>*/*</exclude>
                        <exclude>*/static</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-dir</id>
                        <!-- Bound to package Life cycle stage -->
                        <phase>package</phase>
                        <goals>
                            <!-- Bound to package Life cycle stage -->
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}</finalName>
                            <!--Configuration description file path-->
                            <descriptors> <!--Description file path-->
                                <descriptor>${project.basedir}/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

In the above configuration, there are Maven jar plugin default packaging plug-ins. skipTest packaging skips testing. The main configuration is

maven-assembly-plugin

Plug-ins

 

Assembly configuration

Structure catalog

xml configuration

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd
http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 ">
    <id>${project.version}</id><!--id Identifier, a suffix added to the name of the build file. Can not be specified-->

    <formats>
        <!--Packaging method,Support writing multiple include(zip,tar,tar.gz (or tgz),tar.bz2 (or tbz2),jar,dir,war)-->
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!-- fileSet Mapping relationship between project path and packaged relative path-->

        <!--Will all resources The files under files are packed in conf Directory-->
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
        <!--Package startup scripts in bin Directory-->
        <fileSet>
            <directory>${project.basedir}/bin</directory>
            <outputDirectory>bin</outputDirectory>
        </fileSet>
        <!--You can also initialize sql The files are packed in init Directory-->
        <fileSet>
            <directory>${project.basedir}/sql</directory>
            <outputDirectory>init</outputDirectory>
        </fileSet>

    </fileSets>

    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!-- take scope by runtime The dependency package of is packaged into lib Under the directory. -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>


</assembly>

 

bin script command

start.sh

#!/bin/sh
nohup java -jar ../lib/demo-0.1.0-SNAPSHOT.jar >/dev/null 2>&1 &
echo $! > /var/run/demo.pid

stop.sh

#!/bin/sh
PID=$(cat /var/run/demo.pid)
kill -9 $PID

 

Package success

 

After the package is successful, we can upload the tag package directly to the linux server

Deployment process

Upload tag Package >

Keywords: Programming Maven Apache xml Docker

Added by ozestretch on Tue, 11 Feb 2020 15:15:00 +0200