- 浏览: 61867 次
- 性别:
- 来自: 成都
文章分类
最新评论
在上一篇文章中,讨论到在对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还是很强大的,继续学习.....
发表评论
-
HA(高可用)技术---负载均衡
2011-10-20 09:09 2825今天抽空把一些HA(高 ... -
Maven dependency and repository
2011-10-12 10:04 780http://tech.ddvip.com/2009-01/1 ... -
maven profiles
2011-10-12 10:03 825定义Profiles 你可以把profiles定义在4个地 ... -
使用maven2 进行团队配置
2011-10-12 10:03 735对于团队来说,建立统一的开发环境是必须的,而maven能很好帮 ... -
maven 配置篇 之pom.xml
2011-10-12 10:02 690http://tech.ddvip.com/2009-01/1 ... -
maven 配置篇 之 settings.xml
2011-10-12 10:02 663maven2 比起maven1 来说, ... -
初学maven(4)-使用maven ant task实现非标准打包
2011-10-11 18:04 1123maven很强大,但是总有些事情干起来不是得心应手,没有使用a ... -
初学maven(3)-使用nexus替代artifactory作为maven私服
2011-10-11 18:02 954之前看到过一些Nexus的 ... -
初学maven(2)-maven/artifactory/m2eclipse安装全过程
2011-10-11 17:58 825前段时间研究过一下maven,中途因为工作忙搁置了一段时间,重 ... -
初学maven(1)-常见小问题集锦
2011-10-11 17:57 754http://www.bianceng.cn/Programm ... -
HttpSessionListener用法(计数器)
2011-08-12 14:00 953继上次说到Listener的功 ... -
maven仓库管理器Nexus
2011-08-12 10:53 1028关键字: naven2.0.9 nexus1.2.1 ... -
使用nexus替代artifactory作为maven私服-转
2011-08-12 10:48 1110之前看到过一些Nexus的 ... -
eclipse中使用maven插件 index不能更新
2011-08-12 10:21 2152问题产生如下: 因为单位使用了过滤,访问Internet时, ...
相关推荐
Maven-assembly-plugin是maven中针对打包任务而提供的标准插件,可以实现自定义打包。主要提供如下功能: ● 提供一个把工程依赖元素、模块、网站文档等其他文件存放到单个归档文件里。 ● 打包成指定格式分发包,...
Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip
maven-assembly-plugin-2.2-beta-5.jar
java运行依赖jar包
java运行依赖jar包
maven-deploy-plugin-2.8.2.jar
maven-jar-plugin-3.1.1.jar
eclipse-maven3-plugin插件 包含apache-maven-3.3.9和eclipse-maven3-plugin
maven是个项目管理工具,如果我们不告诉它我们的代码要使用什么样的jdk版本编译的话,它就会用maven-compiler-plugin默认的jdk版本来进行处理,这样就容易出现版本不匹配,以至于可能导致编译不通过的问题。...
maven-antrun-plugin-3.0.0.jar
maven-resources-plugin-2.4.1.jar
maven-install-plugin-2.4.jar
maven-surefire-plugin-2.22.1.jar
maven-site-plugin-3.3.jar
maven-deploy-plugin-2.7.jar
maven-notice-plugin-1.0.1.jar
maven-jxr-plugin-2.0.jar
maven-resources-plugin-3.1.0.jar
maven-jxr-plugin-1.5.jar
maven-surefire-plugin-2.7.1.jar