zoukankan      html  css  js  c++  java
  • 【Maven实战技巧】「插件使用专题」Maven-Assembly插件实现自定义打包

    前提概要

    最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有木有。本文分享该插件的配置以及微服务的统一打包方式。

    maven-assembly-plugin打包插件

    配置步骤及其他事项

    首先我们需要在pom.xml中配置maven的assembly插件

    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
    		<!-- 配置执行器 -->
              <execution>
                <id>make-assembly</id>
    			<!-- 绑定到package生命周期阶段上 -->
                <phase>package</phase>
                <goals>
    			  <!-- 只运行一次 -->
                  <goal>single</goal>
                </goals>
                <configuration>
                  <!--生成包的末尾添加assembly id,一般关闭 -->
                  <appendAssemblyId>false</appendAssemblyId>
                  <finalName>${project.artifactId}-${project.version}</finalName>
                  <!--加载指定的配置文件-->
                  <descriptors>
                  		<descriptor>src/main/assembly/assembly.xml</descriptor>
                  </descriptors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
     </build>
    

    配置参数介绍说明

    • execution:配置执行器
      • phase:绑定到package生命周期阶段上
      • goal:{single}:只运行一次
      • configuration->appendAssemblyId:生成包的末尾添加assembly id,一般关闭
      • descriptor:src/main/assembly/assembly.xml:后续会讲解,主要用于描述如何进行打包的规则。

    自定义格式包assembly.xml

    接着我们在src/main/assembly文件中配置assembly.xml文件

      <assembly>
          <id>唯一编号</id>
          <formats>
    	      <!--打包的文件格式,也可以有:war zip-->
              <format>tar.gz</format>
          </formats>
          <!--tar.gz压缩包下是否生成和项目名相同的根目录-->
          <includeBaseDirectory>true</includeBaseDirectory>
         <fileSets>
             <fileSet>
                 <directory>src/main/bin</directory>
                 <outputDirectory>/</outputDirectory>
    			 <!-- Linux权限 -->
                 <fileMode>0644</fileMode>
             </fileSet>
             <fileSet>
    			<directory>target/classes/META-INF/conf</directory>
                <outputDirectory>conf/META-INF/conf</outputDirectory>
    			<!-- Linux权限 -->
    			<fileMode>0644</fileMode>
            </fileSet>
    		<fileSet>
    			<directory>target/classes</directory>
    			<outputDirectory>conf</outputDirectory>
    			<fileMode>0644</fileMode>
    			<includes><!-- 只负责这些目标文件-->
    				<include>*.properties</include>
    				<include>*.xml</include>
    			</includes>
    		</fileSet>
         </fileSets>
         <!-- 输出到lib路径 -->
         <dependencySets>
              <dependencySet>
                 <!--是否在最外层套一个本项目的名称的文件目录-->
                 <useProjectArtifact>true</useProjectArtifact>
                 <!-- 输出到这个路径下 -->
                 <outputDirectory>lib</outputDirectory>
                 <!--将scope为runtime的依赖包打包-->
                 <scope>runtime</scope>
             </dependencySet>
         </dependencySets>
     </assembly>
    
    • 生成的lib文件夹下放该项目的所有依赖以及该服务jar包,src/main/bin文件夹下我们一般放start.sh和stop.sh两个脚本文件用来开启和关闭该服务,打包后直接放到根目录下

    • 生成的tar.gz文件的名字为:[maven-assembly-plugin插件中配置的finalName]-[assembly.xml配置的id(若assembly中没有指定id,则只有前半部分)].

    assembly的具体语法,请参见官网:

    • 所以:maven-compiler-plugin是用来将代码生成jar的工具,maven-assembly-plugin是用来生成指定格式的工具。

    • 配置完成后进入标签里指定的位置建xml文件,这里是src/assembly/assembly-descriptor.xml,内容如下:

    <assembly
    	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    	<id>my-assembly</id>
    	<formats>
              <!--打包生成后的格式 -->
    		<format>zip</format>
    	</formats>
         <!-- -->
    	<includeBaseDirectory>false</includeBaseDirectory>
    	<fileSets>
    		<fileSet>
                   <!--脚本所在的文件夹,以及打包后将脚本输出到哪个文件夹中 -->
    			<directory>src/scripts</directory>
    			<outputDirectory>alarm/bin</outputDirectory>
                   <!-- 哪些文件会被提取 -->
    			<includes>
    				<include>*.sh</include>
    			</includes>
                   <!-- 文件权限及编码 -->
    			<fileMode>0755</fileMode>
    			<lineEnding>unix</lineEnding>
    		</fileSet>
    		<fileSet>
                   <!--同上,这里配置的是配置文件所在的文件夹 -->
    			<directory>src/main/resources</directory>
    			<outputDirectory>alarm/conf</outputDirectory>
    			<includes>
    				<include>*.yml</include>
    			</includes>
    			<lineEnding>unix</lineEnding>
    		</fileSet>
    		<!--artifact -->
    		<fileSet>
                   <!--这里的target是maven-compiler-plugin生成该项目的jar包的位置 -->
    			<directory>target</directory>
    			<outputDirectory>alarm/lib</outputDirectory>
    			<includes>
    				<include>*.jar</include>
    			</includes>
    			<fileMode>0755</fileMode>
    		</fileSet>
    	</fileSets>
    	<dependencySets>
    		<dependencySet>
                   <!--这里是将该项目依赖的所有jar包存入lib文件夹中 -->
    			<outputDirectory>alarm/lib</outputDirectory>
    			<useProjectArtifact>false</useProjectArtifact>
    			<useProjectAttachments>true</useProjectAttachments>
    			<scope>runtime</scope>
    		</dependencySet>
    	</dependencySets>
    </assembly>
    

    启动脚本

    在完成以上配置后,只需在指定的位置建立scripts/start.sh和stop.sh两个脚本即可完成。具体启动脚本如下:

    start.sh启动文件脚本

    #!/bin/sh
    basepath=$(cd `dirname $0`;cd '../'; pwd)
    echo "path:"$basepath
    jarHome='/my.jar'
    echo "Starting my service"
    ls_date=`date +%Y%m%d`
    if [ ! -d "${basepath}/log" ]; then
      mkdir ${basepath}/log
    fi
    if [ ! -d "$basepath/log/${ls_date}" ]; then
      mkdir $basepath/log/${ls_date}
    fi
    nohup java -jar $basepath$jarHome --spring.config.location=$basepath/conf/server-attach.yml,$basepath/conf/server-shared.yml> $basepath/log/${ls_date}/${ls_date}.log &
    #####
    

    主要命令是获取当天日期,然后在log文件夹下建立指定日期的文件夹,并将日志存放进去。其中–spring.config.location用于加载指定的配置文件,多个配置文件用逗号分割。

    stop.sh停止文件脚本

    停止脚本通过ps -ef获取进程id然后kill,如下:

    #!/bin/sh
    my=`ps -ef |grep my.jar | grep -v grep | awk '{print $2}'`
    kill -9 $my
    

    结语

    以上配置完成后使用mvn package命令即可自动打成一个zip压缩包,包内包含bin、conf、lib文件夹,可用启动脚本一键启动。实际上这里能修改的地方还有很多,包括启动脚本也可以用其他方式如java -classpath方式启动等等,具体的可以根据自身需求进行相应修改。

    极限就是为了超越而存在的
  • 相关阅读:
    制作交互式网站风格的移动背景
    HTML5中的一些拖拽成员
    你不一定了解的CSS技巧
    DOS bat上使用MSSQLSERVER服务
    优化你的C语言程序就是这么简单
    编写宏的有效方法
    vba,excel,身份证,照片
    网址收藏
    PowerShell让系统可以执行.ps1文件,开机,关机,在线时间 , Function自定义函数
    按键精灵txt判断
  • 原文地址:https://www.cnblogs.com/liboware/p/15092958.html
Copyright © 2011-2022 走看看