maven自定义打包 步骤
pom文件引入两个插件
maven-dependency-plugin maven 自带插件
maven-assembly-plugin assembly插件
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.apache.maven.plugins</groupId> 5 <artifactId>maven-assembly-plugin</artifactId> 6 <!--这里需要指定assemble打包规则的xml 在父pom中指定了此处省略--> 7 </plugin> 8 <plugin> 9 <groupId>org.apache.maven.plugins</groupId> 10 <artifactId>maven-dependency-plugin</artifactId> 11 <executions> 12 <execution> 13 <id>uyun-unpack</id> 14 <phase>generate-resources</phase> 15 <goals> 16 <goal>unpack</goal> 17 </goals> 18 <configuration> 19 <artifactItems> 20 <!-- 将依赖的构建tar.gz包解压 --> 21 <artifactItem> 22 <groupId>uyun.kaola</groupId> 23 <artifactId>kaola-app-build</artifactId> 24 <classifier>bin</classifier> 25 <version>${project.version}</version> 26 <type>tar.gz</type> 27 <overWrite>true</overWrite> 28 <outputDirectory>${project.build.directory}/iotdiscovery-app</outputDirectory> 29 <includes>**</includes> 30 </artifactItem> 31 </artifactItems> 32 </configuration> 33 </execution> 34 </executions> 35 </plugin> 36 </plugins> 37 </build>
父pom.xml如下 指定插件版本 以及使用assembly.xml定义打包规则
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>${assembly.version}</version> <executions> <execution> <id>build-bin</id> <!-- 绑定到package生命周期阶段 --> <phase>package</phase> <goals> <!-- 只运行一次 --> <goal>single</goal> </goals> <configuration> <descriptors> <!--描述文件路径 ,默认跟当前pom.xml同一级目录下 --> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> <!-- 依赖分析插件 执行 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>${maven_dependency_plugin_version}</version> <!-- <executions> package阶段绑定,将maven依赖的构建jar拷贝到项目根目录/lib目录下 <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/lib/ </outputDirectory> </configuration> </execution> </executions> --> </plugin> </plugins> </pluginManagement> </build>
assembly.xml中具体定义打包的规则 需要的包 输出目录等
1 <?xml version="1.0" encoding="UTF-8"?> 2 <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> 4 5 <id>bin</id> 6 <formats> 7 <!-- <format>tar.gz</format> --> 8 <format>tar.gz</format> 9 </formats> 10 <includeBaseDirectory>false</includeBaseDirectory> 11 12 13 <!-- 配置要归档的文件集合 --> 14 <fileSets> 15 <fileSet> 16 <!-- 定义要归档的文件目录位置 --> 17 <directory>src/main/resources</directory> 18 <filtered>true</filtered> 19 <!-- 定义归档输出目录 --> 20 <outputDirectory>iotdiscovery-app</outputDirectory> 21 <!-- 定义包含规则 --> 22 <!-- <includes> 23 <include>spring/**</include> 24 <include>logback.xml</include> 25 <include>disconf.properties</include> 26 </includes> --> 27 <!-- 定义排除规则 --> 28 <excludes> 29 <exclude>**/.gitkeep</exclude> 30 <exclude>**/iot-discovery.properties</exclude> 31 <exclude>META-INF/**</exclude> 32 </excludes> 33 <!-- 指定文件权限(*inux) --> 34 <fileMode>755</fileMode> 35 </fileSet> 36 37 38 39 <fileSet> 40 <directory>${project.build.directory}/iotdiscovery-app</directory> 41 <includes> 42 <!-- 包含iotdiscovery-app模块哪些修改过的文件 --> 43 <include>**/message_zh_CN.properties</include> 44 <include>**/message_en_US.properties</include> 45 <!-- <include>**/kaola-app-web.jar</include> --> 46 <!-- <include>**/kaola-app-service.jar</include> --> 47 <include>**/web/html/</include> 48 49 </includes> 50 <outputDirectory>iotdiscovery-app</outputDirectory> 51 </fileSet> 52 <fileSet> 53 <directory>${project.build.directory}/iotdiscovery-app/web/lib</directory> 54 <includes> 55 <!-- 包含iotdiscovery-app模块哪些修改过的文件 --> 56 <!-- <include>**/message_zh_CN.properties</include> --> 57 <!-- <include>**/message_en_US.properties</include> --> 58 <include>kaola-app-web.jar</include> 59 <include>kaola-app-service.jar</include> 60 <!-- <include>**/web/html/</include> --> 61 62 </includes> 63 <outputDirectory>iotdiscovery-app/custom</outputDirectory> 64 </fileSet> 65 66 </fileSets> 67 68 </assembly>