当执行mvn package的时候,maven是怎么打包的

来源:互联网 发布:傻瓜式淘宝客app 编辑:程序博客网 时间:2024/05/16 17:13
为了优化需要,我需要看一下我的maven项目(一个java服务)在打包的时候是如何把项目打包成一个tar.gz包的,gz包里的各个文件又是哪来的。
也就是说,我需要知道maven是怎么把项目从这样:

变成这样的:

我的工程是有一个父模块三个子模块的java服务,用maven管理相关的依赖。
mvn package命令打成的gz包有app、bin、conf、lib四个文件夹:
  • bin里面有一堆脚本,启动、停止之类的。我最想弄明白的其实就是这里面的脚本是怎么生成的,现在遇到的优化项目就需要调整这里面的启动脚本。
  • conf里是一堆配置文件。
  • lib里是所有相关的jar包。

探寻过程

一,pom文件

在父模块的pom文件里,我找到了maven的打包插件:

<plugin><groupId>com.dangdang</groupId><artifactId>dd-assembly-plugin</artifactId><version>2.0.1</version></plugin>


在子模块tms-bms-bootstrap的pom文件里,我找到了使用父模块打包插件的代码:

<plugin>    <groupId>com.dangdang</groupId>    <artifactId>dd-assembly-plugin</artifactId>    <executions>        <execution>            <phase>package</phase>            <goals>                <goal>assembly</goal>            </goals>        </execution>    </executions></plugin>


二,maven打包插件

在我本地的maven仓库中,根据pom文件里配置的groupId、artifactId和version组成的路径:com\dangdang\dd-assembly-plugin\2.0.1找到了dd-assembly-plugin-2.0.1.jar。
dd-assembly-plugin-2.0.1.jar是maven的打包插件,和maven自己的打包插件名字不一样,因为这个属于公司框架,是在那基础上改过的,这个jar包的结构是这样的:


三,打包插件的配置文件

在assemblies目录下有dd-frame-assemby.xml(不一定非要叫这么个名字),是打包的描述文件,这个文件描述了在打包的时候maven会怎样操作,是maven打包插件的关键配置,这个文件的内容如下:

<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>assembly</id>    <formats>        <format>tar.gz</format>    </formats>    <includeBaseDirectory>true</includeBaseDirectory>    <fileSets>        <fileSet>            <directory>classpath:assemblies/bin/properties/jmx_config</directory>            <outputDirectory>bin/properties/jmx_config</outputDirectory>            <includes>                <include>jmxremote.password</include>            </includes>            <fileMode>0600</fileMode>        </fileSet>        <fileSet>            <directory>classpath:assemblies/bin</directory>            <outputDirectory>bin</outputDirectory>            <fileMode>0755</fileMode>        </fileSet>        <fileSet>            <directory>classpath:assemblies/app</directory>            <outputDirectory>app</outputDirectory>            <fileMode>0644</fileMode>        </fileSet>        <fileSet>            <directory>classpath:assemblies/conf</directory>            <outputDirectory>conf</outputDirectory>            <fileMode>0644</fileMode>        </fileSet>        <fileSet>            <directory>src/main/resources/conf</directory>            <includes>                <include>*.properties</include>                <include>*/*.properties</include>                <include>logback.xml</include>            </includes>            <excludes>                <exclude>*.private.properties</exclude>                <exclude>*/*.private.properties</exclude>            </excludes>            <outputDirectory>conf</outputDirectory>            <fileMode>0644</fileMode>        </fileSet>    </fileSets>        <dependencySets>        <!-- copy all dd-frame and third party jars to lib folder. -->        <dependencySet>            <outputDirectory>lib</outputDirectory>            <excludes>                <exclude>com.dangdang:*</exclude>                <exclude>com.dang:*</exclude>            </excludes>        </dependencySet>        <dependencySet>            <outputDirectory>lib</outputDirectory>            <includes>                <include>com.dangdang:apimonitor</include>                <include>com.dangdang:ddschedule</include>                <include>com.dangdang:dd-assembly-plugin</include>                <include>com.dangdang:dd-common</include>                <include>com.dangdang:dd-container</include>                <include>com.dangdang:dd-soa</include>                <include>com.dangdang:dd-job</include>                <include>com.dangdang:dd-job-tbschedule</include>                <include>com.dangdang:dd-rdb</include>                <include>com.dangdang:dd-reg</include>                <include>com.dangdang:dd-log</include>                <include>com.dangdang:dd-web</include>                <include>com.dangdang:dd-test</include>                <include>com.dangdang:dd-package</include>                <include>com.dangdang:elastic-job-core</include>                <include>com.dangdang:elastic-job-spring</include>            </includes>            <fileMode>0644</fileMode>        </dependencySet>                <!-- copy all dangdang jars to app folder. -->        <dependencySet>            <outputDirectory>app</outputDirectory>            <includes>                <include>com.dangdang:*</include>                <include>com.dang:*</include>            </includes>            <excludes>                <exclude>com.dangdang:apimonitor</exclude>                <exclude>com.dangdang:ddschedule</exclude>                <exclude>com.dangdang:dd-assembly-plugin</exclude>                <exclude>com.dangdang:dd-common</exclude>                <exclude>com.dangdang:dd-container</exclude>                <exclude>com.dangdang:dd-soa</exclude>                <exclude>com.dangdang:dd-job</exclude>                <exclude>com.dangdang:dd-job-tbschedule</exclude>                <exclude>com.dangdang:dd-rdb</exclude>                <exclude>com.dangdang:dd-reg</exclude>                <exclude>com.dangdang:dd-log</exclude>                <exclude>com.dangdang:dd-web</exclude>                <exclude>com.dangdang:dd-test</exclude>                <exclude>com.dangdang:dd-package</exclude>                <exclude>com.dangdang:elastic-job-core</exclude>                <exclude>com.dangdang:elastic-job-spring</exclude>            </excludes>            <fileMode>0644</fileMode>        </dependencySet>                <!--             Copy all dangdang jar's '*.properties' and profiles's '*.properties' to conf folder, exclude '*.private.properties'.        -->        <dependencySet>            <includes>                <include>com.dangdang:*</include>                <include>com.dang:*</include>            </includes>            <excludes>                <exclude>com.dangdang:apimonitor</exclude>                <exclude>com.dangdang:ddschedule</exclude>                <exclude>com.dangdang:dd-assembly-plugin</exclude>                <exclude>com.dangdang:dd-common</exclude>                <exclude>com.dangdang:dd-container</exclude>                <exclude>com.dangdang:dd-soa</exclude>                <exclude>com.dangdang:dd-job</exclude>                <exclude>com.dangdang:dd-job-tbschedule</exclude>                <exclude>com.dangdang:dd-rdb</exclude>                <exclude>com.dangdang:dd-reg</exclude>                <exclude>com.dangdang:dd-log</exclude>                <exclude>com.dangdang:dd-web</exclude>                <exclude>com.dangdang:dd-test</exclude>                <exclude>com.dangdang:dd-package</exclude>                <exclude>com.dangdang:elastic-job-core</exclude>                <exclude>com.dangdang:elastic-job-spring</exclude>            </excludes>            <unpack>true</unpack>            <unpackOptions>                <includes>                    <include>conf/*.properties</include>                    <include>conf/*/*.properties</include>                    <include>conf/logback.xml</include>                    <include>conf/includedLogbackConfig.xml</include>                </includes>                <excludes>                    <exclude>conf/*.private.properties</exclude>                    <exclude>conf/*/*.private.properties</exclude>                </excludes>            </unpackOptions>            <fileMode>0644</fileMode>        </dependencySet>        <!--             If app jar does not have includedLogbackConfig.xml, copy default includedLogbackConfig.xml to conf folder. For ddframe package only.        -->        <dependencySet>            <includes>                <include>com.dangdang:dd-log</include>            </includes>            <unpack>true</unpack>            <unpackOptions>                <includes>                    <include>includedLogbackConfig.xml</include>                </includes>            </unpackOptions>            <fileMode>0644</fileMode>            <outputDirectory>conf/</outputDirectory>        </dependencySet>    </dependencySets></assembly>


对这个文件的部分内容说明如下:
  • <formats>表示打包后的文件格式,现在写的tar.gz就表示打包后会生成一个xxx.tar.gz的压缩包,还可以设置为war什么的。
  • <fileSets>表示要生成的包下有什么目录,目录里有哪些文件,多个目录可以用多个<fileSet>标签来表示。
  • <fileSets>标签下的<directory>表示从哪个目录获取文件,classpath:xxxx表示从打包插件的jar包中获取文件,比如20行的<directory>classpath:assemblies/bin</directory>(打包后的bin目录下的脚本就来自这里,我的目的达到了),而35行的<directory>src/main/resources/conf</directory>代表从项目路径中获取文件。
  • <fileSet>标签下的<includes>代表了那些文件要被打包,<excludes>标签功能相反,代表哪些文件在打包时将被忽略。
  • <fileSet>标签下的<outputDirectory>代表了将要生成的包下的目录名字,比如打包后生成tar.gz包里有bin目录,就是用这个标签设置的。
  • <fileSet>标签下的<fileMode>可能是目录权限,我不太确定。
  • <dependencySets>标签代表了项目的maven依赖的jar包的处理方式,以及这些jar包将要被放到目标的那个目录下,多个目录可以用多个<dependencySet>标签来表示。
  • <dependencySet>下的<outputDirectory>功能和<fileSet>下的同名标签功能相同,指定了目标路径。
  • <dependencySet>下的<includes>功能和<fileSet>下的同名标签功能相同,指定了哪些jar包将被放入目标目录。
  • <dependencySet>下的<excludes>功能和<fileSet>下的同名标签功能相同,指定了哪些jar包将被忽略。

maven打包插件的这个配置文件其实实现了更多的功能,用户可以自行配置,我只研究到这里,大概就可以知道maven打包的时候都是按照什么规则进行的。
0 0
原创粉丝点击