圖文詳解Maven工程打jar包的N種方式
一、IDEA自帶打包插件
內(nèi)容:此種方式可以自己選擇制作胖包或者瘦包,但推薦此種方式制作瘦包。
輸出:輸出目錄在out目錄下
流程步驟:
第一步: 依次選擇 file->projecct structure->artifacts->點(diǎn)擊+ (選擇jar)->選擇 from module with dependencies

第二步:彈出窗口中指定Main Class,是否選擇依賴jar包,是否包含測(cè)試。(盡量不選依賴包,防止依賴包選擇不全)




第三步:點(diǎn)擊Build–>Build Artifacts–>選擇bulid
二、maven插件打包
輸出:輸出目錄在target目錄下
2.1 制作瘦包(直接打包,不打包依賴包)
內(nèi)容:僅打包出項(xiàng)目中的代碼到JAR包中。
方式:在pom.xml中添加如下plugin; 隨后執(zhí)行maven install
<!-- java編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>指定版本</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin>
2.2 制作瘦包和依賴包(相互分離)
內(nèi)容:將依賴JAR包輸出到lib目錄方式(打包方式對(duì)于JAVA項(xiàng)目是通用的)
將項(xiàng)目中的JAR包的依賴包輸出到指定的目錄下,修改outputDirectory配置,如下面的${project.build.directory}/lib。
方式:
- pom.xml的build>plugins中添加如下配置。
- 點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package打包
注意:如果想將打包好的JAR包通過命令直接運(yùn)行,如java -jar xx.jar。需要制定manifest配置的classpathPrefix與上面配置的相對(duì)應(yīng)。如上面把依賴JAR包輸出到了lib,則這里的classpathPrefix也應(yīng)指定為lib/;同時(shí),并指定出程序的入口類,在配置mainClass節(jié)點(diǎn)中配好入口類的全類名。
<plugins>
<!-- java編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.yourpakagename.mainClassName</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
注意:默認(rèn)的classpath會(huì)在jar包內(nèi)。為了方便,可以在Main方法配置后加上manifestEntries配置,指定classpath。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <classesDirectory>target/classes/</classesDirectory> <archive> <manifest> <!-- 主函數(shù)的入口 --> <mainClass>com.yourpakagename.mainClassName</mainClass> <!-- 打包時(shí) MANIFEST.MF文件不記錄的時(shí)間戳版本 --> <useUniqueVersions>false</useUniqueVersions> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>.</Class-Path> </manifestEntries> </archive> </configuration> </plugin>
2.3 制作胖包(項(xiàng)目依賴包和項(xiàng)目打?yàn)橐粋€(gè)包)
內(nèi)容:將項(xiàng)目中的依賴包和項(xiàng)目代碼都打?yàn)橐粋€(gè)JAR包
方式:
pom.xml的build>plugins中添加如下配置;
點(diǎn)擊maven project(右邊欄)->選擇Plugins->選擇assembly->點(diǎn)擊assembly:assembly
注意:1. 針對(duì)傳統(tǒng)的JAVA項(xiàng)目打包;
2. 打包指令為插件的assembly命令,盡量不用package指令。
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.5.5</version> <configuration> <archive> <manifest> <mainClass>com.xxg.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
2.4 制作胖包(transform部分自定義)
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.tooling</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xxx.xxxInvoke</mainClass> </transformer> </transformers> <minimizeJar>true</minimizeJar> <shadedArtifactAttached>true</shadedArtifactAttached> </configuration> </execution> </executions> </plugin>
三、SpringBoot項(xiàng)目打包
內(nèi)容:將當(dāng)前項(xiàng)目里所有依賴包和當(dāng)前項(xiàng)目的源碼都打成一個(gè)JAR包,同時(shí)還會(huì)將沒有依賴包的JAR包也打出來,以.original保存
方式:
在pom.xml的build>plugins中加入如下配置點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
四、Scala項(xiàng)目打包
方式:
在pom.xml的build>plugins中加入如下配置
點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
五、groovy項(xiàng)目打包
方式:
在pom.xml的build>plugins中加入如下配置
點(diǎn)擊maven project(右邊欄)->選擇Lifecycle->點(diǎn)擊package或install打包
<plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.2</version> <executions> <execution> <goals> <goal>addSources</goal> <goal>addStubSources</goal> <goal>compile</goal> <goal>execute</goal> </goals> </execution> </executions> </plugin>
總結(jié)
到此這篇關(guān)于Maven工程打jar包的N種方式的文章就介紹到這了,更多相關(guān)Maven工程打jar包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Spring Cache和Redis實(shí)現(xiàn)查詢數(shù)據(jù)緩存
在現(xiàn)代應(yīng)用程序中,查詢緩存的使用已經(jīng)變得越來越普遍,它不僅能夠顯著提高系統(tǒng)的性能,還能提升用戶體驗(yàn),在這篇文章中,我們將探討緩存的基本概念、重要性以及如何使用Spring Cache和Redis實(shí)現(xiàn)查詢數(shù)據(jù)緩存,需要的朋友可以參考下2024-07-07
使用Jmeter進(jìn)行http接口測(cè)試的實(shí)踐
本文主要針對(duì)http接口進(jìn)行測(cè)試,使用Jmeter工具實(shí)現(xiàn)。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項(xiàng)目”功能
這篇文章主要介紹了IntelliJ IDEA 2020.3.3現(xiàn)已發(fā)布!新增“受信任項(xiàng)目”功能,本文給大家分享了idea2020.3.3激活碼的詳細(xì)破解教程,每種方法都很好用,使用idea2020.3以下所有版本,需要的朋友可以參考下2021-03-03
Springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置詳解流程
在日常開發(fā)中,若遇到多個(gè)數(shù)據(jù)源的需求,怎么辦呢?通過springboot集成mybatis實(shí)現(xiàn)多數(shù)據(jù)源配置,簡(jiǎn)單嘗試一下,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
SpringBoot使用quartz,注入feignClient,client為null問題
在SpringBoot中使用Quartz和FeignClient時(shí),如果通過@Autowired或構(gòu)造方法注入FeignClient導(dǎo)致為null,可以使用Spring提供的通過文件名獲取bean的方式解決,這種方法在Quartz啟動(dòng)時(shí)通過反射注入類,而類還未初始化好,導(dǎo)致FeignClient為null的問題2024-11-11
Java工廠模式優(yōu)雅地創(chuàng)建對(duì)象以及提高代碼復(fù)用率和靈活性
Java工廠模式是一種創(chuàng)建型設(shè)計(jì)模式,通過定義一個(gè)工廠類來封裝對(duì)象的創(chuàng)建過程,將對(duì)象的創(chuàng)建和使用分離,提高代碼的可維護(hù)性和可擴(kuò)展性,同時(shí)可以實(shí)現(xiàn)更好的代碼復(fù)用和靈活性2023-05-05

