Maven插件maven-assembly-plugin進(jìn)行打包部署

maven程序集插件提供了一種描述符格式,允許您定義項(xiàng)目中文件和目錄的任意程序集。例如,如果Maven項(xiàng)目包含目錄“src/main/bin”,則可以指示程序集插件將此目錄的內(nèi)容復(fù)制到程序集的“bin”目錄,并將“bin”目錄中文件的權(quán)限更改為UNIX模式755。配置此行為的參數(shù)通過程序集描述符提供給程序集插件。

目前,它可以創(chuàng)建以下格式的發(fā)行版:

image

我們平時(shí)在開發(fā)過程中的項(xiàng)目目錄結(jié)構(gòu)是這樣的:

image

在用maven進(jìn)行打包后,希望得到的目錄是這樣的

image

bin: 啟動(dòng)腳本

conf: 配置信息

lib:用的jar包

logs:所有日志信息

輸出的格式為 tar.gz 在assembly.xml中可以這樣配置:

<assembly>
    <id>assembly</id>
  <!-- 輸出格式 -->
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
  <!-- 指定文件組的組裝方式 -->
    <fileSets>
      <!-- 將項(xiàng)目中src/main/bin目錄下的腳本文件copy到target目錄的bin目錄下 -->
        <fileSet>
            <directory>src/main/assembly/bin</directory>
            <outputDirectory>bin</outputDirectory>
 <!--
            0755->即用戶具有讀/寫/執(zhí)行權(quán)限,組用戶和其它用戶具有讀寫權(quán)限;
            0644->即用戶具有讀寫權(quán)限,組用戶和其它用戶具有只讀權(quán)限;
        -->
            <fileMode>0755</fileMode>
        </fileSet>
      <!-- 將項(xiàng)目中src/main/resources目錄下的資源文件copy到target目錄的conf目錄下 -->
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>conf</outputDirectory>
            <fileMode>0644</fileMode>
        </fileSet>
      <!-- 將項(xiàng)目中src/main/resources目錄下的日志文件copy到target目錄的logs目錄下 -->
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>logs</outputDirectory>
            <fileMode>0755</fileMode>
            <excludes>
                <exclude>**/*</exclude>
            </excludes>
        </fileSet>
      <!-- 將項(xiàng)目中target目錄下的包含jar的文件copy到lib目錄下 -->
        <fileSet>
            <directory>target</directory>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

如果想把一些依賴庫(kù)打到包里,可以用 dependencySets 元素,定義依賴包打包到目錄下

  <!-- 指定依賴jar包輸出的目錄 -->
  <dependencySets>
    <dependencySet>
        <useProjectArtifact>true</useProjectArtifact>
        <outputDirectory>lib</outputDirectory>
        <!-- 只包含runtime作用域的依賴 -->
        <scope>runtime</scope>
    </dependencySet>
  </dependencySets>
image.gif

排除文件,在fileSet里可以使用includes 和 excludes來(lái)更精確的控制哪些文件要添加,哪些文件要排除。
例如要排除某個(gè)目錄下所有的txt文件:

<fileSet>  
    <directory>target/classes</directory>  
    <outputDirectory>/</outputDirectory>  
    <excludes>  
        <exclude>**/*.txt</exclude>  
    </excludes>  
</fileSet>

在pom文件中進(jìn)行添加maven-assembly-plugin插件來(lái)定制化打包

 <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <executable>true</executable>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 編碼和編譯和JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!--添加打包插件聲明 -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <!--執(zhí)行器 mvn assembly:assembly-->
                <executions>
                    <execution>
                        <!--名字任意 -->
                        <id>make-assembly</id>
                        <!-- 綁定到package生命周期階段上 -->
                        <phase>package</phase>
                        <!-- 只運(yùn)行一次 -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

startup.bat 啟動(dòng)腳本

@echo off

set APP_NAME=zhaixingzu.jar

set CONFIG= -Dlogging.path=../logs  -Dspring.config.location=../conf/application.yml

set DEBUG_OPTS=
if ""%1"" == ""debug"" (
   set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs 
   goto debug
)

set JMX_OPTS=
if ""%1"" == ""jmx"" (
   set JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9888 -Dcom.sun.management.jmxremote.ssl=FALSE -Dcom.sun.management.jmxremote.authenticate=FALSE 
   goto jmx
)

echo "Starting the %APP_NAME%"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end

:debug
echo "debug"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end

:jmx
java -Xms512m -Xmx512m -server %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end

:end
pause

startup.sh 啟動(dòng)腳本

#!/bin/bash

SERVER_NAME='zhaixingzu'
JAR_NAME="$SERVER_NAME.jar"
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf

#SERVER_PORT=`sed -nr '/port: [0-9]+/ s/.*port: +([0-9]+).*/\1/p' conf/application.yml`
SERVER_PORT='8082'

PIDS=`ps -f | grep java | grep "$CONF_DIR" |awk '{print $2}'`
if [ "$1" = "status" ]; then
    if [ -n "$PIDS" ]; then
        echo "The $SERVER_NAME is running...!"
        echo "PID: $PIDS"
        exit 0
    else
        echo "The $SERVER_NAME is stopped"
        exit 0
    fi
fi

if [ -n "$PIDS" ]; then
    echo "ERROR: The $SERVER_NAME already started!"
    echo "PID: $PIDS"
    exit 1
fi

if [ -n "$SERVER_PORT" ]; then
    SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PORT | wc -l`
    if [ $SERVER_PORT_COUNT -gt 0 ]; then
        echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!"
        exit 1
    fi
fi

LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log

JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
    JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi

JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi

JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
    JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
    JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi

CONFIG_FILES=" -Dlogging.path=$LOGS_DIR -Dspring.config.location=$CONF_DIR/application.yml "
echo -e "Starting the $SERVER_NAME ..."
nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -jar $DEPLOY_DIR/lib/$JAR_NAME > $STDOUT_FILE 2>&1 &

COUNT=0
while [ $COUNT -lt 1 ]; do
    echo -e ".\c"
    sleep 1
    if [ -n "$SERVER_PORT" ]; then
        COUNT=`netstat -an | grep $SERVER_PORT | wc -l`
    else
        COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`
    fi
    if [ $COUNT -gt 0 ]; then
        break
    fi
done

echo "OK!"
PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
echo "PID: $PIDS"
echo "STDOUT: $STDOUT_FILE"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容