【maven】-001用maven assembly插件打jar包实现依赖包归档


服务化代码层次结构


一、采用mvn生成对应的包
(1) pom.xml类型是war ,使用mvn package  自动将项目依赖的jar包打到web-inf 下的lib文件夹中
(2) pom.xml类型是 jar使用 maven的 assembly插件 会在${project}/target 文件夹下发现新生成的 {artifactId}-jar-with-dependencies.jar 这个文件

二、如何配置assembly插件
(1)在pom.xml中添加assembly 插件,详见 标红部分
< project   xmlns =   "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.0 http://maven.apache.org/maven-v4_0_0.xsd" >
      < parent   >
              < artifactId   > dubboparent   </ artifactId   >
              < groupId   > com.ai   </ groupId   >
              < version   > 0.0.1-SNAPSHOT   </ version   >
      </ parent   >
      < modelVersion   > 4.0.0   </ modelVersion   >
      < groupId   > com.ai   </ groupId   >
      < artifactId   > dubboservice   </ artifactId   >
      < version   > 0.0.1-SNAPSHOT   </ version   >
      < name   > dubboservice   </ name   >
      < url   > http://maven.apache.org   </ url   >
      < properties   >
             < project.build.sourceEncoding   > UTF-8   </ project.build.sourceEncoding   >
      </ properties   >

      < dependencies   >
              < dependency   >
                   < groupId   > com.ai   </ groupId   >
                   < artifactId   > dubbodao   </ artifactId   >
                   < version   > 0.0.1-SNAPSHOT   </ version   >
              </ dependency   >
              < dependency   >
                   < groupId   > org.apache   </ groupId   >
                   < artifactId   > zookeeper   </ artifactId   >
                   < version   > 3.4.5   </ version   >
              </ dependency   >
              < dependency   >
                   < groupId   > org.I0Itec.zkclient   </ groupId   >
                   < artifactId   > zkclient   </ artifactId   >
                   < version   > 0.1   </ version   >
              </ dependency   >
      </ dependencies   >
      < build   >
              < plugins   >
                   < plugin   >
                        < groupId   > org.apache.maven.plugins   </ groupId   >
                        < artifactId   > maven-compiler-plugin   </ artifactId   >
                        < version   > 2.3.2   </ version   >
                        < configuration   >
                              < source   > 1.6   </ source   >
                              < target   > 1.6   </ target   >
                              < encoding   > UTF-8   </ encoding   >
                        </ configuration   >
                   </ plugin   >
                   <plugin >
                      <artifactId >maven-assembly-plugin </artifactId >
                      <version >2.2-beta-5 </version >
                      <configuration >
                            <descriptors >
                                 <descriptor >src/main/assembly/src.xml </descriptor >
                            </descriptors >
                      </configuration >
                 </plugin >
              </ plugins   >
      </ build   >
</ project >
(2) 在 main/assembly 下创建 src.xml文件,其中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 >
(3)命令行输入mvn assembly:assembly
      会在 ${project}/target  文件夹下发现新生成的  {artifactId}-jar-with-dependencies.jar  这个文件
    


 (4) 下载对应的依赖包并查看

参考文章: http://blog.csdn.net/e5945/article/details/7777286

Maven Assembly Plugin是一个强大的插件,它允许你在构建过程中将项目依赖项、源代码、文档等打成一个或多个自定义的文件结构,比如传统的WAR、EAR、JAR等格式,或者创建更复杂的结构如Linux发行版风格的tar.gz文件。 当你想把外部的jar包含进你的项目部署(例如,如果你的应用需要使用第三方库),你可以通过配置`maven-assembly-plugin`来完成。首先,你需要在pom.xml文件中添加`assembly-plugin`并指定相关的配置: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.example.ApplicationMain</mainClass> </manifest> </archive> <appendAssemblyId>false</appendAssemblyId> <dependencySets> <dependencySet> <excludes> <exclude>*:sources</exclude> <exclude>*:javadoc</exclude> </excludes> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 在这个例子中,`descriptorRef="jar-with-dependencies"`指定了生成的JAR含所有项目及其依赖JAR。`<mainClass>`用于设置主入口。`dependencySets`部分告诉插件如何处理项目的依赖。 执行`mvn clean package`命令后,会在目标目录下生成一个新的含所有依赖JAR文件,方便部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾文教编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值