maven项目常用的打包方式

article/2025/10/10 23:38:46

一、无依赖其他任何jar

<build>  <plugins>  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-jar-plugin</artifactId>  <version>2.4</version>  <configuration>  <archive>  <manifest>  <addClasspath>true</addClasspath>  <classpathPrefix>lib/</classpathPrefix>  <mainClass>com.think.TestMain</mainClass>  </manifest>  </archive>  </configuration>  </plugin>  </plugins>  </build>  

运行:mvn clean package,在target中找到打包出来的,命令后运行java -jar xxx.jar即可,但是如果程序有依赖其他包,比如程序依赖jdbc去查询db,这时候再执行就会出现找不到jdbc依赖,因为我们并没有将依赖包打进去

二、解决依赖其他包时,可执行jar的打包

1、

<build>  <plugins>  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-assembly-plugin</artifactId>  <version>2.3</version>  <configuration>  <appendAssemblyId>false</appendAssemblyId>  <descriptorRefs>  <descriptorRef>jar-with-dependencies</descriptorRef>  </descriptorRefs>  <archive>  <manifest>  <mainClass>com.think.TestMain</mainClass>  </manifest>  </archive>  </configuration>  <executions>  <execution>  <id>make-assembly</id>  <phase>package</phase>  <goals>  <goal>assembly</goal>  </goals>  </execution>  </executions>  </plugin>  </plugins>  </build>  

但以上方式用的比较少,因为我们依赖的jar,也会打进到我们最终生成的jar,这样不太好,假如你生成的jar要给别人使用,最好给一个纯净的。
一般用assembly会再用他另外一个功能,将我们的jar归档,打包成一个zip

2、打成一个zip包,发布项目的时候,将zip包copy到服务器上,直接unzip xxx.zip,里面包含要运行到jar以及依赖的lib,还有配置的config文件,即可直接启动服务

<build>  <resources>  <!-- 控制资源文件的拷贝 -->  <resource>  <directory>src/main/resources</directory>  <targetPath>${project.build.directory}/classes</targetPath>  </resource>  </resources>  <plugins>  <!-- 设置源文件编码方式 -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-compiler-plugin</artifactId>  <configuration>  <source>1.6</source>  <target>1.6</target>  <encoding>UTF-8</encoding>  </configuration>  </plugin>  <!-- The configuration of maven-jar-plugin -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-jar-plugin</artifactId>  <version>2.4</version>  <!-- The configuration of the plugin -->  <configuration>  <!-- Configuration of the archiver -->  <archive>  <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->  <addMavenDescriptor>false</addMavenDescriptor>  <!-- Manifest specific configuration -->  <manifest>  <!-- 是否要把第三方jar放到manifest的classpath中 -->  <addClasspath>true</addClasspath>  <!-- 生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/ -->  <classpathPrefix>lib/</classpathPrefix>  <!-- 应用的main class -->  <mainClass>com.think.TestMain</mainClass>  </manifest>  </archive>  <!-- 过滤掉不希望包含在jar中的文件 -->  <!--<excludes>-->  <!--<exclude>${project.basedir}/xml/*</exclude>-->  <!--</excludes>-->  </configuration>  </plugin>  <!-- The configuration of maven-assembly-plugin -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-assembly-plugin</artifactId>  <version>2.4</version>  <!-- The configuration of the plugin -->  <configuration>  <!-- Specifies the configuration file of the assembly plugin -->  <descriptors>  <descriptor>src/main/assembly/assembly.xml</descriptor>  </descriptors>  </configuration>  <executions>  <execution>  <id>make-assembly</id>  <phase>package</phase>  <goals>  <goal>single</goal>  </goals>  </execution>  </executions>  </plugin>  </plugins>  </build>  

还有一个重要的文件,名字可以随便起,我上面用的是 src/main/assembly/assembly.xml

<assembly>  <id>bin</id>  <includeBaseDirectory>false</includeBaseDirectory>  <!-- 最终打包成一个用于发布的zip文件 -->  <formats>  <format>zip</format>  </formats>  <!-- Adds dependencies to zip package under lib directory -->  <dependencySets>  <dependencySet>  <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->  <useProjectArtifact>false</useProjectArtifact>  <!--<outputDirectory>lib</outputDirectory>-->  <unpack>false</unpack>  </dependencySet>  </dependencySets>  <fileSets>  <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->  <!--<fileSet>-->  <!--<directory>${project.basedir}</directory>-->  <!--<outputDirectory>/</outputDirectory>-->  <!--</fileSet>-->  <!-- 把项目的配置文件,打包进zip文件的config目录 -->  <fileSet>  <directory>${deploy.dir}/classes/</directory>  <outputDirectory>/conf</outputDirectory>  <includes>  <include>*.xml</include>  <include>*.properties</include>  </includes>  </fileSet>  <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->  <fileSet>  <directory>${project.build.directory}</directory>  <outputDirectory></outputDirectory>  <includes>  <include>*.jar</include>  </includes>  </fileSet>  </fileSets>  
</assembly>  

最终执行命令:mvn clean package,出来的是这样的
这里写图片描述
这里写图片描述
解压zip包,我们看到我们想要的,good

3、还有一种打包方式,上面相当于把我们想要的东西打成一个zip包,全部放到一起,看着整洁好看,但有点繁琐,我们其实可以用另外一个插件来完成,不打包,即看到上图解压后的文件

[html] view plain copy
<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>  <groupId>com.test</groupId>  <artifactId>myTestJar</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>jar</packaging>  <name>myTestJar</name>  <url>http://maven.apache.org</url>  <properties>  <deploy.dir>./target/</deploy.dir>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>  <dependency>  <groupId>junit</groupId>  <artifactId>junit</artifactId>  <version>3.8.1</version>  <scope>test</scope>  </dependency>  <dependency>  <groupId>com.alibaba</groupId>  <artifactId>druid</artifactId>  <version>1.0.9</version>  </dependency>  <dependency>  <groupId>mysql</groupId>  <artifactId>mysql-connector-java</artifactId>  <version>5.1.32</version>  </dependency>  </dependencies>  <build>  <finalName>myTest</finalName>  <sourceDirectory>src/main/java</sourceDirectory>  <resources>  <!-- 控制资源文件的拷贝 -->  <resource>  <directory>src/main/resources</directory>  <targetPath>${project.build.directory}</targetPath>  </resource>  </resources>  <plugins>  <!-- 设置源文件编码方式 -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-compiler-plugin</artifactId>  <configuration>  <defaultLibBundleDir>lib</defaultLibBundleDir>  <source>1.6</source>  <target>1.6</target>  <encoding>UTF-8</encoding>  </configuration>  </plugin>  <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-jar-plugin</artifactId>  <configuration>  <archive>  <manifest>  <addClasspath>true</addClasspath>  <classpathPrefix>lib/</classpathPrefix>  <mainClass>com.think.TestMain</mainClass>  </manifest>  </archive>  </configuration>  </plugin>  <!-- 拷贝依赖的jar包到lib目录 -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-dependency-plugin</artifactId>  <executions>  <execution>  <id>copy</id>  <phase>package</phase>  <goals>  <goal>copy-dependencies</goal>  </goals>  <configuration>  <!-- ${project.build.directory}是maven变量,内置的,表示target目录,如果不写,将在跟目录下创建/lib -->  <outputDirectory>${project.build.directory}/lib</outputDirectory>  <!-- excludeTransitive:是否不包含间接依赖包,比如我们依赖A,但是A又依赖了B,我们是否也要把B打进去 默认不打-->  <excludeTransitive>true</excludeTransitive>  <!-- 复制的jar文件去掉版本信息 -->  <stripVersion>true</stripVersion>  </configuration>  </execution>  </executions>  </plugin>  <!-- 解决资源文件的编码问题 -->  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-resources-plugin</artifactId>  <version>2.3</version>  <configuration>  <encoding>UTF-8</encoding>  </configuration>  </plugin>  <!-- 打包source文件为jar文件 -->  <plugin>  <artifactId>maven-source-plugin</artifactId>  <version>2.1</version>  <configuration>  <attach>true</attach>  <encoding>UTF-8</encoding>  </configuration>  <executions>  <execution>  <phase>compile</phase>  <goals>  <goal>jar</goal>  </goals>  </execution>  </executions>  </plugin>  </plugins>  </build>  </project>  

这里采用的是maven-dependency-plugin插件,进行资源的copy。
4、

<build>  <resources>  <resource>  <targetPath>${project.build.directory}/classes</targetPath>  <directory>src/main/resources</directory>  <filtering>true</filtering>  <includes>  <include>**/*.xml</include>  </includes>  </resource>  </resources>  <plugins>  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-compiler-plugin</artifactId>  <version>3.0</version>  <configuration>  <source>1.6</source>  <target>1.6</target>  <encoding>UTF-8</encoding>  </configuration>  </plugin>  <plugin>  <groupId>org.apache.maven.plugins</groupId>  <artifactId>maven-shade-plugin</artifactId>  <version>2.0</version>  <executions>  <execution>  <phase>package</phase>  <goals>  <goal>shade</goal>  </goals>  <configuration>  <transformers>  <transformer  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  <mainClass>com.think.TestMain</mainClass>  </transformer>  <!--<transformer-->  <!--implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">-->  <!--<resource>applicationContext.xml</resource>-->  <!--</transformer>-->  </transformers>  <shadedArtifactAttached>true</shadedArtifactAttached>  <shadedClassifierName>executable</shadedClassifierName>  </configuration>  </execution>  </executions>  </plugin>  </plugins>  </build>  

这种方式打出来是柔和到一起,成为一个jar,

这里写图片描述
可以直接java -jar xxx.jar运行。

我们可以根据不同需要来打包,如果暴露给外面,可以采用第4种,如果是自己公司项目打包,建议2,3种,因为有时候只是改了个配置文件,不需要打包,直接把配置文件复制进去即可


http://chatgpt.dhexx.cn/article/c1xIPP7U.shtml

相关文章

IDEA中MAVEN项目如何打包JAR包

这篇文章主要为大家展示了“IDEA中MAVEN项目如何打包JAR包”&#xff0c;内容简而易懂&#xff0c;条理清晰&#xff0c;希望能够帮助大家解决疑惑&#xff0c;下面让小编带领大家一起研究并学习一下“IDEA中MAVEN项目如何打包JAR包”这篇文章吧。 Idea中为一般的非Web项目打J…

Maven项目打包为jar的几种方式

Maven项目打包为jar的几种方式 这里收集整理下以往打包MAVEN项目为JAR包的各种方式 ##直接打包&#xff0c;不打包依赖包 直接打包&#xff0c;不打包依赖包&#xff0c;仅打包出项目中的代码到JAR包中。在POM中添加如下plugin即可&#xff0c;随后执行maven install <plu…

mac电脑如何图片压缩优化,附教程

mac电脑如何将图片无损压缩且优化&#xff1f;今天小编给大家带来一款软件ImageOptim这款小巧免费且开源的Mac 图片无损压缩优化工具。如何工作下面给大家介绍ImageOptim for mac图片压缩优化教程。 标题ImageOptim图片压缩优化软件使用教程 当你拖放图片到ImageOptim的窗口&…

Image augmentation library in Python-Augmentor使用心得

Augmentor是个增强图像训练数据的库&#xff0c;减少了使用图像库自己编写代码的繁杂工序&#xff0c;能够批量完成图像的旋转&#xff0c;放大&#xff0c;缩小&#xff0c;添加噪音以扩充数据量。接下来结合官方文档介绍下这个库和使用心得。 首先github&#xff1a;https:/…

9.1_image-augmentation

9.1 图像增广 在5.6节&#xff08;深度卷积神经网络&#xff09;里我们提到过&#xff0c;大规模数据集是成功应用深度神经网络的前提。图像增广&#xff08;image augmentation&#xff09;技术通过对训练图像做一系列随机改变&#xff0c;来产生相似但又不同的训练样本&…

ObjectFormer for Image Manipulation Detection and Localization

paper&#xff1a;https://arxiv.org/abs/2203.14681 Abstract 近年来图像编辑技术的发展对多媒体数据的可信度提出了严峻的挑战&#xff0c;这推动了图像篡改检测的研究。在本文中&#xff0c;我们提出了ObjectFormer来检测和定位图像操作。为了捕捉在RGB域中不可见的细微操…

Multimodal Co-Attention Transformer for Survival Prediction in Gigapixel Whole Slide Images

用于千兆像素全切片病理图像生存预测的多模态下跨模态注意机制的transformer 概念&#xff1a; premutation invariance(置换不变&#xff09;&#xff1a;指输入顺序的改变不会影响输出值 permutation-equivariant(置换等边&#xff09;&#xff1a;与premutation invariance …

【Image Captioning】Improve Image Captioning by Estimating the Gazing Patterns from the Caption

Improve Image Captioning by Estimating the Gazing Patterns from the Caption Department of Computer Science, The George Washington University, CVPR 2022 Abstract CNN等神经网络模型中提取的图像特征中产生类人描述方面达到了良好的性能。然而&#xff0c;之前没有…

Image_captioning

Image caption——图像理解 文章目录 Image caption——图像理解1、任务综述以及早期做法2、从Encoder-Decoder结构谈起&#xff08;前身&#xff09;3、Show and Tell: A Neural Image Caption Generator&#xff08;image caption 开山之作&#xff09; 1、任务综述以及早期做…

From Image to Imuge: Immunized Image Generation

From Image to Imuge: Immunized Image Generation Brief Introduction of myselfIntroduction of this workRelated WorksImage Self-EmbeddingImage Reconstruction (Inpainting) MethodExperimentsConclusion and Contact Brief Introduction of myself 大家好&#xff0c;…

PhotometricStereo

PhotometricStereo 小强老师留下了道CMU的习题&#xff0c;见此题十分有趣&#xff0c;而且博客许久不曾更新&#xff0c;特把该作业奉上。作业是CV中的Photometric Stereo方面的&#xff0c;基本原理还是视觉领域的复原建模&#xff0c;详见这篇论文&#xff1a; Aaron Hertzm…

Free-Form Image Inpainting with Gated Convolution

CVPR 2020的一篇文章 解读一下题目&#xff1a; - free form 自由形式 - image inpainting 图像补全 - gated convolution 门控卷积该文章提出了一个&#xff0c;缺失区域为自由形式的图像补全情况的解决方法&#xff0c;针对该方法提出了一个门控卷积的方法。 模型结构图…

Deep Image Prior

Deep Image Prior 摘要 深度卷积网络已经成为图像生成和重建的常用工具。人们猜想&#xff0c;他们优秀的表现是归功于他们能够从大量图像样本中学习到真实图像先验的能力。而相反&#xff0c;本文中作者展示生成网络在经过任何学习之前就能够捕获大量的低级图像统计信息&…

Automatic Trimap Generation for Image Matting

Automatic Trimap Generation for Image Matting翻译 Abstract image matting 是计算摄影中的一个重要问题。虽然已经研究了二十多年&#xff0c;但是开发一种不需要人工干预的自动matting算法仍然是一个挑战。大多数最先进的matting算法都需要人工干预&#xff0c;以trimap或涂…

Self-augmented Unpaired Image Dehazing via Density and Depth Decomposition程序运行记录

论文题目&#xff1a; 《Self-augmented Unpaired Image Dehazing via Density and Depth Decomposition》 作者列表&#xff1a; 杨洋(天津大学)&#xff0c;王超岳&#xff08;悉尼大学&#xff09;&#xff0c;刘日升&#xff08;大连理工大学&#xff09;&#xff0c;张…

Learning Continuous Image Representation with Local Implicit Image Function

1. 简介 文章简称为LIIF&#xff0c;即Local Implicit Image Function&#xff0c;受到了3D视觉里场景或物体的隐式表示思想的启发&#xff0c;主要的想法便是把正常的图像当做是连续的&#xff0c;用神经网络表示这个图像之后&#xff0c;对于图像上的每一个像素点其实就当做…

【InternImage】Exploring Large-Scale Vision Foundation Models with Deformable Convolutions

基于Deformable Conv的大规模基础模型 特点&#xff1a; 采用Deformable Conv V3 【v2 19论文】CNN模型 背景 大规模的ViT模型借助模型能力在超大规模数据集上取得了非常高的性能&#xff0c;然而大规模CNN模型研究才刚刚开始。 近期CNN研究倾向于使用大的卷积kernel&#…

ImageMol

ai圈也是卷的狠啊~~ 又发现一个全球首个分子图像自监督学习框架来也 分子是维持物质化学稳定性的最小单位。对分子的研究&#xff0c;是药学、材料学、生物学、化学等众多科学领域的基础性问题。 分子的表征学习&#xff08;Molecular Representation Learning&#xff09;是…

Morphological image processing

目录 基本思想&#xff1a; 一.集合论基础 1.集合的概念 2.集合间的关系和运算 二.二值形态学的基本运算 1.腐蚀 &#xff08;1&#xff09;概念 &#xff08;2&#xff09;结构元素形状对腐蚀运算结果的影响 &#xff08;3&#xff09;腐蚀运算在物体识别中的应用 2…

实用的图片批量压缩优化工具

ImageOptim ImageOptim 支持 PNG/JPEG/GIF 动画&#xff0c;本质是各种影像优化工具的图形前端&#xff1a;AdvPNG、OptiPNG、Pngcrush、JpegOptim、jpegtran、Gifsicle 和 PNGOUT素材。 ImageOptim软件非常小&#xff01; 2.6M而已ImageOptim操作非常非常非常快捷&#xff01…