前言:本文将告诉你如何将程序Jar与与依赖Jar及配置文件分离打包,以下列举了两种不同Maven打包方式,其打包效果一致!

一、第一种Maven打包方式,将jar及resources下全部配置文件,拷贝到指定目录:

<!--配置项-->
<properties>
<!--自定义配置-->
<project.jar.output.directory>E:/IDEAFile/file-copy/target/project</project.jar.output.directory>
</properties>
<build>
<plugins>
<!--项目依赖的jar文件,放置默认配置目录下-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> <!-- 设置jar的入口类 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.filecopy.FileCopyApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin> <!-- 使用maven-resources-plugin插件复制resources目录下所有文件到指定的路径-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/project</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin> <!--使用maven-antrun-plugin插件将jar复制到指定的目录下-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<!-- 在maven进行package的时候执行-->
<phase>package</phase>
<configuration>
<tasks>
<!--todir:是将要复制jar包到的地方,overwrite:是否重写-->
<copy todir="${project.jar.output.directory}" overwrite="true">
<!--获取父目录下的target文件夹中的jar-->
<fileset dir="${project.build.directory}">
<include name="*.jar"/>
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

第二种Maven打包方式使用 assembly插件,将jar及配置文件进行压缩打包到指定目录:

<plugins>
<!-- 项目依赖的jar文件,放置默认配置目录下-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> <!-- 设置jar的入口类-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.example.filecopy.FileCopyApplication</mainClass>
</manifest>
</archive>
</configuration>
</plugin> <!--assembly插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<!--指定压缩包名称-->
<finalName>project</finalName>
<!--指定assembly配置文件配置-->
<descriptors>
<descriptor>/assembly/assembly.xml</descriptor>
</descriptors>
<!--打包tar.gz输出target文件夹中-->
<outputDirectory>${project.build.directory}</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

assembly文件:

<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>leaves</id>
<formats>
<!--压缩文件形式 可选 zip tar.gz等 -->
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory> <!-- 项目文件处理 -->
<fileSets>
<!--配置文件输出位置根目录文件夹下-->
<fileSet>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**</include>
</includes>
<filtered>true</filtered>
<outputDirectory>${file.separator}</outputDirectory>
</fileSet> <!-- 项目代码生成的jar文件放在根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>${file.separator}</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>

个人总结:

我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!

Spring Boot使用Maven自定义打包方式的更多相关文章

  1. Spring Boot使用Maven打包替换资源文件占位符

    在Spring Boot开发中,通过Maven构建项目依赖是一件比较舒心的事,可以为我们省去处理冲突等大部分问题,将更多的精力用于业务功能上.近期在项目中,由于项目集成了其他外部系统资源文件,需要根据 ...

  2. Spring Boot 的Maven多模块开发web项目使用外部容器进行部署

    Spring Boot中自带有Tomcat容器,因此Spring Boot项目只需要运行main函数,就可以运行,但是以往的web项目,我们习惯于使用自己安装的Tomcat运行或者使用Tomcat.J ...

  3. 运行 Spring Boot 应用的 3 种方式

    今天介绍 3 种运行 Spring Boot 应用的方式,看大家用过几种? 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spring Boot 开启的 ...

  4. Spring Boot的Maven插件Spring Boot Maven plugin详解

    Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...

  5. Spring boot 自动配置自定义配置文件

    示例如下: 1.   新建 Maven 项目 properties 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

  6. Spring Boot 支持多种外部配置方式

    Spring Boot 支持多种外部配置方式 http://blog.csdn.net/isea533/article/details/50281151 这些方式优先级如下: 命令行参数 来自java ...

  7. spring boot / cloud (四) 自定义线程池以及异步处理@Async

    spring boot / cloud (四) 自定义线程池以及异步处理@Async 前言 什么是线程池? 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线 ...

  8. Spring boot项目maven的profile多环境配置不自动替换变量的问题解决

    Spring boot项目maven的profile多环境配置不自动替换变量的问题解决   在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring b ...

  9. spring boot项目能启动打包失败

    如题,项目本地可以正常启动.但是用 mvn clean package打包就失败! 事件原因如下: 一.pom.xml少packing元素 <groupId>com.sanyi</g ...

随机推荐

  1. ERP制造模块操作与设计--开源软件诞生30

    赤龙ERP制造模块讲解--第30篇 用日志记录"开源软件"的诞生 [进入地址 点亮星星]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redra ...

  2. mysql反序索引

    关于mysql的排序 参考:https://www.cnblogs.com/lccsblog/p/12733773.html 转载: https://www.cnblogs.com/lynn919/p ...

  3. 新手上路之JDK8的下载、安装与PATH环境变量的配置

    有些东西不常用总是会忘记,所以想把它写下来,方便以后自己想用的时候找得到:同时也进一步加深自己的记忆.接触JAVA的时间不长,言语或内容有不当之处,欢迎大佬们指正. 每一个学习JAVA的人都会经历的过 ...

  4. 解决 spring-integration-mqtt 频繁报 Lost connection 错误

    问题描述 在之前的博客介绍了如何在 Spring Boot 集成 MQTT,后面使用中没有发现问题,最近发现一直报错: Lost connection: Connection lost; retryi ...

  5. Python中format_map与format字符串格式化的区别

    在Python3中使用format和format_map方法都可以进行字符串格式化,但format是一种所有情况都能使用的格式化方法,format_map仅使用于字符串格式中可变数据参数来源于字典等映 ...

  6. 关于RequestParam在不同的Spring版本上,接口在controller重载时注解可能失效的踩坑记录

    先抛背景: 我项目中的Spring版本是2.0.3.RELEASE. api-demo负责暴露接口,service-demo负责实现功能.接口参数的@RequestParam和@RequestBody ...

  7. Day6【Scrum 冲刺博客】

    每日会议总结 昨天已完成的工作 方晓莹(PIPIYing) 对接住户相关接口 处理token过期重定向.页面跳转.错误状态处理等内容 方子茵(Laa-L) 暂无 黄芯悦(Sheaxx) 完善物业报修页 ...

  8. 理解java底层通讯协议

    引言: 本周自己重新对底层通讯方式进行了学习,在此做一个输出. 分别从客户端发送多个请求的需求角度与服务端接收多个连接发送请求的需求角度,剖析4种基于java自身技术实现的消息方式通讯所带来的影响,解 ...

  9. 深入理解.NET/WPF内存泄漏

    众所周知,内存管理和如何避免内存泄漏(memory leak)一直是软件开发的难题.不要说C.C++等非托管(unmanaged)语言,即使是Java..NET等托管(managed)语言,尽管有着完 ...

  10. C#实例化对象的三种方式及性能对比

    前言 做项目过程中有个需求要实例化两万个对象并添加到List中,这个过程大概需要1min才能加载完(传参较多),于是开启了代码优化之旅,再此记录. 首先想到的是可能实例化比较耗时,于是开始对每种实例化 ...