如何使用MAVEN打JAR包(直接使用)
使用MAVEN打JAR包
一、簡(jiǎn)單的方法:
首先在pom.xml里面添加:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.qunar.piao.data.integration.Boot</mainClass>
</manifest>
<!-- 需要執(zhí)行的main -->
</archive>
</configuration>
</plugin>
</plugins>
</build>然后執(zhí)行:mvn assembly:assembly
最后執(zhí)行:java -jar target/ticket-data-integration-0.0.1-SNAPSHOT-jar-with-dependencies.jar
二、執(zhí)行任意main方法
兩個(gè)類(lèi),Boot類(lèi):
package com.qunar.check.integration;
public class Boot {
public static void main(String[] args){
System.out.println("test xingxing");
}
}Boot2類(lèi):
package com.qunar.check.integration;
public class Boot2 {
public static void main(String[] args){
System.out.println("test liqiu");
}
}那么執(zhí)行:
$ java -classpath target/check-jar-with-dependencies.jar com.qunar.check.integration.Boot2 test liqiu $ java -classpath target/check-jar-with-dependencies.jar com.qunar.check.integration.Boot test xingxing
兩個(gè)main函數(shù)都可以執(zhí)行
續(xù):
如果你的項(xiàng)目包含Spring,那么打包可能就會(huì)遇到的麻煩,可以參考:
http://www.dbjr.com.cn/article/276778.htm
擴(kuò)展:maven 使用assembly 進(jìn)行打包
1. pom 中添加assembly 插件
要使用assembly 進(jìn)項(xiàng)編譯打包, 首先主要在pom 中的build中添加插件信息, 具體如圖下所示:
<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>${profile.dir}</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<!-- compiler插件參數(shù)設(shè)置,指定編碼 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!-- 這個(gè)插件是關(guān)鍵 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 這個(gè)是assembly 所在位置 -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2. 創(chuàng)建assembly文件夾和assembly.xml文件
創(chuàng)建assembly文件夾和assembly.xml文件, 這個(gè)樣子創(chuàng)建主要是規(guī)范。
在pom 中已經(jīng)介紹assembly.xml 位置。
<!-- 這個(gè)是assembly 所在位置 --> <descriptor>src/main/assembly/assembly.xml</descriptor>
創(chuàng)建assembly.xml 文件后添加如下內(nèi)容:
<assembly>
<formats>
<!--支持 zip,tar,tar.gz,tar.bz2,jar,dir,war 等 -->
<format>tar.gz</format>
<format>zip</format>
<format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>${profile.dir}</directory>
<outputDirectory>conf</outputDirectory>
<!-- 表示的是包含下面格式的資源文件 -->
<includes>
<include>*.xml</include>
<include>*.properties</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>src/main/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
fileMode 官方解釋?zhuān)?/p>
Similar to a UNIX permission, sets the file mode of the files included. THIS IS AN OCTAL VALUE. Format: (User)(Group)(Other) where each component is a sum of Read = 4, Write = 2, and Execute = 1. For example, the value 0644 translates to User read-write, Group and Other
上述的三個(gè)fileSet 分別是將resource 下的資源打包到config 目錄下, 將assembly下的bin 啟動(dòng)相關(guān)腳本打包到bin 目錄下, 將maven項(xiàng)目依賴(lài)的所有jar 包, 打包到lib 中。
具體結(jié)構(gòu)如下圖所示:

參考地址:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
到此這篇關(guān)于如何使用MAVEN打JAR包(直接使用)的文章就介紹到這了,更多相關(guān)maven打jar包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot Excel文件導(dǎo)出下載實(shí)現(xiàn)代碼
這篇文章帶領(lǐng)我們直接實(shí)現(xiàn)Excel文件的直接導(dǎo)出下載,后續(xù)開(kāi)發(fā)不需要開(kāi)發(fā)很多代碼,直接繼承已經(jīng)寫(xiě)好的代碼,增加一個(gè)Xml配置就可以直接導(dǎo)出。具體實(shí)現(xiàn)代碼大家跟隨小編一起通過(guò)本文學(xué)習(xí)吧2018-11-11
Java實(shí)現(xiàn)每日給女友微信發(fā)送早安信息
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)每日給女友微信發(fā)送早安等微信信息,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-12-12
java中的阻塞隊(duì)列應(yīng)用場(chǎng)景及代碼實(shí)例
這篇文章主要介紹了java中的阻塞隊(duì)列應(yīng)用場(chǎng)景及代碼實(shí)例阻塞隊(duì)列是一種特殊的隊(duì)列,它提供了線程安全的操作,并在隊(duì)列為空或滿(mǎn)時(shí)提供了阻塞的功能,阻塞隊(duì)列通常用于多線程場(chǎng)景,其中生產(chǎn)者線程向隊(duì)列中添加元素,而消費(fèi)者線程從隊(duì)列中獲取元素,需要的朋友可以參考下2024-01-01
Java使用MessageFormat應(yīng)注意的問(wèn)題
這篇文章主要介紹了Java使用MessageFormat應(yīng)注意的問(wèn)題,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-06-06

