此种方式可避免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. http://www.36dsj.com/archives/46131

    https://docs.growingio.com/Developer%20Document.html https://youdata.163.com/dash/39706/editor?pid=7 ...

  2. P1147连续自然数和-(尺取法)

    https://www.luogu.org/problemnew/show/P1147 题意:输入一个n,求连续几个数加起来等于n,输出这几个连续的数的第一个和最后一个.10<=n<=20 ...

  3. Handler实现消息的定时发送

    话不多说,直接上代码 private Handler mHandler = new Handler() { @Override public void handleMessage(Message ms ...

  4. Python学习笔记-常用内置函数

    输出:print() 功能:输出打印 语法:print(*objects, sep=' ', end='\n', file=sys.stdout) 参数:objects----复数,表示可以一次输出多 ...

  5. java_20 LinkedList类

    LinkedList类特有的方法 (1)addLast()  将指定元素添加到此列表的结尾. addFirst() 将指定元素添加到此列表的开始. public static void main(St ...

  6. React-router4 第七篇 Recursive Paths 递归路径

    https://reacttraining.com/react-router/web/example/recursive-paths import React from 'react' import ...

  7. BeanUtils.copyProperties的简单示例

    一.新建测试实体 1.UserA package com.dechy.hebswj.test; public class UserA { private String a; private Strin ...

  8. WPF 凭证分录控件

    凭证分录编辑控件,效果如下: 源码:https://gitee.com/orchis/VoucherGridCtl.git

  9. Office365 Manager Plus之报表

    Office365 Manager Plus之报表 也许您刚刚开始使用Office 365,对它的各个组件还有很多疑问,如何快速掌握Office 365各种服务的用法?如何管理邮箱?如何监控邮件流量? ...

  10. Unity3D中播放视频的方法

    播放视频其实和贴图非常相像,因为播放视频用到的 MovieTexture 属于贴图 Texture 的子类.Unity3D 支持的视频格式有很多,但是还是建议使用 ogv 格式的视频,使用其他格式依然 ...