springboot-使用assembly进行项目打包

摘要:
--它主要使用Maven提供的程序集插件来完成--˃Maven程序集插件false${project.basdir}/src/main/resources/assembly/package-xmlmake assemblypackagesingleIII。程序集文件${project.build.finalName}tar。gz${project.basdir}/src/main/resources/configconfigunix˂!

一、相关依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

二、项目构建包

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

  执行mvn package可自动生成一个独立可执行的jar文件

 <build>
        <finalName>assembly</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
            <plugin>
                <!--主要使用的是maven提供的assembly插件完成-->
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <!--具体的配置文件-->
                            <descriptors>${project.basedir}/src/main/resources/assembly/package.xml</descriptors>
                        </configuration>
                        <id>make-assembly</id>
                        <!--绑定到maven操作类型上-->
                        <phase>package</phase>
                        <!--运行一次-->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

三、assembly 文件

<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <!--打包名称,唯一标识-->
    <id>${project.build.finalName}</id>
    <!--打包格式,可以手动修改-->
    <formats>
        <format>tar.gz</format>
    </formats>
    <!--文件设置-->
    <fileSets>
        <fileSet>
            <!--目标目录,会处理目录里面的所有文件-->
            <directory>${project.basedir}/src/main/resources/config</directory>
            <!--相对于打包后的目录-->
            <outputDirectory>config</outputDirectory>
            <!--文件过滤-->
            <includes>
                <include>*.*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/src/main/resources/script</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.*</include>
            </includes>
            <!--文件权限-->
            <fileMode>0755</fileMode>
            <!--如果是脚本,一定要改为unix.如果是在windows上面编码,会出现dos编写问题-->
            <lineEnding>unix</lineEnding>
        </fileSet>
    </fileSets>
    <files>
        <!--包含打包后的jar文件,可以不加入<outputDirectory/>,默认打包的目录-->
        <file>
            <source>${project.build.directory}/${project.build.finalName}.jar</source>
        </file>
        <!--这种方式也可以进行文件处理,但是针对单文件-->
       <!-- <file>
            <source>${project.basedir}/src/main/resources/script/start.sh</source>
            <fileMode>0755</fileMode>
            <lineEnding>unix</lineEnding>
        </file>-->
    </files>
</assembly>

  备注:具体的参数的意义可以参考官网:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 

  Maven程序集插件依赖于提供的程序集描述符来指示其执行。尽管已经有预制的描述符可供使用,但它们只能满足一些常见的装配要求。

  因此,为了自定义程序集插件创建程序集的方式,您需要知道如何使用程序集描述符。

  该描述符指定要创建的程序集档案的类型,程序集的内容,以及将依赖项或其模块与程序集捆绑在一起的方式。

  在使用springboot框架的时候,存在一个问题。就是我们配置yaml文件,需要单独提出来做参数修改。当然这个是可以通过spring.profiles.active的方式来配置dev,prod等环境的激活。但是我们如果存在环境不确定,或者需要启动脚本,启动项目的时候,这样通过jar的方式后续会处理很多工作。所以前期的集成工作还是很有必要的。

免责声明:文章转载自《springboot-使用assembly进行项目打包》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java 实现折半查找Linux基础命令:netstat下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot启动过程中涉及到了扩展接口

SpringApplicationRunListener接口 1、ApplicationListener接口 是ApplicationContext的事件监听器 2、EnvironmentPostProcessor接口 上下文环境后置处理器,事件中调用 3、PropertySourceLoader接口 自定义配置文件加载器,自己解析配置文件属性...

SpringBoot进阶

  没想到把,SpringBoot虽然简化了开发流程,但要学的东西还有很多 1. Spring Boot简介 采用约定大于配置,简化Spring开发步骤与复杂的部署流程 快速创立可独立运行的Spring项目以及集成主流框架 嵌入式Servlet容器,无需打war包 starter自动依赖与版本控制 大量的自动配置,可修改默认值 需要xml,无代码生成,...

spring cloud fegin 原理解析

一、 SpringCloud 中 Feign 核心原理 如果不了解 SpringCloud 中 Feign 核心原理,不会真正的了解 SpringCloud 的性能优化和配置优化,也就不可能做到真正掌握 SpringCloud。 本章从Feign 远程调用的重要组件开始,图文并茂的介绍 Feigh 远程调用的执行流程、Feign 本地 JDK Proxy...

RocketMQ学习分享

消息队列的流派   什么是 MQ Message Queue(MQ),消息队列中间件。很多人都说:MQ 通过将消息的发送和接收分离来实现应用程序的异步和解偶,这个给人的直觉是——MQ 是异步的,用来解耦的,但是这个只是 MQ 的效果而不是目的。MQ 真正的目的是为了通讯,屏蔽底层复杂的通讯协议,定义了一套应用层的、更加简单的通讯协议。一个分布式系统中两个模...

mybatis的知识点总结

1.接口绑定:两种方法,基于注解或者基于xml文档mapper,但要注意mapper的namespace要与接口路径完全一致。 2.orm格式转换:通过设置resultMap和ResultType,将数据库中的记录转换为代码的bean对象。得到list或者对象。 3.通过parameterType接收参数,进行动态sql生成。运用ognl表达式 4.走缓存...

WebSocket 详解教程

目录   概述  WebSocket 客户端  WebSocket 服务端  WebSocket 代理  FAQ  完整示例  资料 概述 WebSocket 是什么? WebSocket 是一种网络通信协议。RFC6455 定义了它的通信标准。 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议。 为什么...