此种方式可避免resource节点对compile阶段的影响,compile阶段会读取resource节点的信息但是不会读取assembly的配置文件

1. pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <groupId>com.skd</groupId>
<artifactId>client</artifactId>
<version>1.0</version>
<name>client</name>
<description>client for file monitor</description> <properties>
<encoding>UTF-8</encoding>
<maven-compiler-plugin-version>3.8.0</maven-compiler-plugin-version>
<maven-jar-plugin-version>3.1.0</maven-jar-plugin-version>
<maven-source-plugin-version>3.0.1</maven-source-plugin-version>
<maven-assembly-plugin-version>3.1.0</maven-assembly-plugin-version>
<maven-dependency-plugin-version>3.1.0</maven-dependency-plugin-version>
<maven-resources-plugin-version>3.1.0</maven-resources-plugin-version>
</properties> <dependencies>
<!--spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--log4j2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!--http client-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency> <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!--json-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.2</version>
<classifier>jdk15</classifier>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--configuration-->
<!-- 通过资源文件注入属性配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<!-- 生成的项目压缩包的名字-->
<finalName>client</finalName>
<!--源代码路径-->
<sourceDirectory>src/main/java</sourceDirectory>
<!--maven-resources-plugin 插件打包resource文件时会参考此节点的配置--> <plugins>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin-version}</version>
<configuration>
<encoding>${encoding}</encoding>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin> <!--将项目的源代码的class文件打包到一个jar包-->
<!--jar包默认在target目录下-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin-version}</version>
<configuration>
<archive>
<!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
<addMavenDescriptor>true</addMavenDescriptor>
<manifest>
<!-- 是否要把第三方jar放到manifest的classpath中 -->
<addClasspath>true</addClasspath>
<!-- 生成的manifest中classpath的前缀,填写依赖jar包相对于项目jar包的路径-->
<!--我会把项目的jar包也打到lib目录下,所以这里使用当前目录-->
<classpathPrefix>./</classpathPrefix>
<!-- 应用的main class -->
<mainClass>com.skd.client.ClientApplication</mainClass>
</manifest>
<!--将资源文件目录添加到classpath中,打包后运行项目时则会在该目录下加载配置文件-->
<manifestEntries>
<!--填写配置文件相对于项目jar包的路径-->
<!--我的项目jar包在lib目录下,配置文件在和lib同级的conf目录下-->
<Class-Path>../conf/</Class-Path>
</manifestEntries>
</archive>
<!--项目打包为jar包时排除这些文件,如果将配置文件打到jar包,则会优先读取jar包中的配置文件,不会读取conf目录下的配置文件-->
<!--注意这玩意从编译结果目录开始算目录结构-->
<excludes>
<exclude>/*.yaml</exclude>
<exclude>/*.yml</exclude>
<exclude>/*.xml</exclude>
</excludes>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin-version}</version> <configuration>
<!--jar包名字是否在finalName后追加AssemblyId-->
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<!--xml文件中配置了打包的相关配置-->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<!--名字任意-->
<id>make-assembly</id>
<!-- 绑定到package生命周期阶段上 -->
<phase>package</phase>
<goals>
<!-- 只运行一次 -->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </build> </project>

2. assembly的xml配置文件

<assembly >
<!--此处是打包名称的设置,最后是会生成一个finalName-id.format 文件在目录 target下-->
<!--appendAssemblyId为false时则压缩包名称不追加id,默认下追加-->
<id>assembly</id>
<!-- 最终打包成一个用于发布的zip文件 -->
<formats>
<format>tar.gz</format>
</formats> <dependencySets>
<dependencySet>
<!--是否将项目jar包打包到指定目录-->
<useProjectArtifact>true</useProjectArtifact>
<!--将依赖jar包打到lib目录-->
<outputDirectory>lib</outputDirectory>
<!--将依赖jar包不解压直接打包到目录-->
<unpack>false</unpack>
</dependencySet>
</dependencySets> <fileSets>
<!--通过fileSet节点可以将制定目录的指定文件打包到压缩文件的制定目录-->
<fileSet>
<!-- 把项目的配置文件,打包到压缩文件的conf目录 -->
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<!--打包脚本文件到根目录-->
<fileSet>
<directory>${project.basedir}/src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet> </fileSets>
</assembly>

3. 打包后的 target 目录结构

解压后目录结构如下:

项目jar包在lib目中中

4. 运行项目

启动脚本

执行启动脚本

使用 maven-assembly-plugin 打包项目的更多相关文章

  1. 记录一次maven打包时将test目录下的类打包到jar中,Maven Assembly Plugin的使用

    今天有人问我打包后找不到主类,运行的类写在test中.按照常规,test目录下的文件不会打包到jar包中.(但是我测试一个springboot工程就可以,这里之后再研究) 具体解决如下 第一步:在po ...

  2. maven assembly plugin使用

    使用场景 在使用maven来管理项目时,项目除了web项目,还有可能为控制台程序,一般用于开发一些后台服务的程序.最近在工作中也遇到了这种场景,使用quartz开发一个任务调度程序.程序中依赖很多ja ...

  3. 使用Maven Assembly plugin将依赖打包进jar

    一个Eclipse的工程,在pom中配置了若干依赖,需要将pom中所有的依赖全部打包进一个jar包中,可以选择的方案有maven-assembly-plugin和fatjar.以前采用fatjar进行 ...

  4. java工程打成jar包 - 使用maven assembly插件打包及手动打包

    在java工程打包的过程中遇到过不少问题,现在总结一下.一种是典型的maven工程打包,依赖的jar包全都在pom.xml中指定,这种方式打包很方便:另一种是依赖了本机jar包(不能通过pom.xml ...

  5. maven mvn package 打包项目时,出现错误导致失败的解决方法

    解决思路:看报错时在maven打包过程中的哪一步,然后看报错内容,解决报错内容即可,如果是实在不好解决的部分,看看能不能设置不检测,能打包出来就行. 这里是因为mybatis逆向工程插件出现异常所以中 ...

  6. Maven Assembly插件介绍

    转自:http://blueram.iteye.com/blog/1684070 已经写得挺好的,就不用重写了. 你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembl ...

  7. maven assembly 配置详解

    Maven Assembly插件介绍 博客分类: 项目构建   你是否想要创建一个包含脚本.配置文件以及所有运行时所依赖的元素(jar)Assembly插件能帮你构建一个完整的发布包. Assembl ...

  8. maven学习(4)-本地项目打包发布到私有仓库

    发布本地项目到私服仓库 在前面章节有介绍maven发布本地jar包到私服仓库,这里详细介绍一下步骤. 在项目开发中通常会引用其他的jar,怎样把自己的项目做为一个jar包的形式发布到私服仓库中,主要有 ...

  9. Maven Assembly打包提示[WARNING] transitive dependencies if any will not be available

    maven assembly打包出现错误 [WARNING] The POM for com.flink.xxr:0.0.1-SNAPSHOT is invalid, transitive depen ...

  10. eclispe中使用 maven build启动maven项目和打包项目

    1.右键项目2.点击run as按钮 3.点击run configurations 4.配置如下: =============================加油加油加油加油加油加油========= ...

随机推荐

  1. document.getElementById(“id”)与$("#id")的区别

    document.getElementById("id")可以直接获取当前对象, jQuery利用$("#id")获取的是一个[object Object],需 ...

  2. 中间件之Kafka

    (一)kafka简介 Kafka/Jafka 高性能跨语言的分布式发布/订阅消息系统,数据持久化,全分布式,同时支持在线和离线处理. 1.1 kafka设计目标 高吞吐率 在廉价的商用机器上单机可支持 ...

  3. FortiGate上架前准备

    1.收集信息 1.网络拓扑信息(了解网络拓扑信息有助于网络方案的规划) 2.环境信息(了解部署位置.部署模式.最大吞吐.最大用户数有助于对设备性能的评估) 3.客户需求,对FortiGate部署的功能 ...

  4. vue修改端口号

    vue修改端口号   默认端口号 修改端口号 重新启动项目 npm run dev        

  5. 面试01:解释内存中的栈(stack)、堆(heap)和方法区(method area)的用法

    栈的使用:通常我们定义一个基本数据类型的变量,一个对象的引用,还有就是函数调用的现场保存都使用JVM中的栈空间. 队的使用:通过new关键字和构造器创建的对象则放在堆空间,堆是垃圾收集器管理的主要区域 ...

  6. 特殊篮子问题——C语言暴力破解

    You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of th ...

  7. CSS三种插入样式表格式

    首先简单理解浏览器与网页的交互过程 CSS样式表(stylesheet) cascading style sheet 级联样式表 表现HTML或XHTML文件样式的计算机语言:包含对字体.颜色.边距. ...

  8. SSM三大框架整合

    三大框架整合的思路 1.Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头.文件必须存在. applicationContext-dao.xml: ...

  9. ABP框架系列之三十七:(Navigation-导航)

    Every web application has some menu to navigate between pages/screens. ASP.NET Boilerplate provides ...

  10. PC平台在Unity3D中播放硬盘ogg,mp3,wav文件

    Unity3D PC平台本身是支持直接用www读取本地ogg,wav的,但是并不能读取byte[],字节数组格式,这对用习惯了bass,fmod的人来说有点不方便. 搜了一圈发现了一个C#的音频库叫N ...