精通Spring Boot
内容纲要

1.背景介绍

新建Spring Boot后,会自带打包方式,现在一般都是打包成jar包,当然你想打包成war包也可以,我就不介绍了!
本文主要想谈谈自带的打包方式和assembly打包方式,这两者有什么区别和优缺点呢?
精通Spring Boot

2.自带打包方式

使用IDEA 的 spring initializr或者start.spring.io创建 Spring Boot 项目后,可以在 pom.xml文件中看到自带的 maven 打包方式

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

使用命令 mvn clean package 即可将项目打包成jar包,但这种打包的方式将所有的配置文件和模板文件(如果存在template的话)都打包在jar中,如果更改,必须重新打包。

思考一下

这样的打包方式确实非常简单和方便,但是当我们修改配置时,就需要重新打包发布,还有个问题就是,线上数据库地址是在配置文件中的,开发人员一般是不知道的(当然运维也不会告诉你,免得误操作),那难道让运维去打包??明显不可能!所以我们可以采取下面的assembly打包方式!

3.assembly打包方式

第一步:
排除Spring Boot 自带的打包插件:注释或删除pom.xml中的代码

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <fork>true</fork>
            <mainClass>com.joyreach.base.JoyBaseServerApplication</mainClass>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

第二步:
添加assembly打包插件,在pom.xml中添加

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <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>

第三步
配置assembly:
首先在pom.xml中,添加如下代码,分离配置文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
        <excludes>
            <exclude>*.*</exclude>
        </excludes>
        <archive>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <addClasspath>true</addClasspath>
                <classpathPrefix>../lib/</classpathPrefix>
                <mainClass>com.guuidea.basesave.BaseSaveApplication</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>../conf/</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

其次,在assemble.xml中配置

<?xml version="1.0" encoding="UTF-8" ?>
<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>${project.version}</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <!-- 项目的依赖 -->
        <dependencySet>
            <!-- 排除当前项目的构件 -->
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
        <fileSet>
            <directory>lib/</directory>
            <outputDirectory>${file.separator}lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>

        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>${file.separator}bin</outputDirectory>
            <includes>
                <include>${project.artifactId}-${project.version}.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>${file.separator}conf</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

说下我遇到的坑:

  • 1.include一定要将所有的配置文件都包含进去。
    1. exclude 一定要排除jar包下的所有配置文件,否则,将会默认先使用jar包中的配置,这也就是为什么修改了conf目录下的配置文件后,没有生效的原因!

      4.总结一下

      两种方式各有利弊吧,默认的方式方便快捷,更适合用来开发,测试。
      assembly打包方式则是去服务化和工程化的,更适用公司的流程和生产。
      如果公司大部分项目部署,是由开发来完成的那么推荐采用自带的方式,如果有运维专门维护上线,用assembly更为规范一些。

By liu luli

8年IT行业从业经验,参与、负责过诸多大型项目建设。掌握多门编程语言,对Java、Python编程有较为深刻的理解。现为杭州某公司开发负责人。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注