网站快速刷排名工具,什么是互联网营销,东莞常平邮政编码查询,万装网装修平台文章目录 Pre概述Jar 打包方式_maven-jar-pluginOverview使用官方文档 Shade 打包方式_maven-shade-pluginOverview使用将部分jar包添加或排除将依赖jar包内部资源添加或排除自动将所有不使用的类排除将依赖的类重命名并打包进来 #xff08;隔离方案#xff09;修改包的后缀… 文章目录 Pre概述Jar 打包方式_maven-jar-pluginOverview使用官方文档 Shade 打包方式_maven-shade-pluginOverview使用将部分jar包添加或排除将依赖jar包内部资源添加或排除自动将所有不使用的类排除将依赖的类重命名并打包进来 隔离方案修改包的后缀名 官方文档 Assembly 打包方式_maven-assembly-pluginOverview使用官方文档 Pre
Maven - 统一构建规范Maven 插件管理最佳实践 概述
Maven 提供了多种打包方式其中常见的包括三种jar、shade、assembly。下面是它们的详细比较 Jar 打包方式 描述 这是最常见的打包方式它创建一个标准的Java JAR文件。优点 简单直接适用于大多数简单项目。缺点 不能包含项目的依赖如果项目有外部依赖用户必须手动将它们添加到类路径中。 Shade 打包方式 描述 Maven Shade插件允许创建一个可执行的JAR文件其中包含所有依赖。优点 生成一个独立的可执行JAR无需用户手动添加依赖。缺点 可能会导致JAR文件较大不适合所有项目。 Assembly 打包方式 描述 Maven Assembly插件提供了一种更灵活的打包方式允许创建各种自定义分发包。优点 可以根据项目的需要创建定制的分发包非常灵活。缺点 配置相对复杂适用于需要高度定制化的项目。
总结
Jar方式适用于简单项目但对于有依赖的项目需要手动处理依赖 ; 默认的打包方式用来打普通的project JAR包。Shade方式生成可执行JAR但可能导致文件较大; 用来打可执行jar包也就是所谓的fat JAR包。Assembly方式最灵活可以根据项目需求创建定制分发包 ; 自定义的打包结构也可以定制依赖项等。 Jar 打包方式_maven-jar-plugin
Overview
使用maven-jar-plugin插件 默认的打包方式用来打普通的project JAR包 . 使用
buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-jar-plugin/artifactIdversion3.1.0/versionconfigurationarchivemanifest!-- 指定入口函数 --mainClass类的全路径名称/mainClass!-- 是否添加依赖的jar路径配置 --addClasspathtrue/addClasspath!-- 依赖的jar包存放位置和生成的jar放在同一级目录下 --classpathPrefixlib//classpathPrefix/manifest/archive!-- 不打包com.artisan.excludes下面的所有类 --excludescom/artisan/excludes/*/excludes/configuration/plugin/plugins
/build上面配置使用这个 jar包的时候就需要在它同一级的创建一个lib目录来存放。 可以使用includes或excludes选择的打包某些内容
官方文档
https://maven.apache.org/shared/maven-archiver/examples/classpath.html Shade 打包方式_maven-shade-plugin
Overview
插件使用maven-shade-plugin插件
maven-shade-plugin提供了两大基本功能
将依赖的jar包打包到当前jar包常规打包是不会将所依赖jar包打进来的对依赖的jar包进行重命名用于类的隔离
使用
maven-shade-plugin 只存在一个goal shade:shade需要将其绑定到 phase package 上
buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-shade-plugin/artifactIdversion3.1.1/versionconfiguration!-- put your configurations here --/configurationexecutionsexecutionphasepackage/phasegoalsgoalshade/goal/goals/execution/executions/plugin/plugins
/build然后在当前pom文件所在路径下执行打包命令mvn clean package 将部分jar包添加或排除
project...buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-shade-plugin/artifactIdversion3.1.1/versionexecutionsexecutionphasepackage/phasegoalsgoalshade/goal/goalsconfigurationartifactSetexcludesexcludejmock:*/excludeexclude*:xml-apis/excludeexcludeorg.apache.maven:lib:tests/excludeexcludelog4j:log4j:jar:/exclude/excludesincludesincludejunit:junit/include/includes/artifactSet/configuration/execution/executions/plugin/plugins/build...
/projectjar包以groupId:artifactId[[:type]:classifier]的形式表示1.3版本后插件支持通配符 ‘*’ and ‘?’ 将依赖jar包内部资源添加或排除
project...buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-shade-plugin/artifactIdversion3.1.1/versionexecutionsexecutionphasepackage/phasegoalsgoalshade/goal/goalsconfigurationfiltersfilterartifactjunit:junit/artifactincludesincludejunit/framework/**/includeincludeorg/junit/**/include/includesexcludesexcludeorg/junit/experimental/**/excludeexcludeorg/junit/runners/**/exclude/excludes/filterfilterartifact*:*/artifactexcludesexcludeMETA-INF/*.SF/excludeexcludeMETA-INF/*.DSA/excludeexcludeMETA-INF/*.RSA/exclude/excludes/filter/filters/configuration/execution/executions/plugin/plugins/build...
/project自动将所有不使用的类排除
project...buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-shade-plugin/artifactIdversion3.1.1/versionexecutionsexecutionphasepackage/phasegoalsgoalshade/goal/goalsconfigurationminimizeJartrue/minimizeJar/configuration/execution/executions/plugin/plugins/build...
/project将依赖的类重命名并打包进来 隔离方案
project...buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-shade-plugin/artifactIdversion3.1.1/versionexecutionsexecutionphasepackage/phasegoalsgoalshade/goal/goalsconfigurationrelocationsrelocationpatternorg.codehaus.plexus.util/patternshadedPatternorg.shaded.plexus.util/shadedPatternexcludesexcludeorg.codehaus.plexus.util.xml.Xpp3Dom/excludeexcludeorg.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...buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-shade-plugin/artifactIdversion3.1.1/versionexecutionsexecutionphasepackage/phasegoalsgoalshade/goal/goalsconfigurationshadedArtifactAttachedtrue/shadedArtifactAttachedshadedClassifierNamejackofall/shadedClassifierName !-- Any name that makes sense --/configuration/execution/executions/plugin/plugins/build...
/projectInvalid 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时将这些排除掉不再进行完成性校验如下所示
configurationfiltersfilterartifact*:*/artifactexcludesexcludeMETA-INF/*.SF/excludeexcludeMETA-INF/*.DSA/excludeexcludeMETA-INF/*.RSA/exclude/excludes/filter/filters
/configurationhttps://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html 官方文档
https://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html Assembly 打包方式_maven-assembly-plugin
Overview
使用maven-assembly-plugin插件 。
日常使用比较多的是maven-assembly-plugin插件
例如大数据项目中往往有很多shell脚本、SQL脚本、.properties及.xml配置项等采用assembly插件可以让输出的结构清晰而标准化 使用
首先在pom文件添加以下内容
buildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-assembly-plugin/artifactIdversion${maven-assembly-plugin.version}versionexecutionsexecutionidmake-assembly/id!-- 绑定到package生命周期 --phasepackage/phasegoals!-- 只运行一次 --goalsingle/goal/goals/execution/executionsconfiguration!-- 配置描述符文件 --descriptorsrc/main/assembly/assembly.xml/descriptor!-- 也可以使用Maven预配置的描述符descriptorRefsdescriptorRefjar-with-dependencies/descriptorRef/descriptorRefs --/configuration/plugin/plugins
/build然后编写描述符文件
assemblyidassembly/idformatsformattar.gz/format/formatsincludeBaseDirectorytrue/includeBaseDirectoryfileSetsfileSetdirectorysrc/main/bin/directoryincludesinclude*.sh/include/includesoutputDirectorybin/outputDirectoryfileMode0755/fileMode/fileSetfileSetdirectorysrc/main/conf/directoryoutputDirectoryconf/outputDirectory/fileSetfileSetdirectorysrc/main/sql/directoryincludesinclude*.sql/include/includesoutputDirectorysql/outputDirectory/fileSetfileSetdirectorytarget/classes//directoryincludesinclude*.properties/includeinclude*.xml/includeinclude*.txt/include/includesoutputDirectoryconf/outputDirectory/fileSet/fileSetsfilesfilesourcetarget/${project.artifactId}-${project.version}.jar/sourceoutputDirectory./outputDirectory/file/filesdependencySetsdependencySetunpackfalse/unpackscoperuntime/scopeoutputDirectorylib/outputDirectory/dependencySet/dependencySets
/assembly字段解析formats是assembly插件支持的打包文件格式有zip、tar、tar.gz、tar.bz2、jar、war。可以同时定义多个formats。id是添加到打包文件名的标识符用来做后缀。比如说如果按上面的配置生成的文件就是artifactId-{artifactId}-artifactId-{version}-assembly.tar.gzfileSets/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