当执⾏mvnpackage 的时候,maven 是怎么打包的
为了优化需要,我需要看⼀下我的maven项⽬(⼀个java服务)在打包的时候是如何把项⽬打包成⼀个包的,gz包⾥的各个⽂件⼜是哪来的。
也就是说,我需要知道maven是怎么把项⽬从这样:
变成这样的:
我的⼯程是有⼀个⽗模块三个⼦模块的java服务,⽤maven管理相关的依赖。 mvn package命令打成的gz包有app、bin、conf、lib四个⽂件夹:
bin⾥⾯有⼀堆脚本,启动、停⽌之类的。我最想弄明⽩的其实就是这⾥⾯的脚本是怎么⽣成的,现在遇到的优化项⽬就需要调整这⾥⾯的启动脚本。conf⾥是⼀堆配置⽂件。
lib⾥是所有相关的jar包。
探寻过程
⼀,pom⽂件
在⽗模块的pom⽂件⾥,我到了的打包插件:在⼦模块tms-bms-bootstrap的pom⽂件⾥,我到了使⽤⽗模块打包插件的代码:
<plugin>
<groupId>com.dangdang</groupId>
<artifactId>dd-assembly-plugin</artifactId>
<version>2.0.1</version>
</plugin>
<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⽬录下有l(不⼀定⾮要叫这么个名字),是打包的描述⽂件,这个⽂件描述了在打包的时候maven会怎样操作,是maven打包插件的关键配置,这个⽂件的内容如下: <assembly
xmlns="/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/plugins/maven-assembly-plugin/assembly/1.1.2 /xsd/assembly-1.1.2.xsd" > <id>assembly</id> <formats>
<format&</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&l</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>l</include>
<include>l</include> </includes> <excludes> <exclude>conf/*.private.properties</exclude>
<exclude>conf/*/*.private.properties</exclude> </excludes> </unpackOptions> <fileMode>0644</fileMode>
</dependencySet> <!-- If app jar does not l, copy l tomaven打包本地jar包
conf folder. For ddframe package only. --> <dependencySet> <includes> <include>com.dangdang:dd-log</include>
</includes> <unpack>true</unpack> <unpackOptions> <includes> <include&l</include> </includes> </unpackOptions> <fileMode>0644</fileMode> <outputDirectory>conf/</outputDirectory> </dependencySet>
</dependencySets> </assembly> 对这个⽂件的部分内容说明如下: <formats>表⽰打包后的⽂件格式,现在写的就表⽰打包后会⽣成⼀个的压缩包,还可以设置为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>代表了将要⽣成的包下的⽬录名字,⽐如打包后⽣成包⾥有bin⽬录,就是⽤这个标签设置的。<fileSet>标签下的<fileMode>可能是⽬录权限,我不太确定。
<dependencySets>标签代表了项⽬的maven依赖的jar包的处理⽅式,以及这些jar包将要被放到⽬标的那个⽬录下,多个⽬录可以⽤多个<dependencySet>标签来表⽰。<dependencySet>下的<outputDirectory>功能和<fileSet>下的同名标签功能相同,指定了⽬标路径。
<dependencySet>下的<includes>功能和<fileSet>下的同名标签功能相同,指定了哪些jar包将被放⼊⽬标⽬录。<dependencySet>下的<excludes>功能和<fileSet>下的同名标签功能相同,指定了哪些jar包将被忽略。 maven打包插件的这个配置⽂件其实实现了更多的功能,⽤户可以⾃⾏配置,我只研究到这⾥,⼤概就可以知道maven打包的时候都是按照什么规则进⾏的。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论