java如何打可执行jar包?4种方式
程序员文章站
2022-05-01 12:26:57
...
1.手动修改
/META-INF/ MANIFEST.MF
普通的jar包
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Administrator
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_171
要变成可执行jar包,需要指定main
Main-Class: com.my.jvm.AssemblyTest
如果jar包有依赖其他第三方jar,还要指定Class-Path
Class-Path: lib/log4j-1.2.17.jar lib/slf4j-log4j12-1.7.21.jar lib/slf4
2. assembly插件:把相关依赖包也打进来,通过plugins下的assembly:assembly执行,缺点据说是spirng项目可能会报错(没验证过)。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.my.jvm.AssemblyTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
3. shade插件(推荐),功能同assembly,直接mvn package执行
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.my.jvm.AssemblyTest</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
4. maven-jar-plugin(推荐),好处是不打包依赖,出来的包很小,直接mvn package执行
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.my.jvm.AssemblyTest</mainClass>
</manifest>
</archive>
<excludes>
<exclude>*.properties</exclude>
<exclude>*.txt</exclude>
</excludes>
</configuration>
</plugin>
上一篇: Java基础——监听器的使用和原理(含有可执行demo)
下一篇: 背景图自适应