Spring Boot使用Maven自定义打包方式
前言:本文将告诉你如何将程序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自定义打包方式的更多相关文章
- Spring Boot使用Maven打包替换资源文件占位符
在Spring Boot开发中,通过Maven构建项目依赖是一件比较舒心的事,可以为我们省去处理冲突等大部分问题,将更多的精力用于业务功能上.近期在项目中,由于项目集成了其他外部系统资源文件,需要根据 ...
- Spring Boot 的Maven多模块开发web项目使用外部容器进行部署
Spring Boot中自带有Tomcat容器,因此Spring Boot项目只需要运行main函数,就可以运行,但是以往的web项目,我们习惯于使用自己安装的Tomcat运行或者使用Tomcat.J ...
- 运行 Spring Boot 应用的 3 种方式
今天介绍 3 种运行 Spring Boot 应用的方式,看大家用过几种? 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spring Boot 开启的 ...
- Spring Boot的Maven插件Spring Boot Maven plugin详解
Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...
- Spring boot 自动配置自定义配置文件
示例如下: 1. 新建 Maven 项目 properties 2. pom.xml <project xmlns="http://maven.apache.org/POM/4 ...
- Spring Boot 支持多种外部配置方式
Spring Boot 支持多种外部配置方式 http://blog.csdn.net/isea533/article/details/50281151 这些方式优先级如下: 命令行参数 来自java ...
- spring boot / cloud (四) 自定义线程池以及异步处理@Async
spring boot / cloud (四) 自定义线程池以及异步处理@Async 前言 什么是线程池? 线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线 ...
- Spring boot项目maven的profile多环境配置不自动替换变量的问题解决
Spring boot项目maven的profile多环境配置不自动替换变量的问题解决 在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring b ...
- spring boot项目能启动打包失败
如题,项目本地可以正常启动.但是用 mvn clean package打包就失败! 事件原因如下: 一.pom.xml少packing元素 <groupId>com.sanyi</g ...
随机推荐
- Kafka入门之consumer--rebalance流程
重平衡(rebalance) 旧版本Kafka依托于Zk进行rebalance,新版本consumer使用了Kafka内置的一个全新的组协调协议.对于每个组而言,Kafka的某个broker会被选举为 ...
- mfc 双缓存
CRect rect; //获取显示区域大小(该值为据对坐标,使用时需转换) GetWindowRect(rect); rect.SetRect(0, 0, rect.Width(), rect.He ...
- 03Python网络编程之单线程服务端
# 对于单线程的服务端,我们借助于zen_utils(我们自己编写好的一些函数)是很容易就实现的.# 导入这个模块import zen_utilsif __name__ == '__main__': ...
- charles 常用功能(七)简易接口压力测试(repeat advance 功能)
接口请求次数.并发量.请求延迟时间均可配置 1.选中需要进行测试的接口,鼠标右键 选中[repeat advance] 设置迭代数量
- 西湖论剑2020MISC-Yusa_yyds
非常规USB流量分析 附件下载: 链接:https://pan.baidu.com/s/1Gjgj1EH9qmX0PYi21uYlDg 提取码:x9xn 先提取USB流量数据,使用工具: https: ...
- Linux用户配置文件
一,用户信息文件 /etc/passwd 1,用户管理简介 1,越是对服务器安全性要求高的服务器,越需要建立合理的用户权限等级制度和服务器操作规范 2,在Linux中主要是通过用户配置文件来查看和修改 ...
- jquery和zepto有何区别?
1.针对移动端程序,Zepto有一些基本的触摸事件可以用来做触摸屏交互(tap事件.swipe事件),Zepto是不支持IE浏览器的. 2.DOM操作的区别:添加id时jQuery不会生效而Zepto ...
- 题解-洛谷P6788 「EZEC-3」四月樱花
题面 洛谷P6788 「EZEC-3」四月樱花 给定 \(n,p\),求: \[ans=\left(\prod_{x=1}^n\prod_{y|x}\frac{y^{d(y)}}{\prod_{z|y ...
- 题解-CF1444C Team-Building
题面 CF1444C Team-Building 给 \(n\) 个点 \(m\) 条边,每个点有颜色 \(c_i(1\le c_i\le k)\),求有多少个颜色对两组点并后是二分图. 数据范围:\ ...
- CentOS 7.6安装MariaDB10.4.8超详细教程
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品. Cent ...