maven如何打包你会吗?

article/2025/10/10 23:50:19

1.新建一个maven项目,在main/java中建立Main类


public class Main {public static void main(String[] args) {System.out.println("hello java ...");}
}

 2.添加依赖,使其成为可执行包

 <build><plugins><!--打包成为可执行包--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.3.0</version><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>cn.scl.Main</mainClass></manifest></archive></configuration></plugin></plugins></build>

 3.打包运行

 

 

 假如我加入其它架包了 ,又该如何打包,才能运行?

 <!--自己加入的架包--><dependencies><!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.16.1</version></dependency></dependencies>

有小伙伴就说了,我依旧像前边的那种方式来打包(结果肯定是不行的)。。。

那你只需加入下边这些代码,就可以了:

 <plugin><!--可将其他插件也加入打包,会生成lib目录--><!-- maven-dependency-plugin --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><version>3.6.0</version><executions><execution><id>copy-dependencies</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/lib</outputDirectory></configuration></execution></executions></plugin>

 代码都加完了主程序不得改进以下啊。。。


public class Main {public static void main(String[] args) {try {System.out.println(Jsoup.connect("http://www.baidu.com").get().title());} catch (Exception e) {e.printStackTrace();}System.out.println("hello java ...");}
}

现在你再试一下打包。。。

细心的小伙伴就发现了,这里多了一个lib目录。。

 那就有小伙伴有疑问了,我不想要lib目录行不行:答案是当然可以

那你只需要修改一点代码就行:(将上边我们写的哪些代码注释掉,换上新的插件即可

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.5.0</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>cn.scl.Main</mainClass></transformer></transformers></configuration></execution></executions></plugin><!--            <plugin>--><!--                &lt;!&ndash;可将其他插件也加入打包,会生成lib目录&ndash;&gt;--><!--                &lt;!&ndash; maven-dependency-plugin &ndash;&gt;--><!--                <groupId>org.apache.maven.plugins</groupId>--><!--                <artifactId>maven-dependency-plugin</artifactId>--><!--                <version>3.6.0</version>--><!--                <executions>--><!--                    <execution>--><!--                        <id>copy-dependencies</id>--><!--                        <phase>package</phase>--><!--                        <goals>--><!--                            <goal>copy-dependencies</goal>--><!--                        </goals>--><!--                        <configuration>--><!--                            <outputDirectory>${project.build.directory}/lib</outputDirectory>--><!--                        </configuration>--><!--                    </execution>--><!--                </executions>--><!--            </plugin>--><!--            &lt;!&ndash;打包成为可执行包&ndash;&gt;--><!--            <plugin>--><!--                <groupId>org.apache.maven.plugins</groupId>--><!--                <artifactId>maven-jar-plugin</artifactId>--><!--                <version>3.3.0</version>--><!--                <configuration>--><!--                    <archive>--><!--                        <manifest>--><!--                            <addClasspath>true</addClasspath>--><!--                            <classpathPrefix>lib/</classpathPrefix>--><!--                            <mainClass>cn.scl.Main</mainClass>--><!--                        </manifest>--><!--                    </archive>--><!--                </configuration>--><!--            </plugin>--></plugins></build>

你再打包你会发现。。。lib目录消失了新增了一个jar文件。我们实验一下:输入cmd....

 

 到这里就结束了,maven的实用打包方式就讲解完了。。。

完整程序:xml文件

<?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><groupId>org.example</groupId><artifactId>m12</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--自己加入的架包--><dependencies><!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.16.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.5.0</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformerimplementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>cn.scl.Main</mainClass></transformer></transformers></configuration></execution></executions></plugin><!--            <plugin>--><!--                &lt;!&ndash;可将其他插件也加入打包,会生成lib目录&ndash;&gt;--><!--                &lt;!&ndash; maven-dependency-plugin &ndash;&gt;--><!--                <groupId>org.apache.maven.plugins</groupId>--><!--                <artifactId>maven-dependency-plugin</artifactId>--><!--                <version>3.6.0</version>--><!--                <executions>--><!--                    <execution>--><!--                        <id>copy-dependencies</id>--><!--                        <phase>package</phase>--><!--                        <goals>--><!--                            <goal>copy-dependencies</goal>--><!--                        </goals>--><!--                        <configuration>--><!--                            <outputDirectory>${project.build.directory}/lib</outputDirectory>--><!--                        </configuration>--><!--                    </execution>--><!--                </executions>--><!--            </plugin>--><!--            &lt;!&ndash;打包成为可执行包&ndash;&gt;--><!--            <plugin>--><!--                <groupId>org.apache.maven.plugins</groupId>--><!--                <artifactId>maven-jar-plugin</artifactId>--><!--                <version>3.3.0</version>--><!--                <configuration>--><!--                    <archive>--><!--                        <manifest>--><!--                            <addClasspath>true</addClasspath>--><!--                            <classpathPrefix>lib/</classpathPrefix>--><!--                            <mainClass>cn.scl.Main</mainClass>--><!--                        </manifest>--><!--                    </archive>--><!--                </configuration>--><!--            </plugin>--></plugins></build></project>

Java文件:

/** Copyright (c) 2020, 2023,  All rights reserved.**/
package cn.scl;import org.jsoup.Jsoup;/*** <p>Project: m12 - Main</p>* <p>Powered by scl On 2023-08-01 20:21:29</p>* <p>描述:<p>** @author scl [1846080280@qq.com]* @version 1.0* @since 17*/
public class Main {public static void main(String[] args) {try {System.out.println(Jsoup.connect("http://www.baidu.com").get().title());} catch (Exception e) {e.printStackTrace();}System.out.println("hello java ...");}
}


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

相关文章

IDEA中Maven项目打包方式

方式一&#xff1a; 直接打包&#xff0c;不打包依赖包&#xff0c;仅打包出项目中的代码到JAR包中&#xff0c;可称为架包。在其他应用中运行时&#xff0c;必须保证其上面有相应的依赖包&#xff0c;才能运行。maven——》Lifecyle——》Clean——》Package 打包成功后就会出…

将IDEA MAVEN项目打包成jar包的通用方法

1.找到左上角File -> Project Structure&#xff0c;点击&#xff08;中文&#xff1a;文件 -> 项目结构&#xff09; 2. 在弹出的界面中&#xff0c;点击Artifacts&#xff08;中文&#xff1a;工件&#xff09;&#xff0c;点击内置页面的左上角的号&#xff0c;选中第…

maven多个子项目如何打包?

两种方式: 第一种方式: 父模块pom 不配置任何打包命令; 子模块(资源pom) 指定这个pom是资源类型; 子模块(服务pom) 指定这个pom是java服务类型; 1. 前提: maven对应多个子项目, 每个子项目都是一个单独运行的jar包 2. 项目结构: 五个 jar包; 一个common资源包 3. 思…

maven项目常用的打包方式

一、无依赖其他任何jar <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive>…

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;对于图像上的每一个像素点其实就当做…