在对maven
的机制不熟悉的情况下,为了实现自己需要的打包格式而使用maven
ant task
以maven + ant
的方式来实现非标准打包,而现在要介绍的是maven
中针对打包任务而提供的标准插件:assembly plugin
。
依然以上文(初学maven(4)-
使用maven ant task
实现非标准打包)的项目为例,要打包的程序如下:
demo1
|____lib
|_____demo1.jar
|_____*****.jar
|_____*****.jar
|____config
|_____*****.properties
|_____*****.xml
|____log
|_____*****.log
|____run.bat
|____run.sh
类似的建立java
项目,
文件结构如下:
demo1
|____src/main/java
|____src/main/config
|____src/main/bin
|____src/main/resources
|____src/main/assemble
|____src/test/java
|____src/test/resources
|____target
|____pom.xml
除开增加了src/main/assemble
目录和没有ant
的build
文件外,其他内容完全一样:其中src/main/java
下放java
代 码;src/main/resources
下放一个*.properties
文件,这个资源文件是打包到 jar
中,内容打包之后不需要改变的。src/main/config
下放一个标准的log4j.xml
,这个是有在安装运行前临时修改的需要的。src /main/bin
下放置可执行文件。
assembly plugin
的使用方式比较简单,主要有:
1.
修改pom.xml
pom.xml
中设置如下:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!--notappendassemblyidinreleasefilename-->
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assemble/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
其中<artifactId>maven-assembly-plugin</artifactId>
的maven-assembly-plugin
是这个插件的标准命名,在maven2.0.*
中带的默认版本是
appendAssemblyId
属性控制是否在生成的打包文件的文件名中包含assembly id
。
descriptor
属性指定maven-assembly-plugin
的配置文件,当然我设置的是src/main/assemble/package.xml.
容许使用多个,功能强大当然用法也复杂,对于简单情况一个足矣。
execution
的设置是为了将maven-assembly-plugin
继承到标准的maven
打包过程中,这样在运行maven-package
时就会执行maven-assembly-plugin
的操作,从而实现我们需要的自定义打包。
2. assemble descriptor file
我的src/main/assemble/package.xml
内容如下:
<assemblyxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/assembly-1.0.0.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
详细的语法不介绍了,请参考官方指南,有非常详尽的说明:Assembly Descriptor Format
reference
简单解释一下:
1) format
format=zip
设置打包的最终文件格式为zip.
支持的其他格式还有gz,tar,tar.gz,tar.bz2
。
2)
fileset
<fileSet>
<directory>src/main/bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
将src/main/bin
目录下的文件打包到根目录(/)
下.
<fileSet>
<directory>src/main/config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
将src/main/config
目录下的文件打包到config
下.
3) dependencySets
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
将scope
为runtime
的依赖包打包到lib
目录下。
总结一下,pom.xml
中引入maven-assembly-plugin
,然后assemble descriptor file
按需设置,最后在eclipse
中执行Run As -> Maven package
,在target
目录下就会出现***.zip
文件,里面的格式和要求的完全一致。
够简单明了吧?感觉比使用maven ant task
要轻快不少,看来maven
还是很强大的,继续学习......
分享到:
相关推荐
Maven-assembly-plugin是maven中针对打包任务而提供的标准插件,可以实现自定义打包。主要提供如下功能: ● 提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。 ● 打包成指定格式分发包,...
Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip
The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable...
通过自定义plugin插件实现了javascript与Android的交互。
主要介绍了Maven3种打包方式中maven-assembly-plugin的使用,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
maven-assembly-plugin-2.2-beta-5.jar
Qt Creator plugin动手实践(2)自己动手写qt creator插件,实现自定义工具栏按钮
博文链接:https://lengjing.iteye.com/blog/2259212
java运行依赖jar包
java运行依赖jar包
maven打包dubbo服务接口(maven-assembly-plugin)
自定义plugin插件,实现javascript与Android的交互
Android Studio 自定义 Gradle Plugin
Maven的组装插件的例子 该项目包含maven-assembly-plugin用法的示例阅读 yeoman generator(java)生成的项目
maven plugin打包下载
malihu-custom-scrollbar-plugin-master 自定义滚动条样式,可自定义滚动条颜色,长度,滚动条框的背景色等。
自定义科尔多瓦插件自定义 Cordova 插件安装sudo phonegap local plugin add https://github.com/bensonnjonjo/custom-cordova-plugin.git 卸载sudo phonegap local plugin remove de.appplant.cordova.plugin....
maven安装与配置 确保已安装JDK。Maven 3.9+版本需要JDK 8或更高版本。可以通过输入java -version来检查JDK是否安装以及其版本。 下载Maven。访问Maven官网下载最新版本,例如3.9.1。对于Windows用户,通常下载...
NULL 博文链接:https://dreamzhong.iteye.com/blog/1128392
assembly插件根据不同环境打包成tar.gz或者zip 将spring boot项目中的配置文件提取到外部config目录中 将spring boot项目中的启动jar包移动到boot目录中 将spring boot项目中的第三方依赖jar包移动到外部lib目录中 ...