maven 打包,打包依赖,并只打某个包下的依赖

1、先导出一个个maven项目下的某些包的源文件到target下的dubbo目录

注意:maven-jar-plugin插件智能打包本项目下的代码(自己写的代码)

<plugin>
				<artifactId>maven-jar-plugin</artifactId>
				<executions>
					<execution>
					
						<id>dubbo-service-model-export</id>
						<goals>
							<goal>jar</goal>
						</goals><!-- 打包的后缀 -->
						<phase>package</phase><!-- life的多个阶段 ,预打包 -->
						<configuration>
						<outputDirectory>target/dubbo</outputDirectory>
						<classifier>dubbo-service-model-export</classifier><!--dubbo-service-model-export-->
							<includes><!-- 引入 路径 -->
								<include>**/model/**</include>
								<include>**/service/**</include>
								<include>**/export/**</include>
							</includes>
							<excludes>
							<exclude>**/impl/**</exclude>
							</excludes>
						</configuration>
					</execution>
					<execution>
						<id>all</id>
						<goals>
							<goal>jar</goal>
						</goals>
						<phase>package</phase>
						<configuration>
							<classifier>all</classifier><!-- ***-all.jar -->
						<!-- 	<excludes>排除
								<exclude>**/model/**</exclude>
							</excludes>
							<includes>引入
								<include>**/impl/**</include>
							</includes> -->
						</configuration>
					</execution>
					
				</executions>
			</plugin>




2、使用maven-assembly-plugin插件打包各个依赖包,或者把某些目录下的包打包到一起

<plugin>  
                <artifactId>maven-assembly-plugin</artifactId>  <!-- 此文件为此插件的配置文件,此位置为项目根目录下-->
                <version>2.2-beta-5</version>  
                <configuration>  
                    <finalName>app</finalName>  
                    <attach>false</attach>  
                    <descriptors>  
                        <descriptor>assembly.xml</descriptor>  
                    </descriptors>  
                </configuration>  
                <executions>  
                    <execution>  
                        <id>make-assembly</id>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>single</goal>  
                        </goals>  
                    </execution>  
                </executions>  
            </plugin> 

3、编写 assembly.xml文件,放下项目根目录下

<?xml version="1.0" encoding="UTF-8"?>  
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.1 http://maven.apache.org/xsd/assembly-1.1.1.xsd"  
>  
    <formats>  
        <format>zip</format>  
        <!-- format>tar.gz</format -->  
    <!-- format>dir</format -->  
    </formats>  
    <dependencySets>  
         
        <dependencySet>  
            <unpack>false</unpack>  
            <outputDirectory>lib</outputDirectory>  
            <useTransitiveFiltering>true</useTransitiveFiltering>  
            <useStrictFiltering>true</useStrictFiltering>  
            <fileMode>666</fileMode>  
            <includes>  
                <include>com.gooddeep.dev:gooddeep-service</include>  
           <!--      <include>com.gooddeep.dev:dev-redis</include>  
                <include>com.gooddeep.dev:dev-elasticsearch</include>  
                <include>com.gooddeep.dev:dev-mongodb</include>  
                <include>com.gooddeep.dev:gooddeep-service</include>  --> 
            </includes>  
        </dependencySet>  
    </dependencySets>  
    <fileSets>  
        <fileSet>  
            <directory>/home/lhy/Workspaces/MyEclipse_data/gooddeep-dev/dev-core/target/dubbo</directory>  
            <outputDirectory>dubbo/</outputDirectory>  
            <filtered>false</filtered>  
        </fileSet> 
         <fileSet>  
            <directory>/home/lhy/Workspaces/MyEclipse_data/gooddeep-dev/dev-elasticsearch/target/dubbo</directory>  
            <outputDirectory>dubbo/</outputDirectory>  
            <filtered>false</filtered>  
        </fileSet>
         <fileSet>  
            <directory>/home/lhy/Workspaces/MyEclipse_data/gooddeep-dev/dev-mongodb/target/dubbo</directory>  
            <outputDirectory>dubbo/</outputDirectory>  
            <filtered>false</filtered>  
        </fileSet>
         <fileSet>  
            <directory>/home/lhy/Workspaces/MyEclipse_data/gooddeep-dev/dev-redis/target/dubbo</directory>  
            <outputDirectory>dubbo/</outputDirectory>  
            <filtered>false</filtered>  
        </fileSet>
         <fileSet>  
            <directory>/home/lhy/Workspaces/MyEclipse_data/gooddeep-service/target/dubbo</directory>  
            <outputDirectory>dubbo/</outputDirectory>  
            <filtered>false</filtered>  
        </fileSet> 
    </fileSets>  
</assembly>  




4、网络学习


使用一个简单的基于spring框架的demo来做程序示例,来介绍maven assembly插件的使用方法。
项目中的代码目录如下:


在以上代码目录中,assembly目录下为打包的描述文件,下面会详细介绍该文件,bin目录下为启动脚本以及readme等文件,main下为maven标准中建立的文件,java代码以及配置文件位于该目录下。
打包完成后压缩包目录如下:


打包完成后,我们可以看到bin目录来存放启动脚本等文件,config为配置文件,lib下为运行时依赖的jar包。
使用maven assembly插件需要在pom文件中配置,添加这个插件

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>make-zip</id>
                        <!-- 绑定到package生命周期阶段上 -->
                        <phase>package</phase>
                        <goals>
                            <!-- 绑定到package生命周期阶段上 -->
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors> <!--描述文件路径-->
                                <descriptor>src/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

其中execution节点,我们配置了执行maven assembly插件的一些配置,descriptor节点配置指向assembly.xml的路径。
在assembly.xml配置了,我们打包的目录以及相应的设置

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}\src\main\resources</directory>
            <outputDirectory>\</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}\src\bin</directory>
            <outputDirectory>\bin</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <!-- 将scope为runtime的依赖包打包到lib目录下。 -->
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets></assembly>

assembly.xml的配置项非常多,可以参考http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
以上只是用了很少的一部分。
format设置包输出的格式,以上格式设置的为zip格式,目前还支持zip,tar,tar.gz,tar.bz2,jar,dir,war格式
fileSet定义代码目录中与输出目录的映射,在该节点下还有 <includes/>,<excludes/>两个非常有用的节点。
比如:

        <fileSet>
            <directory>${project.basedir}\src\main\resources</directory>
            <outputDirectory>\</outputDirectory>
            <includes>
                <include>some/path</include>
            </includes>
            <excludes>
                <exclude>some/path1</exclude>
            </excludes>
        </fileSet>

以上代码表示归档时包括some/path,不包括some/path1
dependencySets节点下为依赖设置
在上述配置中,表示所有运行时依赖的jar包归档到lib目录下。在上述截图中lib目录下的文件就是所有依赖的jar包

更多节点的用法可以去官网查询
http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven是一种用于管理Java项目的构建工具,可以自动化完成项目的编译、测试、打包等工作。在使用Maven进行打包时,可能会遇到依赖包没有被打进Jar包的情况。 造成依赖包没有被打进Jar包的原因有以下几种可能: 1. 配置错误:Maven的pom.xml文件中的配置有误。可能是依赖包的scope被设置为provided,这意味着依赖包只在编译和测试时可用,不会被打包进最终的Jar包。 2. 打包插件错误:可能没有正确配置Maven打包插件,或者使用了不适合项目的插件。打包插件负责定义构建过程,包括哪些文件被包含进Jar包中。 3. 依赖包冲突:在项目中使用了不同版本的依赖包,可能会导致打包时只有其中一个版本的依赖包被打进Jar包中。 解决这个问题的方法如下: 1. 检查并确保pom.xml文件中的依赖包scope设置正确。如果需要将依赖打包进Jar包中,应该将scope设置为compile。 2. 确认使用的打包插件是否正确配置。可以参考官方文档或者其他资源,了解正确的配置方式。 3. 处理依赖包冲突问题。可以通过在pom.xml中手动指定要使用的依赖包版本,或者通过使用Maven的dependencyManagement来管理依赖包的版本,以确保正确的依赖包被打包进Jar包中。 总之,对于Maven打包没有将依赖打包进Jar包的问题,我们需要检查和修正pom.xml文件的配置,正确使用打包插件,并解决依赖包冲突问题,从而确保依赖包能够被正确地打包进最终的Jar包中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值