天天看點

大資料應用常用打包方式

一、簡介#

在送出大資料作業到叢集上運作時,通常需要先将項目打成 JAR 包。這裡以 Maven 為例,常用打包方式如下:

  • 不加任何插件,直接使用 mvn package 打包;
  • 使用 maven-assembly-plugin 插件;
  • 使用 maven-shade-plugin 插件;
  • 使用 maven-jar-plugin 和 maven-dependency-plugin 插件;

以下分别進行詳細的說明。

二、mvn package#

不在 POM 中配置任何插件,直接使用 mvn package 進行項目打包,這對于沒有使用外部依賴包的項目是可行的。但如果項目中使用了第三方 JAR 包,就會出現問題,因為 mvn package 打的 JAR 包中是不含有依賴包,會導緻作業運作時出現找不到第三方依賴的異常。這種方式局限性比較大,因為實際的項目往往很複雜,通常都會依賴第三方 JAR。

大資料架構的開發者也考慮到這個問題,是以基本所有的架構都支援在送出作業時使用 --jars 指定第三方依賴包,但是這種方式的問題同樣很明顯,就是你必須保持生産環境與開發環境中的所有 JAR 包版本一緻,這是有維護成本的。

基于上面這些原因,最簡單的是采用 All In One 的打包方式,把所有依賴都打包到一個 JAR 檔案中,此時對環境的依賴性最小。要實作這個目的,可以使用 Maven 提供的 maven-assembly-plugin 或 maven-shade-plugin 插件。

三、maven-assembly-plugin插件#

Assembly 插件支援将項目的所有依賴、檔案都打包到同一個輸出檔案中。目前支援輸出以下檔案類型:

  • zip
  • tar
  • tar.gz (or tgz)
  • tar.bz2 (or tbz2)
  • tar.snappy
  • tar.xz (or txz)
  • jar
  • dir
  • war

3.1 基本使用#

在 POM.xml 中引入插件,指定打包格式的配置檔案 assembly.xml(名稱可自定義),并指定作業的主入口類:

Copy

<build>    <plugins>        <plugin>            <artifactId>maven-assembly-plugin</artifactId>            <configuration>                <descriptors>                    <descriptor>src/main/resources/assembly.xml</descriptor>                </descriptors>                <archive>                    <manifest>                        <mainClass>com.heibaiying.wordcount.ClusterWordCountApp</mainClass>                    </manifest>                </archive>            </configuration>        </plugin>    </plugins></build>

assembly.xml 檔案内容如下:

Copy

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0                               http://maven.apache.org/xsd/assembly-2.0.0.xsd">        <id>jar-with-dependencies</id>    <!--指明打包方式-->    <formats>        <format>jar</format>    </formats>    <includeBaseDirectory>false</includeBaseDirectory>    <dependencySets>        <dependencySet>            <outputDirectory>/</outputDirectory>            <useProjectArtifact>true</useProjectArtifact>            <unpack>true</unpack>            <scope>runtime</scope>            <!--這裡以排除 storm 環境中已經提供的 storm-core 為例,示範排除 Jar 包-->            <excludes>                <exclude>org.apache.storm:storm-core</exclude>            </excludes>        </dependencySet>    </dependencySets></assembly>3.2 打包指令#

采用 maven-assembly-plugin 進行打包時指令如下:

Copy

# mvn assembly:assembly

打包後會同時生成兩個 JAR 包,其中字尾為 jar-with-dependencies 是含有第三方依賴的 JAR 包,字尾是由 assembly.xml 中 <id>标簽指定的,可以自定義修改。

大資料應用常用打包方式

四、maven-shade-plugin插件#

maven-shade-plugin 比 maven-assembly-plugin 功能更為強大,比如你的工程依賴很多的 JAR 包,而被依賴的 JAR 又會依賴其他的 JAR 包,這樣,當工程中依賴到不同的版本的 JAR 時,并且 JAR 中具有相同名稱的資源檔案時,shade 插件會嘗試将所有資源檔案打包在一起時,而不是和 assembly 一樣執行覆寫操作。

通常使用 maven-shade-plugin 就能夠完成大多數的打包需求,其配置簡單且适用性最廣,是以建議優先使用此方式。

4.1 基本配置#

采用 maven-shade-plugin 進行打包時候,配置示例如下:

Copy

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-shade-plugin</artifactId>    <configuration>        <createDependencyReducedPom>true</createDependencyReducedPom>        <filters>            <filter>                <artifact>*:*</artifact>                <excludes>                    <exclude>META-INF*.scala</include>                </includes>            </configuration>        </execution>        <execution>            <id>scala-test-compile</id>            <goals>                <goal>testCompile</goal>            </goals>        </execution>    </executions></plugin>