FatJar也就叫作UberJar,是一種可執行的Jar包(Executable Jar)。FatJar和普通的jar不一樣在於它包含了依賴的jar包。java
<build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.0.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.yourcompay.Demo</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
java -jar ./target/demo.jar
注意:採用maven-jar-plugin方式有個缺點,demo.jar不能移動到其餘路徑下面執行java -jar命令,由於demo.jar依賴放在了lib/目錄下apache
解決方法有點複雜,首先用maven-dependency-plugin把依賴copy到${project.build.directory}/classes/lib,而後須要自定義jar加載的classloader,真複雜...json
maven-assembly-plugin是一個強大的打包工具,除了jar包還能打zip、tar.gz、tar.bz2等類型的包,描述文件放在assembly.xml中maven
如何用assembly打一個FatJar,使用預約義格式jar-with-dependencies打包工具
實際上ui
<build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <archive> <manifest> <mainClass>com.yourcompany.Demo</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>
執行命令make clean package,獲得demo-jar-with-dependencies.jar,這時咱們就能夠使用java -jar命令了spa
注意:預約義格式jar-with-dependencies實際上只有基本的FatJar打包功能,背後集成是maven-shade-plugin。下一節就講shade-plugincode
maven-shade-plugin將goal shade:shade綁定到maven的package階段orm
<build> <finalName>demo</finalName> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
執行命令maven package在target目錄下生成一個demo.jarxml
<build> <finalName>demo</finalName> <plugins> <plugin> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>com.yourcompany.Demo</Main-Class> </manifestEntries> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build>
執行mvn package命令生成一個demo.jar,執行demo.jar
java -jar ./target/demo.jar
在configuration元素下增長includes和excludes節點
<configuration> <artifactSet> <includes> <include>org.apache.commons:commons-langs</include> </includes> <excludes> <exclude>com.alibaba:fastjson</exclude> </excludes> </artifactSet> </configuration>
在configuration元素下增長filters節點
<configuration> <filters> <filter> <artifact>junit:junit</artifact> <includes> <include>junit/framework/**</include> <include>org/junit/**</include> </includes> <excludes> <exclude>org/junit/experimental/**</exclude> <exclude>org/junit/runners/**</exclude> </excludes> </filter> </filters> </configuration>
<executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <minimizeJar>true</minimizeJar> </configuration> </execution> </executions>