Maven常用的编译、打包方式总结

目录

一、简介

二、mvn package

三、maven-assembly-plugin插件

3.1 基本使用

3.2 打包命令

四、maven-shade-plugin插件

4.1 基本配置

4.2 打包命令

五、其他打包需求

1. 使用非Maven仓库中的Jar

2. 排除集群中已经存在的Jar

3. 打包Scala文件

参考资料


一、简介

在提交大数据作业到集群上运行时,通常需要先将项目打成 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-pluginmaven-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(名称可自定义),并指定作业的主入口类:

<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 文件内容如下:

<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 进行打包时命令如下:

# mvn assembly:assembly 

打包后会同时生成两个 JAR 包,其中后缀为 jar-with-dependencies 是含有第三方依赖的 JAR 包,后缀是由 assembly.xml<id> 标签指定的,可以自定义修改。

四、maven-shade-plugin插件

maven-shade-pluginmaven-assembly-plugin 功能更为强大,比如你的工程依赖很多的 JAR 包,而被依赖的 JAR 又会依赖其他的 JAR 包,这样,当工程中依赖到不同的版本的 JAR 时,并且 JAR 中具有相同名称的资源文件时,shade 插件会尝试将所有资源文件打包在一起时,而不是和 assembly 一样执行覆盖操作。

通常使用 maven-shade-plugin 就能够完成大多数的打包需求,其配置简单且适用性最广,因此建议优先使用此方式。

4.1 基本配置

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

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <configuration>
        <createDependencyReducedPom>true</createDependencyReducedPom>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.sf</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.dsa</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                    <exclude>META-INF/*.rsa</exclude>
                    <exclude>META-INF/*.EC</exclude>
                    <exclude>META-INF/*.ec</exclude>
                    <exclude>META-INF/MSFTSIG.SF</exclude>
                    <exclude>META-INF/MSFTSIG.RSA</exclude>
                </excludes>
            </filter>
        </filters>
        <artifactSet>
            <excludes>
                <exclude>org.apache.storm:storm-core</exclude>
            </excludes>
        </artifactSet>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                       implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer
                       implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

以上配置来源于 Storm Github,在上面的配置中,排除了部分文件,这是因为有些 JAR 包生成时,会使用 jarsigner 生成文件签名 (完成性校验),分为两个文件存放在 META-INF 目录下:

  • a signature file, with a .SF extension;

  • a signature block file, with a .DSA, .RSA, or .EC extension。

如果某些包的存在重复引用,这可能会导致在打包时候出现 Invalid signature file digest for Manifest main attributes 异常,所以在配置中排除这些文件。

4.2 打包命令

使用 maven-shade-plugin 进行打包的时候,打包命令和普通打包一样:

# mvn package

打包后会生成两个 JAR 包,提交到服务器集群时使用非 original 开头的 JAR。

4.3 我常用打包方式

方式1

<!--编译打包插件-->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin </artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>需要填写和自己建的全类名一致</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 方式2:推荐

使用maven-compiler-plugin以及maven-shade-plugin完成maven项目打包

  • maven-compiler-plugin负责项目编译;
  • maven-shade-plugin负责最终的打包操作.
    <build>
        <plugins>
            <!-- 编译java项目-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- 打包ava项目,包含所有的依赖-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.demo.project.test.CreateOrgDemo</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

maven-shade-plugin比较强大,还可以解决打包文件中的依赖冲突 

五、其他打包需求

1. 使用非Maven仓库中的Jar

通常上面两种打包能够满足大多数的使用场景。但是如果你想把某些没有被 Maven 管理 JAR 包打入到最终的 JAR 中,比如你在 resources/lib 下引入的其他非 Maven 仓库中的 JAR,此时可以使用 maven-jar-pluginmaven-dependency-plugin 插件将其打入最终的 JAR 中。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                          <!--指定 resources/lib 目录-->
                        <classpathPrefix>lib/</classpathPrefix>
                          <!--应用的主入口类-->
                        <mainClass>com.heibaiying.BigDataApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>compile</phase>
                    <goals>
                         <!--将 resources/lib 目录所有 Jar 包打进最终的依赖中-->
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                         <!--将 resources/lib 目录所有 Jar 包一并拷贝到输出目录的 lib 目录下-->
                        <outputDirectory>
                            ${project.build.directory}/lib
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

2. 排除集群中已经存在的Jar

通常为了避免冲突,官方文档都会建议你排除集群中已经提供的 JAR 包,如下:

Spark 官方文档 Submitting Applications 章节:

When creating assembly jars, list Spark and Hadoop as provided dependencies; these need not be bundled since they are provided by the cluster manager at runtime.

Strom 官方文档 Running Topologies on a Production Cluster 章节:

Then run mvn assembly:assembly to get an appropriately packaged jar. Make sure you exclude the Storm jars since the cluster already has Storm on the classpath.

按照以上说明,排除 JAR 包的方式主要有两种:

  • 对需要排除的依赖添加 <scope>provided</scope> 标签,此时该 JAR 包会被排除,但是不建议使用这种方式,因为此时你在本地运行也无法使用该 JAR 包;

  • 建议直接在 maven-assembly-pluginmaven-shade-plugin 的配置文件中使用 <exclude> 进行排除。

3. 打包Scala文件

如果你使用到 Scala 语言进行编程,此时需要特别注意 :默认情况下 Maven 是不会把 scala 文件打入最终的 JAR 中,需要额外添加 maven-scala-plugin 插件,常用配置如下:

<plugin>
    <groupId>org.scala-tools</groupId>
    <artifactId>maven-scala-plugin</artifactId>
    <version>2.15.1</version>
    <executions>
        <execution>
            <id>scala-compile</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/*.scala</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>scala-test-compile</id>
            <goals>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

参考资料

关于 Maven 各个插件的详细配置可以查看其官方文档:

关于 maven-shade-plugin 的更多配置也可以参考该博客: maven-shade-plugin 入门指南

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven是一个功能强大的项目管理工具,可以帮助我们自动化构建、编译打包项目。Maven提供了一种称为插件(plugins)的机制,可以扩展其功能。下面是一些常用Maven编译打包插件: 1. Maven Compiler Plugin:该插件用于编译Java源代码。它默认使用Java编译器进行编译,但也支持其他编译器,如Groovy等。可以通过配置插件的参数来指定编译的源码路径、目标版本等。 2. Maven Surefire Plugin:该插件用于运行项目中的单元测试。它会自动搜索项目中的测试类,并执行这些测试类中的测试方法。可以通过配置插件的参数来指定测试类的匹配规则、报告输出等。 3. Maven Javadoc Plugin:该插件用于生成项目的API文档。它会根据项目中的源代码和Javadoc注释生成HTML格式的文档,并可以通过配置插件参数来指定输出路径、是否包含私有成员等。 4. Maven Source Plugin:该插件用于生成项目的源代码包。它会将项目中的源代码打包成一个压缩文件,供其他开发人员使用。可以通过配置插件参数来指定输出路径、是否包含测试代码等。 5. Maven Jar Plugin:该插件用于将项目打包成一个可执行的JAR文件。它会将项目中的编译后的类文件、资源文件等打包到JAR文件中,并可以通过配置插件参数来指定JAR文件的名称、包含的内容等。 这些插件可以通过在项目的pom.xml文件中配置来启用和使用。例如,可以在`<build>`标签下的`<plugins>`标签中添加相应的插件配置。具体的插件配置和参数可以参考Maven官方文档或插件的官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值