Maven - 统一构建规范:Maven 插件管理最佳实践
Maven 提供了多种打包方式,其中常见的包括三种:jar、shade、assembly。下面是它们的详细比较:
Jar 打包方式:
Shade 打包方式:
Assembly 打包方式:
总结 :
使用maven-jar-plugin
插件, 默认的打包方式,用来打普通的project JAR包 .
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 指定入口函数 -->
<mainClass>类的全路径名称</mainClass>
<!-- 是否添加依赖的jar路径配置 -->
<addClasspath>true</addClasspath>
<!-- 依赖的jar包存放位置,和生成的jar放在同一级目录下 -->
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<!-- 不打包com.artisan.excludes下面的所有类 -->
<excludes>com/artisan/excludes/*
上面配置使用这个 jar包的时候就需要在它同一级的创建一个lib目录来存放。 可以使用includes或excludes选择的打包某些内容
https://maven.apache.org/shared/maven-archiver/examples/classpath.html
插件:使用maven-shade-plugin插件
maven-shade-plugin提供了两大基本功能:
maven-shade-plugin 只存在一个goal shade:shade,需要将其绑定到 phase package 上
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后在当前pom文件所在路径下,执行打包命令:mvn clean package
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>3.1.1version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<artifactSet>
<excludes>
<exclude>jmock:*exclude>
<exclude>*:xml-apisexclude>
<exclude>org.apache.maven:lib:testsexclude>
<exclude>log4j:log4j:jar:exclude>
excludes>
<includes>
<include>junit:junitinclude>
includes>
artifactSet>
configuration>
execution>
executions>
plugin>
plugins>
build>
...
project>
groupId:artifactId[[:type]:classifier]
的形式表示‘*’ and ‘?’
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>3.1.1version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<filters>
<filter>
<artifact>junit:junitartifact>
<includes>
<include>junit/framework/**include>
<include>org/junit/**include>
includes>
<excludes>
<exclude>org/junit/experimental/**exclude>
<exclude>org/junit/runners/**exclude>
excludes>
filter>
<filter>
<artifact>*:*artifact>
<excludes>
<exclude>META-INF/*.SFexclude>
<exclude>META-INF/*.DSAexclude>
<exclude>META-INF/*.RSAexclude>
excludes>
filter>
filters>
configuration>
execution>
executions>
plugin>
plugins>
build>
...
project>
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>3.1.1version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<minimizeJar>trueminimizeJar>
configuration>
execution>
executions>
plugin>
plugins>
build>
...
project>
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>3.1.1version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<relocations>
<relocation>
<pattern>org.codehaus.plexus.utilpattern>
<shadedPattern>org.shaded.plexus.utilshadedPattern>
<excludes>
<exclude>org.codehaus.plexus.util.xml.Xpp3Domexclude>
<exclude>org.codehaus.plexus.util.xml.pull.*exclude>
excludes>
relocation>
relocations>
configuration>
execution>
executions>
plugin>
plugins>
build>
...
project>
将“org.codehaus.plexus.util
”重命名为“org.shaded.plexus.util
”,原始jar包中的“org.codehaus.plexus.util.xml.Xpp3Dom
”和“org.codehaus.plexus.util.xml.pull
”不会被重命名到目的包中;
默认会生成一个Jar包和一个以 “-shaded”为结尾的uber-jar包,可以通过配置来指定uber-jar的后缀名。
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>3.1.1version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<shadedArtifactAttached>trueshadedArtifactAttached>
<shadedClassifierName>jackofallshadedClassifierName>
configuration>
execution>
executions>
plugin>
plugins>
build>
...
project>
Invalid signature file digest for Manifest main attributes
原因:有些jar包生成时,会 使用jarsigner生成文件签名(完成性校验),分为两个文件存放在META-INF目录下:
a signature file, with a .SF extension;
a signature block file, with a .DSA, .RSA, or .EC extension;
需要在生成 uber-jar时,将这些排除掉,不再进行完成性校验,如下所示:
<configuration>
<filters>
<filter>
<artifact>*:*artifact>
<excludes>
<exclude>META-INF/*.SFexclude>
<exclude>META-INF/*.DSAexclude>
<exclude>META-INF/*.RSAexclude>
excludes>
filter>
filters>
configuration>
https://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html
https://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html
使用maven-assembly-plugin
插件 。
日常使用比较多的是maven-assembly-plugin
插件
例如:大数据项目中往往有很多shell脚本、SQL脚本、.properties及.xml配置项等,采用assembly插件可以让输出的结构清晰而标准化
首先在pom文件添加以下内容:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-assembly-pluginartifactId>
<version>${maven-assembly-plugin.version}<version>
<executions>
<execution>
<id>make-assemblyid>
<phase>packagephase>
<goals>
<goal>singlegoal>
goals>
execution>
executions>
<configuration>
<descriptor>src/main/assembly/assembly.xmldescriptor>
configuration>
plugin>
plugins>
build>
然后编写描述符文件:
<assembly>
<id>assemblyid>
<formats>
<format>tar.gzformat>
formats>
<includeBaseDirectory>trueincludeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bindirectory>
<includes>
<include>*.shinclude>
includes>
<outputDirectory>binoutputDirectory>
<fileMode>0755fileMode>
fileSet>
<fileSet>
<directory>src/main/confdirectory>
<outputDirectory>confoutputDirectory>
fileSet>
<fileSet>
<directory>src/main/sqldirectory>
<includes>
<include>*.sqlinclude>
includes>
<outputDirectory>sqloutputDirectory>
fileSet>
<fileSet>
<directory>target/classes/directory>
<includes>
<include>*.propertiesinclude>
<include>*.xmlinclude>
<include>*.txtinclude>
includes>
<outputDirectory>confoutputDirectory>
fileSet>
fileSets>
<files>
<file>
<source>target/${project.artifactId}-${project.version}.jarsource>
<outputDirectory>.outputDirectory>
file>
files>
<dependencySets>
<dependencySet>
<unpack>falseunpack>
<scope>runtimescope>
<outputDirectory>liboutputDirectory>
dependencySet>
dependencySets>
assembly>
字段 | 解析 |
---|---|
formats | 是assembly插件支持的打包文件格式,有zip、tar、tar.gz、tar.bz2、jar、war。可以同时定义多个formats。 |
id | 是添加到打包文件名的标识符,用来做后缀。比如说,如果按上面的配置,生成的文件就是artifactId-{artifactId}-artifactId-{version}-assembly.tar.gz |
fileSets/fileSet | 用来设置一组文件在打包时的属性。 |
directory | 源目录的路径。 |
includes/excludes | 设定包含或排除哪些文件,支持通配符。 |
fileMode | 指定该目录下的文件属性,采用Unix八进制描述法,默认值是064。 |
outputDirectory | 生成目录的路径。 |
files/file | 与fileSets大致相同,不过是指定单个文件,并且还可以通过destName属性来设置与源文件不同的名称。 |
dependencySets/dependencySet | 用来设置工程依赖文件在打包时的属性,也与fileSets大致相同。 |
dependencySet-unpack | 布尔值,false表示将依赖以原来的JAR形式打包,true则表示将依赖解成*.class文件的目录结构打包。 |
dependencySet-scope | 表示符合哪个作用范围的依赖会被打包进去。compile与provided都不用管,一般是写runtime。 |
按照以上配置打包好后,将.tar.gz文件上传到服务器,解压之后就会得到bin、conf、lib等规范化的目录结构,十分方便。
https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html