1、创建maven项目,项目结构如下:
2、创建启动类
/** * 启动类 * * @author Administrator * @date 6/20/2020 */ public class JarTestApplication { public static void main(String[] args) { System.out.println("jarTest启动"); } }
3、pom文件内容
<?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.dh</groupId> <artifactId>jarTest</artifactId> <version>1.0-SNAPSHOT</version> <!--打包后的包后缀,war/jar/zip--> <packaging>jar</packaging> <properties> <!--字符编码--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--对应jdk版本--> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <!--打包插件需要的依赖--> <dependencies> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> </dependency> </dependencies> <build> <plugins> <!--打包用到的插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <!-- 此处为主入口--> <mainClass>JarTestApplication</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> <!--打包得到的包名--> <finalName>jarTest</finalName> </build> </project>
4、cmd进入jar包所在文件夹,java -jar jarTest.jar启动jar包,成功结果如下: