Maven构建可执行的jar包(包含依赖jar包)
程序员文章站
2022-03-02 17:33:19
...
maven构建jar包的步骤:
1.执行可执行的class,代码内需要有入口main方法
2.通过mvn package来构建jar包
3.使用java -jar test.jar来执行jar包
一、包含依赖jar包
maven的pom.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>com.xxx.delon</groupId>
<artifactId>bugly</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 工程所需jar包引用开始 -->
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!-- 代码所需依赖 -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.0</version>
</dependency>
<!-- 代码所需依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
<!-- 工程所需jar包引用开始 -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 此处指定main方法入口的class -->
<mainClass>com.xxx.uploadFile</mainClass>
</manifest>
</archive>
ta </configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
生成jar包,会生成在target目录下
执行打包
mvn package
执行:
# 运行jar
java -jar test.jar