Maven可以使用mvn package指令對(duì)項(xiàng)目進(jìn)行打包验残,如果使用Java -jar xxx.jar執(zhí)行運(yùn)行jar文件,會(huì)出現(xiàn)"no main manifest attribute, in xxx.jar"(沒(méi)有設(shè)置Main-Class)巾乳、ClassNotFoundException(找不到依賴包)等錯(cuò)誤您没。
要想jar包能直接通過(guò)java -jar xxx.jar運(yùn)行,需要滿足:
- 在jar包中的META-INF/MANIFEST.MF中指定Main-Class胆绊,這樣才能確定程序的入口在哪里氨鹏;
- 要能加載到依賴包。
使用Maven有以下幾種方法可以生成能直接運(yùn)行的jar包压状,可以根據(jù)需要選擇一種合適的方法仆抵。
方法一:使用maven-jar-plugin和maven-dependency-plugin插件打包
在pom.xml中配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
maven-jar-plugin用于生成META-INF/MANIFEST.MF文件的部分內(nèi)容,<mainClass>com.xxg.Main</mainClass>指定MANIFEST.MF中的Main-Class种冬,<addClasspath>true</addClasspath>會(huì)在MANIFEST.MF加上Class-Path項(xiàng)并配置依賴包镣丑,<classpathPrefix>lib/</classpathPrefix>指定依賴包所在目錄。
例如下面是一個(gè)通過(guò)maven-jar-plugin插件生成的MANIFEST.MF文件片段:
Class-Path: lib/commons-logging-1.2.jar lib/commons-io-2.4.jar
Main-Class: com.xxg.Main
只是生成MANIFEST.MF文件還不夠碌廓,maven-dependency-plugin插件用于將依賴包拷貝到<outputDirectory>${project.build.directory}/lib</outputDirectory>指定的位置传轰,即lib目錄下。
配置完成后谷婆,通過(guò)mvn package指令打包慨蛙,會(huì)在target目錄下生成jar包,并將依賴包拷貝到target/lib目錄下纪挎,目錄結(jié)構(gòu)如下:
指定了Main-Class期贫,有了依賴包,那么就可以直接通過(guò)java -jar xxx.jar運(yùn)行jar包异袄。
這種方式生成jar包有個(gè)缺點(diǎn)通砍,就是生成的jar包太多不便于管理,下面兩種方式只生成一個(gè)jar文件烤蜕,包含項(xiàng)目本身的代碼封孙、資源以及所有的依賴包。
方法二:使用maven-assembly-plugin插件打包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
mvn package assembly:single
打包后會(huì)在target目錄下生成一個(gè)xxx-jar-with-dependencies.jar文件讽营,這個(gè)文件不但包含了自己項(xiàng)目中的代碼和資源虎忌,還包含了所有依賴包的內(nèi)容。所以可以直接通過(guò)java -jar來(lái)運(yùn)行橱鹏。
此外還可以直接通過(guò)mvn package來(lái)打包膜蠢,無(wú)需assembly:single堪藐,不過(guò)需要加上一些配置:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xxg.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
其中<phase>package</phase>、<goal>single</goal>即表示在執(zhí)行package打包時(shí)挑围,執(zhí)行assembly:single礁竞,所以可以直接使用mvn package打包。
不過(guò)杉辙,如果項(xiàng)目中用到spring Framework模捂,用這種方式打出來(lái)的包運(yùn)行時(shí)會(huì)出錯(cuò),使用下面的方法三可以處理奏瞬。
方法三:使用maven-shade-plugin插件打包
<build>
<plugins>
<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.xxg.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置完成后枫绅,執(zhí)行mvn package即可打包。在target目錄下會(huì)生成兩個(gè)jar包硼端,注意不是original-xxx.jar文件并淋,而是另外一個(gè)。和maven-assembly-plugin一樣珍昨,生成的jar文件包含了所有依賴县耽,所以可以直接運(yùn)行。
如果項(xiàng)目中用到了Spring Framework镣典,將依賴打到一個(gè)jar包中兔毙,運(yùn)行時(shí)會(huì)出現(xiàn)讀取XML schema文件出錯(cuò)。原因是Spring Framework的多個(gè)jar包中包含相同的文件spring.handlers和spring.schemas兄春,如果生成一個(gè)jar包會(huì)互相覆蓋澎剥。為了避免互相影響,可以使用AppendingTransformer來(lái)對(duì)文件內(nèi)容追加合并:
<build>
<plugins>
<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.xxg.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>