这篇文章主要介绍了怎么使用MAVEN打JAR包的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用MAVEN打JAR包文章都会有所收获,下面我们一起来看看吧。
使用MAVEN打JAR包
一、简单的方法:
首先在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>
<!-- 需要执行的main -->
</archive>
</configuration>
</plugin>
</plugins>
</build>
然后执行:mvn assembly:assembly
最后执行:java -jar target/ticket-data-integration-0.0.1-SNAPSHOT-jar-with-dependencies.jar
二、执行任意main方法
两个类,Boot类:
package com.qunar.check.integration;
public class Boot {
public static void main(String[] args){
System.out.println("test xingxing");
}
}
Boot2类:
package com.qunar.check.integration;
public class Boot2 {
public static void main(String[] args){
System.out.println("test liqiu");
}
}
那么执行:
$ 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
两个main函数都可以执行
扩展:maven 使用assembly 进行打包
1. pom 中添加assembly 插件
要使用assembly 进项编译打包, 首先主要在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插件参数设置,指定编码 -->
<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>
<!-- 这个插件是关键 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- 这个是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. 创建assembly文件夹和assembly.xml文件
创建assembly文件夹和assembly.xml文件, 这个样子创建主要是规范。
在pom 中已经介绍assembly.xml 位置。
<!-- 这个是assembly 所在位置 -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
创建assembly.xml 文件后添加如下内容:
<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 官方解释:
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
上述的三个fileSet 分别是将resource 下的资源打包到config 目录下, 将assembly下的bin 启动相关脚本打包到bin 目录下, 将maven项目依赖的所有jar 包, 打包到lib 中。
具体结构如下图所示: