介绍
Maven的Assembly Plugin使开发人员能够将项目输出组合到一个可分发的存档中,该存档还包含依赖项,模块,站点文档和其他文件。
您的项目可以使用预制的装配体描述符之一轻松构建分发“装配体”。 这些描述符处理许多常见的操作,例如将项目的文件以及生成的文档打包到单个zip存档中。 或者,您的项目可以提供自己的描述符,并且可以对程序集中的依赖项,模块,文件集和单个文件的打包方式进行更高级别的控制。
当前,它可以创建以下格式的发行版:
- zip
- tar
- tar.gz (or tgz)
- tar.bz2 (or tbz2)
- tar.snappy
- tar.xz (or txz)
- jar
- dir
- war
- 等等
实战
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version> <configuration> <descriptors> <descriptor>src/assembly/package.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> |
工程截图
package.xml
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 |
<assembly> <id>bin</id> <formats> <!-- 最终打包成一个用于发布的zip文件 --> <format>zip</format> </formats> <dependencySets> <dependencySet> <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 --> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>./lib</outputDirectory> <unpack>false</unpack> </dependencySet> </dependencySets> <fileSets> <!-- 把项目相关的说明文件,打包进zip文件的根目录 --> <fileSet> <directory>${project.basedir}</directory> <outputDirectory>./</outputDirectory> <includes> <include>README*</include> <include>LICENSE*</include> <include>NOTICE*</include> </includes> </fileSet> <!-- 把项目的配置文件,打包进zip文件的config目录 --> <fileSet> <directory>${project.basedir}/src/main/config</directory> <outputDirectory>./config</outputDirectory> <includes> <include>*.xml</include> <include>*.properties</include> </includes> </fileSet> <!-- 把项目的脚本文件目录( src/main/scripts )中的启动脚本文件,打包进zip文件的根目录 --> <fileSet> <directory>${project.build.scriptSourceDirectory}</directory> <outputDirectory>./</outputDirectory> <includes> <include>*.bat</include> <include>*.sh</include> </includes> </fileSet> <fileSet> <directory>${project.basedir}/logs</directory> <outputDirectory>./logs</outputDirectory> <excludes> <exclude>*/**</exclude> </excludes> </fileSet> <!-- 把项目的pid文件,打包进zip文件的run目录 --> <fileSet> <directory>${project.basedir}/src/main/run</directory> <outputDirectory>./run</outputDirectory> <includes> <include>*.pid</include> </includes> </fileSet> <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 --> <fileSet> <directory>${project.build.directory}</directory> <outputDirectory>./lib</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> </assembly> |
pid文件
1 |
8888 |
web-server.bat脚本
1 2 |
cd /d %~dp0 java -server -Xms768m -Xmx1280m -XX:NewRatio=3 -XX:PermSize=96m -XX:MaxPermSize=128m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=1 -XX:ParallelGCThreads=4 -XX:ParallelCMSThreads=4 -XX:CMSInitiatingOccupancyFraction=50 -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSMarkStackSize=8M -XX:CMSMarkStackSizeMax=32M -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -Xloggc:./logs/gc.log -classpath ".;./lib/*;" -Dio.netty.leakDetection.level=advanced com.xxxx.xxxxx.WebApplication |
web-server.sh脚本
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
#!/bin/bash #-----------------------------------------------------------------------------------------------------------------# #-CONFIG----------------------------------------------------------------------------------------------------------# APP_DIR=$(cd "$(dirname "$0")"; pwd) SCRIPT_DIR=$APP_DIR/bin/ LOG_DIR=$APP_DIR/logs/ JVM_ARGS="-server -Xms768m -Xmx1280m -XX:NewRatio=3 -XX:PermSize=96m -XX:MaxPermSize=128m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=1 -XX:ParallelGCThreads=4 -XX:ParallelCMSThreads=4 -XX:CMSInitiatingOccupancyFraction=50 -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSMarkStackSize=8M -XX:CMSMarkStackSizeMax=32M -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:${APP_DIR}/logs/gc.log -Dio.netty.leakDetection.level=advanced -Dspring.profiles.active=dev -Dspring.profiles.address=192.168.0.153:8848 -Dspring.profiles.namespace=nacos的命名空间 -Ddubbo.protocol.host=192.168.0.153 -Dspring.cloud.inetutils.preferred-networks=192.168.0.153" PID_FILE_DIR=$APP_DIR/run/ APP_NAME="com.xxxx.xxxx.WebApplication" # Classpath CLASSPATH=".:$APP_DIR/lib/*:" NEWPATH="-classpath \"${CLASSPATH}\"" # System Path #PATH="$JAVA_HOME/bin:$PATH" #export PATH ${JAVA_HOME}/bin/java -version echo #-VARS------------------------------------------------------------------------------------------------------------# APP=$2 #-FUNCTIONS-------------------------------------------------------------------------------------------------------# getStatus() { if [ -f $PID_FILE ] ; then pid=`cat $PID_FILE` if [ "x$pid" != "x" ] && kill -0 $pid 2>/dev/null ; then return 0 fi fi return 1 } printStatus() { PID_FILE="${PID_FILE_DIR}${APP_NAME}.pid" getStatus if [ $? -eq 0 ]; then echo "Active ${APP_NAME}" else echo "Inactive ${APP_NAME}" fi } #-MAIN------------------------------------------------------------------------------------------------------------# if [ "$1" = "start" ]; then shift PID_FILE="${PID_FILE_DIR}${APP_NAME}.pid" getStatus if [ $? -eq 0 ]; then echo "Active ${APP_NAME} pid" `cat ${PID_FILE}` else ${JAVA_HOME}/bin/java ${JVM_ARGS} ${NEWPATH} ${APP_NAME} #>> ${LOG_DIR}${APP_NAME}.log 2>&1 & echo "Starting ${APP_NAME} at" `date` >> ${LOG_DIR}${APP_NAME}.log echo "Starting ${APP_NAME}" echo $! > $PID_FILE fi exit 0 elif [ "$1" = "list" ]; then shift echo "${APP_NAME}" exit 0 elif [ "$1" = "kill" ]; then shift PID_FILE="${PID_FILE_DIR}${APP}.pid" getStatus if [ $? -eq 0 ]; then kill `cat ${PID_FILE_DIR}${APP}.pid` else echo "${APP} Wasn't Active" fi exit 0 elif [ "$1" = "stop" ]; then shift PID_FILE="${PID_FILE_DIR}${APP_NAME}.pid" getStatus if [ $? -eq 0 ]; then kill `cat ${PID_FILE}` else echo "${APP_NAME} Wasn't Active" fi exit 0 elif [ "$1" = "status" ]; then shift printStatus exit 0 elif [ "$1" = "config" ]; then shift echo "Script dir : ${SCRIPT_DIR}" echo "Where to log : ${LOG_DIR}" echo "JVM args : ${JVM_ARGS}" echo "Pidfile directory : ${PID_FILE_DIR}" echo "Properties File : ${PROPERTIES_FILE}" exit 0 else echo "" echo "Usage:" echo "Runner {start|stop|list|kill {app}|status|config}" echo " start - start all Applications" echo " stop - stop all Applications" echo " list - Prints a list of the application names": echo " kill {app} - kill single app and restarts (useful when the jvm is not responding)" echo " status - status Generate WebPageStatistics" echo " config - show script config" echo "" exit 0 fi |
打包命令
1 |
mvn clean package -Pdev |
打包完的截图


lib文件下放了所有用到的jar包,要想运行程序,直接运行脚本文件即可。很方便。使用maven-assembly-plugin插件进行自定义打包就介绍完成了。
0