1, spring – the boot – maven plugin

The default springboot package tool is spring-boot-maven-plugin

Pom configuration:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.gsafety.bg.enterprise.EnterpriseApplication</mainClass>
        <layout>ZIP</layout>
        <! -- Add a full package to remove the includes. -->
        <includes>
            <include>
                <groupId>com.gsafety.bg</groupId>
                <artifactId>enterprise-controller</artifactId>
            </include>
            <include>
                <groupId>com.gsafety.bg</groupId>
                <artifactId>enterprise-service</artifactId>
            </include>
            <include>
                <groupId>com.gsafety.bg</groupId>
                <artifactId>enterprise-dao</artifactId>
            </include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Copy the code

Packaged directory structure:

Boot-inf contains the following directories: Lib (enterprise-service-1.0.0.jar, enterprise-dao-1.0.0.jar, enterprise-controller-1.0.0.jar), classes, classpath

2, maven – assembly – the plugin

The main purpose of the Maven-assembly-plugin is to allow users to assemble project output along with its dependencies, modules, site documentation, and other files into a distributable archive. In short, it is a custom packaged tool with its own configuration file (Assembly descriptor file). Microservices use this plug-in probability is relatively high, ordinary projects do not need such a way of implementation.

Pom configuration:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>Against 2.4.1</version>
    <configuration>
        <finalName>enterprise</finalName>
        <encoding>utf-8</encoding>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Copy the code

assembly.xml

Can be set to all nodes may refer to website: maven.apache.org/plugins/mav…

<assembly>
    <id>1.0</id>
    <formats>
        <format>tar.gz</format>
    </formats>

    <includeBaseDirectory>true</includeBaseDirectory>
    <! -- Lib package required for project -->
    <! -- <dependencySets>-->
    <! -- &lt; ! &ndash; Package all dependencies into liBS folders. &gt; -->
    <! -- <dependencySet>-->
    <! -- <useProjectArtifact>true</useProjectArtifact>-->
    <! -- <outputDirectory>libs</outputDirectory>-->
    <! -- <scope>runtime</scope>-->
    <! -- </dependencySet>-->
    <! -- </dependencySets>-->
    <fileSets>
        <! Package the startup file to the deploy directory -->
        <fileSet>
            <! Start. sh-->
            <directory>src/main/assembly/bin</directory>
            <! -- Address to pack to -->
            <outputDirectory>deploy</outputDirectory>
            <! - Linux permissions - >
            <fileMode>0755</fileMode>
        </fileSet>
        <! Package the executable jar into the application-server directory
        <fileSet>
            <directory>target</directory>
            <outputDirectory>application-server</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <! SRC /main/resources into logs/enterprise -->
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>logs/enterprise</outputDirectory>
            <fileMode>0755</fileMode>
            <! SRC /main/resources: logs/enterprise -->
            <excludes>
                <exclude>/ * * *</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>
Copy the code

Packaged directory structure:

Unzip executable files in application-server folder:

Found an inconsistency with the packaged directory of spring-boot-Maven-plugin, apparently missing three JARS and some other files in lib

3. Maven-assembly-plugin executable file missing lib

Modifying poM files:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.gsafety.bg.enterprise.EnterpriseApplication</mainClass>
                <layout>ZIP</layout>
                <! -- Add a full package to remove the includes. -->
                <includes>
                    <include>
                        <groupId>com.gsafety.bg</groupId>
                        <artifactId>enterprise-controller</artifactId>
                    </include>
                    <include>
                        <groupId>com.gsafety.bg</groupId>
                        <artifactId>enterprise-service</artifactId>
                    </include>
                    <include>
                        <groupId>com.gsafety.bg</groupId>
                        <artifactId>enterprise-dao</artifactId>
                    </include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
		<! Build project dependencies to lib local, and set the jar to exclude
        <plugin>
            <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>. /.. /.. /lib</outputDirectory>
                        <! -- groupId of jars to exclude -->
                        <excludeArtifactIds>
                            enterprise-controller,enterprise-service,enterprise-dao
                        </excludeArtifactIds>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>Against 2.4.1</version>
            <configuration>
                <finalName>enterprise</finalName>
                <encoding>utf-8</encoding>
                <descriptors>
                    <descriptor>src/main/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>
Copy the code

Plugins refer to spring-boot-Maven-plugin first and then maven-assembly-plugin. Jar: enterprise-service-1.0.0.jar: enterprise-dao-1.0.0.jar: Enterprise-controller-1.0.0. jar: enterprise-dao-1.0.0.jar: Enterprise-controller-1.0.0. jar: enterprise-controller-1.0.0.jar In lib, the Maven-assembly-plugin packages it into enterprise-1.0.tar.gz.

In this case, enterprise-1.0.tar.gz contains the deploy file, the executable file (application-server/enterprise-main-1.0.0.jar), and the log directory (logs/enterprise) The directory structure of the deployment.