如何打一個FatJar(uber-jar)

如何打一個FatJar(uber-jar)

FatJar也就叫作UberJar,是一種可執行的Jar包(Executable Jar)。FatJar和普通的jar不一樣在於它包含了依賴的jar包。java

1. maven-jar-plugin

例子

<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>
  1. 首先使用maven-dependency-plugin把依賴所有copy到outputDirectory中,爲了將包打到一個jar中,須要把outputDirectory配置到classes/lib中
  2. 使用maven-jar-plugin指定MainClass和classpath,須要注意的是classpathPrefix必須是${project.build.finalName}/lib路徑
  3. 執行命令
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

2. maven-assembly-plugin

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

3. maven-shade-plugin

3.1 一個簡單的例子

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

3.2 設置MainClass建立一個可執行的Jar包

<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

3.2.1 Resource Transformers

  1. AppendingTransformer 增長一個文件到resource
  2. XmlAppendingTransformer 增長一個xml到resource
  3. ManifestResourceTransformer 設置MANIFEST

3.3 使用include/exclude指定須要打包的jar

在configuration元素下增長includes和excludes節點

<configuration>
  <artifactSet>
    <includes>
      <include>org.apache.commons:commons-langs</include>
    </includes>
    <excludes>
      <exclude>com.alibaba:fastjson</exclude>
    </excludes>
  </artifactSet>
</configuration>

3.4 使用filter過濾jar包中的類或者資源

在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>

3.5 maven-shade-plugin 自動將全部不使用的類所有排除掉

<executions>
  <execution>
    <phase>package</phase>
    <goals>
      <goal>shade</goal>
    </goals>
    <configuration>
      <minimizeJar>true</minimizeJar>
    </configuration>
  </execution>
</executions>
相關文章
相關標籤/搜索