mavenpom配置学习笔记(一)之maven-asmbly-plugin进行打包

更新时间:2023-05-12 23:31:06 阅读: 评论:0

五一休假安排mavenpom配置学习笔记(⼀)之maven-asmbly-plugin进⾏
表里如一的近义词打包
maven-asmbly-plugin使⽤描述(拷⾃ )
The Asmbly Plugin for Maven is primarily intended to allow urs to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.
asmbly:single
简单的说,maven-asmbly-plugin 就是⽤来帮助打包⽤的,⽐如说打出⼀个什么类型的包,包⾥包括哪些内容等等。⽬前⾄少⽀持以下打包类型:zip
tar
<
tar.bz2
jar
dir
war
水明月默认情况下,打jar包时,只有在类路径上的⽂件资源会被打包到jar中,并且⽂件名是${artifactId}-${version}.jar,下⾯看看怎么⽤maven-asmbly-plugin插件来定制化打包。
⾸先需要添加插件声明:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-asmbly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<pha>package</pha>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
使⽤内置的Asmbly Descriptor
要使⽤maven-asmbly-plugin,需要指定⾄少⼀个要使⽤的asmbly descriptor ⽂件,对于当前使⽤的版本(2.4)对应的asmbly descriptor的schema定义为:,其中asmbly descriptor中⼜可以包括 component 的定义 (component 可以很⽅便的⽤于多个asmbly descriptor之间共享),component 的schema 定义在:。关于asmbly descriptor的component descriptor的更详细的说明,请见:和。
默认情况下,maven-asmbly-plugin内置了⼏个可以⽤的asmbly descriptor:
bin :类似于默认打包,会将bin⽬录下的⽂件打到包中
jar-with-dependencies :会将所有依赖都解压打包到⽣成物中
src :只将源码⽬录下的⽂件打包
project :将整个project资源打包
要查看它们的详细定义,可以到maven-asmbly-plugin-2.4.jar⾥去看,例如对应 bin 的asmbly descriptor 如下:
<asmbly xmlns="/plugins/maven-asmbly-plugin/asmbly/1.1.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/plugins/maven-asmbly-plugin/asmbly/1.1.0 /xsd/asmbly-1.1.0.xsd">
<id>bin</id>
生活中的启示
<formats>
<format&</format>
<format>tar.bz2</format>
<format>zip</format>
<fileSets>
<fileSet>
<directory>${project.badir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
弟子规书
<include>README*</include>
<include>LICENSE*</include>
学年英语
<include>NOTICE*</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.build.directory}/site</directory>
<outputDirectory>docs</outputDirectory>
</fileSet>
</fileSets>
</asmbly>
⾃定义Asmbly Descriptor
⼀般来说,内置的asmbly descriptor都不满⾜需求,这个时候就需要写⾃⼰的asmbly descriptor的实现了。先从⼀个最简单的定义开始:<?xml version='1.0' encoding='UTF-8'?>
<asmbly xmlns="/plugins/maven-asmbly-plugin/asmbly/1.1.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/plugins/maven-asmbly-plugin/asmbly/1.1.0
/xsd/asmbly-1.1.0.xsd">
<id>demo</id>
<formats>
<format>jar</format>
</formats>
<includeBaDirectory>fal</includeBaDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/class</directory>
<outputDirectory>/</outputDirectory>长篇动漫
</fileSet>
</asmbly>
这个定义很简单:
format:指定打包类型
includeBaDirectory:指定是否包含打包层⽬录(⽐如finalName是output,当值为true,所有⽂件被放在output⽬录下,否则直接放在包的根⽬录下)fileSets:指定要包含的⽂件集,可以定义多个fileSet
directory:指定要包含的⽬录
outputDirectory:指定当前要包含的⽬录的⽬的地要使⽤这个asmbly descriptor,需要如下配置:
<configuration>
<finalName>demo</finalName>
<descriptors>
<descriptor>l</descriptor>
</descriptors>
<outputDirectory>output</outputDirectory>
</configuration>
最后会⽣成⼀个demo-demo.jar ⽂件在⽬录 output 下,其中前⼀个demo来⾃finalName,后⼀个demo来⾃asmbly descriptor中的id,其中的内容和默认的打包出来的jar类似。
如果只想有finalName,则增加配置:
<appendAsmblyId>fal</appendAsmblyId>
添加⽂件
上⾯演⽰了添加所有编译后的资源,同样的可以增加其他资源,例如想添加当前⼯程⽬录下的某个⽂件 b.txt ,在asmbly descriptor的asmbly结点下增加<files>
<file>
<source&</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
这⾥⽤到了 files 元素类型,可以想象 fileSets 下的结点都是针对⽂件夹的;files 下的结点都是针对⽂件的。
也可以改变打包后的⽂件名,例如上⾯的 b.txt ,希望打包后的名字为 b.txt.bak,只需要在file ⾥添加以下配置:
<destName&bak</destName>
排除⽂件
在fileSet⾥可以使⽤includes 和 excludes来更精确的控制哪些⽂件要添加,哪些⽂件要排除。例如要排除某个⽬录下所有的txt⽂件:
<fileSet>
<directory>${project.build.directory}/class</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>**/*.txt</exclude>
</excludes>
</fileSet>
或者某个⽬录下只想 .class ⽂件:
<fileSet>
<directory>${project.build.directory}/class</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/*.class</include>
</includes>
</fileSet>
添加依赖
如果想把⼀些依赖库打到包⾥,可以⽤ dependencySets 元素,例如最简单的,把当前⼯程的所有依赖都添加到包⾥:
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
</dependencySet>
</dependencySets>
在asmbly下添加以上配置,则当前⼯程的依赖和⼯程本⾝⽣成的jar都会被打包进来。
如果要排除⼯程⾃⾝⽣成的jar,则可以添加
<uProjectArtifact>fal</uProjectArtifact>
unpack参数可以控制依赖包是否在打包进来时是否解开,例如解开所有包,添加以下配置:
<unpack>true</unpack>
和 fileSet ⼀样,可以使⽤ excludes 和 includes 来更详细的控制哪些依赖需要打包进来;另外
uProjectAttachments,uTransitiveDependencies,uTransitiveFiltering等参数可以对间接依赖、传递依赖进⾏控制。
其他选项
moduleSets:当有⼦模块时候⽤
repositories:想包含库的时候⽤
containerDescriptorHandlers:可以进⾏⼀些合并,定义ArtifactHandler之类的时候可以⽤,(可以参考:)
componentDescriptors:如上所述,可以包含⼀些componentDescriptor定义,这些定义可以被多个a
smbly共享
Asmbly Plugin更多配置
上⾯已经看到了⼀些Asmbly Plugin本⾝的配置,例如 finalName, outputDirectory, appendAsmblyId和descriptors等,除了这些还有其他的⼀些可配置参数,参见:,其中某些参数会覆盖在asmbly descriptor中的参数。有⼀个⽐较有⽤的参数是: archive,它的详细配置在:。下⾯介绍⼀些archive的⽤法。
指定Main-Class
archive的⼀个重要⽤处就是配置⽣成的MANIFEST.MF⽂件。默认会⽣成⼀个MANIFEST.MF⽂件,不过这个⽂件默认值没什么意义。如果想指定⽣成jar的Main-Class,可以如下配置:
<archive>
<manifest>
<mainClass>demo.DemoMain</mainClass>
</manifest>
</archive>
添加MANIFEST项
除了可以指定Main-Class外,还可以添加任意项。⽐如在OSGI bundle的MANIFEST.MF定义⾥就有很多⽤来定义bundle的属性的项,如Import-Package,Export-Package等等。要添加项,可以使⽤如下配置:
<archive>
<manifestEntries>大脑反应
<Import-Package&l.ws.*</Import-Package>
</manifestEntries>
</archive>
指定MANIFEST.MF⽂件
还可以直接指定MANIFEST.MF⽂件。如下:
<archive>
<manifestFile>META-INF/MANIFEST.MF</manifestFile>
</archive>

本文发布于:2023-05-12 23:31:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/605879.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:打包   依赖   定义   添加   配置   例如
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图