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 ...
随机推荐
- Beta冲刺随笔——测试
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 团队进行Alpha冲刺 作业正文 正文 其他参考文献 无 ...
- msql语句
表相关语句: 创表相关 CREATE TABLE `Student`( `s_id` VARCHAR(20), `s_name` VARCHAR(20) NOT NULL DEFAULT '', `s ...
- springboot:读取application.yml文件
现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...
- PyQt(Python+Qt)学习随笔:QTreeWidget中标题相关属性访问方法headerItem、setHeaderLabels
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件窗口可以有一个标题头,其中包含部件中每个列的节(即标题).QTreeWidget的标题属性包 ...
- 课堂练习之疫情APP
title: 课堂练习之疫情查询APP date: 2020-03-17 20:08:51 tags: 在之前的体温记录APP上改进,只写出疫情信息查询页面的代码. 实体类与上篇博客SSM整合中的Ci ...
- VirtualBox安装Centos出现E_FAIL (0x80004005)的解决方法
问题描述:UUID已经存在 Cannot register the hard disk 'F:\hadoop\VirtualBox-centos\centos6.4\centos6.4.vdi' {0 ...
- AcWing 369. 北大ACM队的远足
\(\text{Update on 2020.3.25}\) 我之前的做法也有问题,讨论还是不够严谨,导致又有几组(见 打卡评论区)\(\text{Hack}\) 此题数据极水,这里有几种错误写法: ...
- pyppeteer 登录一般网站 并利用 http方法获取登录页面的验证码
主函数 新建浏览器,进行登录,由于验证码的识别准确率不是百分之百,需要多次尝试. async def main(self, username, pwd, url): # 定义main协程函数, log ...
- deepstrem编译缺少gst/gst.h解决方案
Deepstream在编译程序的程序的显示缺少gst/gst.h 具体情况是Deepstream运行已编译好的deepstream-app可以正常运行,但对源码编译的时候出现以述情况,初步分析是我们安 ...
- Filebeat+Logstash自定义多索引
方案一:推荐 [root@elk-node-1 filebeat]# cat filebeat.yml|egrep -v "^$|^#|#" filebeat.inputs: - ...