使用maven的maven-jar-plugin插件可以生成manifest文件,同时把依赖的jar写入manifest.
<
build
>
< plugins >
< plugin >
< groupId >org.apache.maven.plugins </ groupId >
< artifactId >maven-jar-plugin </ artifactId >
...
< configuration >
< archive >
< manifest >
< addClasspath >true </ addClasspath >
</ manifest >
</ archive >
</ configuration >
...
</ plugin >
</ plugins >
</ build >
...
< dependencies >
< dependency >
< groupId >commons-lang </ groupId >
< artifactId >commons-lang </ artifactId >
< version >2.1 </ version >
</ dependency >
< dependency >
< groupId >org.codehaus.plexus </ groupId >
< artifactId >plexus-utils </ artifactId >
< version >1.1 </ version >
</ dependency >
</ dependencies >
< plugins >
< plugin >
< groupId >org.apache.maven.plugins </ groupId >
< artifactId >maven-jar-plugin </ artifactId >
...
< configuration >
< archive >
< manifest >
< addClasspath >true </ addClasspath >
</ manifest >
</ archive >
</ configuration >
...
</ plugin >
</ plugins >
</ build >
...
< dependencies >
< dependency >
< groupId >commons-lang </ groupId >
< artifactId >commons-lang </ artifactId >
< version >2.1 </ version >
</ dependency >
< dependency >
< groupId >org.codehaus.plexus </ groupId >
< artifactId >plexus-utils </ artifactId >
< version >1.1 </ version >
</ dependency >
</ dependencies >
build后生成的manifest文件将包含以下信息
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: ${user.name} Build-Jdk: ${java.version} Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar
想生成可以执行的jar包,可以在<manifest></manifest>加入
<
mainClass
>fully.qualified.MainClass
</
mainClass
>
即把程序入口(含main方法的类)添加进manifest中
build后manifest文件信息
Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: ${user.name} Build-Jdk: ${java.version} Main-Class: fully.qualified.MainClass Class-Path: plexus-utils-1.1.jar commons-lang-2.1.jar
可以使用java -jar jarname 执行jar包