用maven assembly插件打jar包实现依赖包归档
来源:互联网 发布:广告宣传录音软件下载 编辑:程序博客网 时间:2024/06/05 13:35
如果你的项目使用maven构建的话,当项目要上线,部署到服务器上去的时候或许会碰见这样的问题。问题就是,服务器上没有maven的环境,也就是说,项目所依赖到的那些仓库(repository)中的jar包你需要单独提取出来上传到服务器中去。
我知道pom类型如果是war的话,在使用mvn package 的命令就能自动将项目依赖的jar包打到web-inf 下的lib文件夹中。但是,如果pom类型为jar的话,当你调用mvn package命令,执行过程中不会将依赖的第三方包提取出来。
以前,我的做法是,等到项目要上线的时候将pom类型改成war,然后执行一下mvn package命令,这样先把所以依赖到的包提取出来,然后再把pom类型改成jar,这样虽然能完成任务,但是,总感觉这样的做法比较拙劣。
所以最近寻找了一下有没有更好的办法,其实很简单,就是使用 maven的 assembly插件,在pom.xml 添加如下插件:
- <project>
- [...]
- <build>
- [...]
- <plugins>
- <plugin>
- <!-- NOTE: We don't need a groupId specification because the group is
- org.apache.maven.plugins ...which is assumed by default.
- -->
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-5</version>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- [...]
- </project>
- <project>
- [...]
- <build>
- [...]
- <plugins>
- <plugin>
- <!-- NOTE: We don't need a groupId specification because the group is
- org.apache.maven.plugins ...which is assumed by default.
- -->
- <artifactId>maven-assembly-plugin</artifactId>
- <version>2.2-beta-5</version>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- </configuration>
- [...]
- </project>
然后在命令行上输入:
mvn assembly:assembly
你会在${project}/target 文件夹下发现新生成的 {artifactId}-jar-with-dependencies.jar 这个文件
在上面的这个 命令执行的过程中,maven会将jar包所依赖的包导出,并且解压(unpackage),一并放在
这个{artifactId}-jar-with-dependencies.jar 包中,这样对于程序的部署人员来说很方便,哪怕你的项目依赖了再多的第三方包,在部署的时候都会合并到一个assembly中。
但是问题又来了,在部署的过程中我们往往还是希望,将各个第三方包单独部署,比如ibatis归ibatis包,spring包归spring包,这样以后单独为某个第三方包升级也比较方便。
其实也有解决办法:
之前使用
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
这个jar-with-dependencies是assembly预先写好的一个,组装描述引用(assembly descriptor)我们来看一下这个定义这个组装描述(assemly descriptor)的xml文件
- <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
- <id>jar-with-dependencies</id>
- <formats>
- <format>jar</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <dependencySet>
- <unpack>true</unpack>
- <scope>runtime</scope>
- </dependencySet>
- </dependencySets>
- <fileSets>
- <fileSet>
- <directory>${project.build.outputDirectory}</directory>
- </fileSet>
- </fileSets>
- </assembly>
- <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
- <id>jar-with-dependencies</id>
- <formats>
- <format>jar</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <dependencySet>
- <unpack>true</unpack>
- <scope>runtime</scope>
- </dependencySet>
- </dependencySets>
- <fileSets>
- <fileSet>
- <directory>${project.build.outputDirectory}</directory>
- </fileSet>
- </fileSets>
- </assembly>
其实只要将上面这个xml文件中的
- <dependencySet>
- <unpack>true</unpack>
- <scope>runtime</scope>
- </dependencySet>
- <dependencySet>
- <unpack>true</unpack>
- <scope>runtime</scope>
- </dependencySet>
改成:
- <dependencySet>
- <unpack>false</unpack>
- <scope>runtime</scope>
- </dependencySet>
- <dependencySet>
- <unpack>false</unpack>
- <scope>runtime</scope>
- </dependencySet>
在 main/assembly 下创建 src.xml文件,将刚才修改过的内用写入文件中,内容为:
- <assembly
- xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
- <id>jar-with-dependencies</id>
- <formats>
- <format>jar</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <dependencySet>
- <unpack>false</unpack>
- <scope>runtime</scope>
- </dependencySet>
- </dependencySets>
- <fileSets>
- <fileSet>
- <directory>${project.build.outputDirectory}</directory>
- </fileSet>
- </fileSets>
- </assembly>
- <assembly
- xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
- <id>jar-with-dependencies</id>
- <formats>
- <format>jar</format>
- </formats>
- <includeBaseDirectory>false</includeBaseDirectory>
- <dependencySets>
- <dependencySet>
- <unpack>false</unpack>
- <scope>runtime</scope>
- </dependencySet>
- </dependencySets>
- <fileSets>
- <fileSet>
- <directory>${project.build.outputDirectory}</directory>
- </fileSet>
- </fileSets>
- </assembly>
将之前pom.xml 中的plugin改成如下:
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptors>
- <descriptor>src/main/assembly/src.xml</descriptor>
- </descriptors>
- </configuration>
- </plugin>
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptors>
- <descriptor>src/main/assembly/src.xml</descriptor>
- </descriptors>
- </configuration>
- </plugin>
再执行
mvn assembly:assembly
这样在target文件夹中就会看见新创建出来的{artifactId}-jar-with-dependencies.jar 这个jar包
里面会将项目所依赖的所有第三方包全部打包在里面,如下图:
- 【maven】-001用maven assembly插件打jar包实现依赖包归档
- 用maven assembly插件打jar包实现依赖包归档
- 用maven assembly插件打jar包实现依赖包归档
- 用maven assembly插件打jar包实现依赖包归档
- 用maven assembly插件打jar包实现依赖包归档
- 用maven assembly插件打jar包实现依赖包归档
- maven 打jar包 mvn assembly:assembly
- maven-assembly-plugin 打jar包
- 通过maven插件assembly打war包
- maven打jar包插件
- eclipse 用maven打jar包(依赖包一起)
- maven 打源码包,maven 打带依赖 jar包
- [问题记录]MAVEN打依赖jar包
- maven打可执行jar包及依赖jar包
- maven打jar包时添加依赖的jar包
- maven打可执行jar包的插件
- maven-compiler-plugin插件打jar包
- maven设置打jar包并引入依赖包
- 6.6 某银行金融授权系统公司面试
- 《暗时间》读书笔记与读后感
- Django - Django框架 简介
- C++ builder 文件操作
- hdu1285+hdu2467(拓扑排序)
- 用maven assembly插件打jar包实现依赖包归档
- 项目管理能力提升四要素
- 黑马程序员---网络编程
- Oracle基础知识(二十七) - PL/SQL修改字符编码
- Struts2 用 s:if test 判断属性和字符串相等时 注意双引号和单引号的使用 以及%{} ${}%{#}的使用
- 操作系统 架构
- 黑马程序员 Java基础 I/O技术
- c# 事件
- Doing Enterprise Architecture- Part 2- Implementation, Projects & Governance