• mvn打包:依赖包和启动包分离


    简述

    • 依赖插件:
    maven-jar-plugin
    maven-assembly-plugin
    
    • 1
    • 2
    • 项目目录结构
      项目目录结构示例

    assembly.xml

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
        <id>assemblyid>
        
        <formats>
            <format>zipformat>
        formats>
        
        <dependencySets>
            <dependencySet>
                <outputDirectory>liboutputDirectory>
                <scope>runtimescope>
            dependencySet>
        dependencySets>
        <fileSets>
            
            <fileSet>
                <directory>lib/directory>
                <outputDirectory>liboutputDirectory>
                <includes>
                    <include>*include>
                includes>
            fileSet>
    
    
            <fileSet>
                <directory>src/main/resourcesdirectory>
                <outputDirectory>configoutputDirectory>
                <includes>
                    <include>*.ymlinclude>
                    <include>*.yamlinclude>
                    <include>*.txtinclude>
                    <include>*.xmlinclude>
                includes>
            fileSet>
    
    
            <fileSet>
                <directory>sqldirectory>
                <outputDirectory>sqloutputDirectory>
                <includes>
                    <include>**include>
                includes>
            fileSet>
    
            <fileSet>
                
                <directory>bindirectory>
                <outputDirectory>binoutputDirectory>
                <directoryMode>0777directoryMode>
                <fileMode>0777fileMode>
                <lineEnding>unixlineEnding>
                <includes>
                    <include>*include>
                includes>
            fileSet>
        fileSets>
    assembly>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    插件配置(启动模块POM.XML)

    
    <properties>
            <deploy.plugin.version>3.1.1deploy.plugin.version>
            <assembly.plugin.version>3.6.0assembly.plugin.version>
            <dependency.plugin.version>3.3.0dependency.plugin.version>
            <jar.plugin.version>3.2.2jar.plugin.version>
    properties>
    
    
     <build>
            <resources>
                <resource>
                    <directory>src/main/resourcesdirectory>
                    <includes>
                        <include>**/*include>
                    includes>
                    <filtering>falsefiltering>
                resource>
            resources>
            <finalName>AppNamefinalName> 
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>${surefire.plugin.version}version>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-jar-pluginartifactId>
                    <version>${jar.plugin.version}version>
                    <configuration>
                        <archive>
                            <manifest>
                            
                                <mainClass>path.to.*ApplicationmainClass>
                                <addClasspath>trueaddClasspath>
                                <addDefaultImplementationEntries>trueaddDefaultImplementationEntries>
                                <addDefaultSpecificationEntries>trueaddDefaultSpecificationEntries>
                                <addBuildEnvironmentEntries>trueaddBuildEnvironmentEntries>
                            manifest>
                        archive>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-dependency-pluginartifactId>
                    <version>${dependency.plugin.version}version>
                    <executions>
                        <execution>
                            <id>copy-dependenciesid>
                            <phase>prepare-packagephase>
                            <goals>
                                <goal>copy-dependenciesgoal>
                            goals>
                            <configuration>
                         		<outputDirectory>${project.build.directory}/liboutputDirectory>                             <overWriteIfNewer>trueoverWriteIfNewer>
                            configuration>
                        execution>
                    executions>
                plugin>
                <plugin>
                    <artifactId>maven-assembly-pluginartifactId>
                    <version>${assembly.plugin.version}version>
                    <executions>
                        <execution>
                            <id>distro-assemblyid>
                            <phase>packagephase>
                            <goals>
                                <goal>singlegoal>
                            goals>
                            <configuration>
                                <descriptors>
                                    <descriptor>assembly/assembly.xmldescriptor>
                                descriptors>
                            configuration>
                        execution>
                    executions>
                    <configuration>
                        <appendAssemblyId>falseappendAssemblyId>
                    configuration>
                plugin> 
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>${deploy.plugin.version}version>
                    <configuration>
                        <skip>trueskip>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombokgroupId>
                                <artifactId>lombokartifactId>
                            exclude>
                        excludes>
                    configuration>
                plugin>
            plugins>
        build>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    结果

    执行 mvn package打包

    压缩包目录
    在这里插入图片描述

    附录

    service.sh脚本

    #! /bin/bash
    # init app environment
    export APP_HOME=`cd $(dirname $0)/..; pwd`
    
    PIDFILE=$APP_HOME/app.pid
    
    
    # set java main class
    MAIN_CLASS="path.to.*Application"
    
    # set class path config and jar package
    CLASSPATH=${CLASSPATH}:${APP_HOME}/config
    CLASSPATH=${CLASSPATH}:${APP_HOME}/lib/*
    
    # init java environment
    export JAVA=$(which java)
    if [ -z "$JAVA" ] ; then
      echo "no java environment ... "
      exit 1;
    fi
    
    # set java opt
    JAVA_OPT=" ${JAVA_OPT} -cp ${CLASSPATH}"
    JAVA_OPT=" ${JAVA_OPT} -XX:+UseConcMarkSweepGC"
    JAVA_OPT=" ${JAVA_OPT} -XX:CMSInitiatingOccupancyFraction=75"
    #JAVA_OPT=" ${JAVA_OPT} -XX:+UseCMSInitiatingOccupancyOnly"
    #JAVA_OPT=" ${JAVA_OPT} -Dlog4j2.formatMsgNoLookups=true"
    JAVA_OPT=" ${JAVA_OPT} -Dapp.home=${APP_HOME}"
    #JAVA_OPT=" ${JAVA_OPT} -Dfastjson.parser.safeMode=true"
    JAVA_OPT=" ${JAVA_OPT} ${MAIN_CLASS}"
    
    
    check_process_file() {
      if [ -f $PIDFILE ]
      then
        PID=$(cat $PIDFILE)
        ps -p $PID > /dev/null 2>&1
        if [ $? -eq 0 ]
        then
          echo "Process already running"
          exit 1
        fi
      fi
    }
    
    start() {
      # shell method
      cd $APP_HOME
      echo "starting program ... "
      echo "$JAVA ${JAVA_OPT}"
      nohup $JAVA ${JAVA_OPT} > /dev/null 2>&1 &
      check_process_file
      if [ $? -eq 0 ] ; then
        if echo -n $! > "$PIDFILE" ; then
          	PID=$(cat $PIDFILE)
          	echo "The program[$PID] launch succeed"
        else
            exit 1
        fi
      else
        exit 1
      fi
    }
    
    stop() {
      echo "Stopping program ... "
      if [ ! -f "$PIDFILE" ]
      then
        echo "No program to stop (could not find file $PIDFILE)"
      else
        PID=$(cat "$PIDFILE")
        kill -15  $PID
        while [ 0 -ne $(ps | awk '{print $1}' | grep -v -i "pid" | grep $PID | wc -l) ]
        do
          echo -e ".\c"
          sleep 1s
        done
        rm "$PIDFILE"
        echo "The program[$PID] is stopped"
      fi
    }
    
    if [ "$1" = "stop" ]; then
      stop
    elif [ "$1" = "start" ]; then
      start
    elif [ "$1" = "restart" ]; then
      echo "restart..."
      stop
      start
    else
      echo "Usage [start|stop|restart]"
    fi
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
  • 相关阅读:
    哪些企业可以做知识产权质押?
    米尔电子|第十六届STM32全国研讨会即将走进11个城市
    1555. 银行账户概要
    https代理如何设置?https代理有什么好处和坏处?
    Spring-Boot自我总结-001
    为什么要学习GoF设计模式?
    Mysql索引
    如何使用插件扩展Vue的功能
    IDEA双击无效打不开
    Eclipse使用教程
  • 原文地址:https://blog.csdn.net/istruth/article/details/132805602