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

时间: 2024-09-18 03:06:56

maven 打包,打包依赖,并只打某个包下的依赖的相关文章

maven之打包插件(maven-assembly-plugin,maven-shade-plugin与maven-assembly-plugin)

一. 介绍 maven提供的打包插件有如下三种: plugin function maven-jar-plugin maven 默认打包插件,用来创建 project jar maven-shade-plugin 用来打可执行包,executable(fat) jar  maven-assembly-plugin  支持定制化打包方式,例如 apache 项目的打包方式 每种打包方式都具有自己的应用场景. 二. 打包准备 1). 需要设定文件的编码格式(如果不设定,将会以系统的默认编码进行处理)

mac os maven 项目打包

问题描述 mac os maven 项目打包 平时都在wins上开发,maven打包项目没有问题,现在移到mac os上开发,mvn package打包后,发现每个目录下都多处以"._"开头的文件,例如总目录下有 a,b两个文件夹,则打出来以后多出._a,._b两个文件,有人遇到过吗?是不是maven在mac os上使用有一些不同 解决方案 有人知道吗.....

Linux下打包压缩war、解压war包和jar命令

环境 RedHat Linux 9 + VWWare 8.0 + SSH 3.2.9 + Putty 0.62 问题 Linux下打包压缩war.解压war包和jar命令 解决 把project_a文件夹下的文件打包成project.war 1.打包 jar - xvf project.war /project_a -c   创建war包 -v   显示过程信息 -f   指 定 JAR文件名,通常这个参数是必须的 -M  不产生所有项的清单 (MANIFEST]文件,此参数会忽略 -m参数 -

关于maven依赖引入多少个jar包问题?

问题描述 关于maven依赖引入多少个jar包问题? 10C 我有这样的疑问我项目中依赖了spring-context的时候项目中能自动导入: 但是当我依赖的是就只能引入一个jar包:我能不能在maven的中央仓库中查询到某个依赖会引用多少个jar包进来?还有就是我怎么实现只要在pom文件引用一个dependency就能把hibernate的所以相关jar引进来? 解决方案 你得groupid artifactid等这些都会确定唯一的jar ,不会引入多个jar的. maven仓库 hibern

setup py-如何用python setup.py打包多个.py文件到同一个包下面

问题描述 如何用python setup.py打包多个.py文件到同一个包下面 网上提供的一个最简单的例子是:只有一个test.py需要打包时 在setup.py中写上如下内容: coding: utf-8 from distutils.core import setup setup(name="MyTest", version="1.0", description="test", author="ChaoMa", autho

maven dependencie-maven项目下的依赖吗maven dependencie老是丢失

问题描述 maven项目下的依赖吗maven dependencie老是丢失 丢失后删掉项目下的那.setting .classpath .project 然后重新用svn服务器上更新,又有了,怎么回事?

myeclipse maven项目,update 操作时会改变工程的上下文 以及依赖path的jdk

问题描述 myeclipse maven项目,update 操作时会改变工程的上下文 以及依赖path的jdk 求救,如题, myeclipse maven项目,update project configration操作时会改变工程的上下文 以及依赖path的jdk 因为发布到tomcat上且需要作为默认工程发布,开始时候讲工程的上下文 改为"/",每一次做update project configration后上下文 会随之改成工程的名字, 第一次提问,没有分.各位见谅

maven项目部署在linux上的jar包问题

问题描述 maven项目部署在linux上的jar包问题 昨天吧写好的程序部署在linux上准备测试.发现找不到hibernate3.0.6.jar 看后发现, 这个jar包是引用在本地磁盘上的一个jar包.如:G:xxxx/xxxx/xx/hibernateXX.jar ,但是maven的jar包也已经打包到lib下放到程序包的同级目录下.可能linux上没有盘符, 而maven里找jar包的是通过pom文件找jar包的.pom文件里面写的是G:xxxxxxx.hibernateXX.jar

gradle-android 依赖 提示找不到 包

问题描述 android 依赖 提示找不到 包 用gradle运行的时候 发现提示找不到包. 结构是 android项目 依赖A项目,A项目依赖B项目 运行的时候提示找不到 B项目里面的class. android 不能智能点依赖么 output都勾选了的. 因为B项目不是跟android项目直接依赖. 我也不想直接依赖. 因为本身就没什么关系 只是A项目用到了这些东西. 请问 打包的时候如何能在android 项目里面 不配置任何跟b项目有关的东西 让能正确导出依赖关系?