日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java项目构建部署包

發布時間:2023/12/2 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java项目构建部署包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

  • 博客分類:
  • JAVA

???? Java 工程在生產環境運行時,一般需要構建成一個jar,同時在運行時需要把依賴的jar添加到classpath中去,如果直接運行添加classpath很不方便,比較方便的是創建一個shell腳本。在公司項目中看到把工程代碼和依賴jar包合并到一塊,省去設置classpath的麻煩。但這樣把項目jar依賴綁定死,被其它項目引入,容易造成jar依賴沖突,如果用maven管理java項目,導致提交到公司倉庫jar過于龐大,同時也失去maven對jar依賴管理的作用。
??? 為了方便java項目部署運行,這里為構建部署包定義一個固定格式:

/java 項目
?? /lib??? --存放所有依賴jar
?? /conf?? --存放配置文件,例如:log4j, spring, properties等配置文件,不放入jar是為
???????????? 了方便修改這些配置文件
?? /logs?? --運行時自動創建的目錄,存放日志文件
?? /bin??? --存放運行腳本: server.sh 啟動和停止項目運行。

如果在構建部署包時,需要我們手動去創建這樣的目錄,把文件拷貝相應目錄,實在是太繁瑣了,幸好maven為我們這樣“懶惰”coder提供了一個很好的plugin:maven-assembly-plugin,可以去定制這樣的部署結構

1:配置maven-assembly-plugin

Java代碼 ?
  • <plugin> ??
  • ????<groupId>org.apache.maven.plugins</groupId> ??
  • ????<artifactId>maven-assembly-plugin</artifactId> ??
  • ????<configuration> ??
  • ????????<descriptors> ??
  • ????????????<descriptor>src/main/assembly/assembly.xml</descriptor> ??
  • ????????</descriptors> ??
  • ????</configuration> ??
  • ????<executions> ??
  • ????????<execution> ??
  • ????????????<id>make-assembly</id> ??
  • ????????????<phase>package</phase> ??
  • ????????????<goals> ??
  • ????????????????<goal>single</goal> ??
  • ????????????</goals> ??
  • ????????</execution> ??
  • ????</executions> ??
  • </plugin>??
  • <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><descriptors><descriptor>src/main/assembly/assembly.xml</descriptor></descriptors></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions> </plugin>


    2:assembly 配置打包格式

    ? ? 定義在assembly.xml文件中,文件中配置信息不作過多解釋,可以參考相應文檔

    Java代碼 ?
  • <assembly ??
  • ????xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"??
  • ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  • ????xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2?http://maven.apache.org/xsd/assembly-1.1.2.xsd"> ??
  • ???? ??
  • ????<id>bin</id> ??
  • ????<formats> ??
  • ????????<format>tar.gz</format> ??
  • ????</formats> ??
  • ????<dependencySets> ??
  • ????????<dependencySet> ??
  • ????????????<useProjectArtifact>true</useProjectArtifact> ??
  • ????????????<outputDirectory>lib</outputDirectory> ??
  • ????????????<scope>runtime</scope> ??
  • ????????</dependencySet> ??
  • ????</dependencySets> ??
  • ????<fileSets> ??
  • ????????<fileSet> ??
  • ????????????<outputDirectory>/</outputDirectory> ??
  • ????????????<includes> ??
  • ????????????????<include>README.txt</include> ??
  • ????????????</includes> ??
  • ????????</fileSet> ??
  • ????????<fileSet> ??
  • ????????????<directory>src/main/scripts</directory> ??
  • ????????????<outputDirectory>/bin</outputDirectory> ??
  • ????????????<lineEnding>unix</lineEnding> ??
  • ????????????<fileMode>0755</fileMode> ??
  • ????????????<includes> ??
  • ????????????????<include>*.sh</include> ??
  • ????????????</includes> ??
  • ????????</fileSet> ??
  • ????????<fileSet> ??
  • ????????????<directory>target/classes</directory> ??
  • ????????????<outputDirectory>/conf</outputDirectory> ??
  • ????????????<includes> ??
  • ????????????????<include>*.xml</include> ??
  • ????????????</includes> ??
  • ????????</fileSet> ??
  • ????</fileSets> ??
  • </assembly>??
  • <assemblyxmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"><id>bin</id><formats><format>tar.gz</format></formats><dependencySets><dependencySet><useProjectArtifact>true</useProjectArtifact><outputDirectory>lib</outputDirectory><scope>runtime</scope></dependencySet></dependencySets><fileSets><fileSet><outputDirectory>/</outputDirectory><includes><include>README.txt</include></includes></fileSet><fileSet><directory>src/main/scripts</directory><outputDirectory>/bin</outputDirectory><lineEnding>unix</lineEnding><fileMode>0755</fileMode><includes><include>*.sh</include></includes></fileSet><fileSet><directory>target/classes</directory><outputDirectory>/conf</outputDirectory><includes><include>*.xml</include></includes></fileSet></fileSets> </assembly>



    下面這段配置主要從src/main/scripts目錄獲取shell腳本復制到bin目錄。同時設置文件模式為unix,文件具有可執行權限。

    Java代碼 ?
  • <directory>src/main/scripts</directory> ??
  • <outputDirectory>/bin</outputDirectory> ??
  • <lineEnding>unix</lineEnding> ??
  • <fileMode>0755</fileMode> ??
  • <includes> ??
  • ????<include>*.sh</include> ??
  • </includes>??
  • <directory>src/main/scripts</directory> <outputDirectory>/bin</outputDirectory> <lineEnding>unix</lineEnding> <fileMode>0755</fileMode> <includes><include>*.sh</include> </includes>

    ?

    3:啟動類設計

    在學習metamorphosis的時候,對它的啟動腳本做了一些了解,發現服務stop方式設計比較優雅,通過JMX連接JVM,去關閉系統內部資源,再kill掉進程。

    UeapServerMBean定義stop方法,實現類UeapServer完成系統啟動加載和停止功能。ServerStartup是main方法入口,調用 UeapServer啟動方式,并注冊 UeapServer到JMX中。


    4:啟動腳本

    系統啟動腳本分為了兩個文件:

    env.sh 配置一些環境變量和JVM參數,實際應用中只需要修改 SERVER_NAME、? STARTUP_CLASS和 UEAP_JVM_ARGS變量名稱。

    server.sh 實際運行腳本,提供啟動、停止、運行狀態查詢、重啟功能,實際應用中不需要改動該文件。

    ?

    env.sh

    ?

    Java代碼 ?
  • #!/bin/bash ??
  • ??
  • #Config?your?java?home ??
  • #JAVA_HOME=/opt/jdk/ ??
  • ??
  • if?[?-z?"$JAVA_HOME"?];?then ??
  • ??export?JAVA=`which?java` ??
  • else??
  • ??export?JAVA="$JAVA_HOME/bin/java"??
  • fi ??
  • ??
  • UEAP_HOME=$BASE_DIR ??
  • SERVER_NAME="collect?master"??
  • STARTUP_CLASS="com.starit.ueap.collect.master.ServerStartup"??
  • ??
  • #Ueap?JMX?port ??
  • export?JMX_PORT=9123??
  • export?CLASSPATH=$BASE_DIR/conf:$(ls?$BASE_DIR/lib/*.jar?|?tr?'\n'?:) ??
  • ??
  • #UEAP?jvm?args ??
  • UEAP_JVM_ARGS="-Xmx512m?-Xms256m?-server"??
  • UEAP_JVM_ARGS="$UEAP_JVM_ARGS?-cp?$CLASSPATH?-Dueap.home=$ueap_home?-Dcollect.start.worker=false"??
  • ??
  • if?[?-z?"$UEAP_ARGS"?];?then ??
  • ??export?UEAP_ARGS="$UEAP_JVM_ARGS"??
  • fi??
  • #!/bin/bash#Config your java home #JAVA_HOME=/opt/jdk/if [ -z "$JAVA_HOME" ]; thenexport JAVA=`which java` elseexport JAVA="$JAVA_HOME/bin/java" fiUEAP_HOME=$BASE_DIR SERVER_NAME="collect master" STARTUP_CLASS="com.starit.ueap.collect.master.ServerStartup"#Ueap JMX port export JMX_PORT=9123 export CLASSPATH=$BASE_DIR/conf:$(ls $BASE_DIR/lib/*.jar | tr '\n' :)#UEAP jvm args UEAP_JVM_ARGS="-Xmx512m -Xms256m -server" UEAP_JVM_ARGS="$UEAP_JVM_ARGS -cp $CLASSPATH -Dueap.home=$ueap_home -Dcollect.start.worker=false"if [ -z "$UEAP_ARGS" ]; thenexport UEAP_ARGS="$UEAP_JVM_ARGS" fi

    ?

    server.sh

    ?

    Java代碼 ?
  • #!/bin/bash ??
  • ??
  • if?[?-z?"$BASE_DIR"?]?;?then ??
  • ??PRG="$0"??
  • ??
  • ??#?need?this?for?relative?symlinks ??
  • ??while?[?-h?"$PRG"?]?;?do??
  • ????ls=`ls?-ld?"$PRG"` ??
  • ????link=`expr?"$ls"?:?'.*->?\(.*\)$'` ??
  • ????if?expr?"$link"?:?'/.*'?>?/dev/null;?then ??
  • ??????PRG="$link"??
  • ????else??
  • ??????PRG="`dirname?"$PRG"`/$link"??
  • ????fi ??
  • ??done ??
  • ??BASE_DIR=`dirname?"$PRG"`/.. ??
  • ??
  • ??#?make?it?fully?qualified ??
  • ??BASE_DIR=`cd?"$BASE_DIR"?&&?pwd` ??
  • ??#echo?"collect?master?is?at?$BASE_DIR"??
  • fi ??
  • ??
  • source?$BASE_DIR/bin/env.sh ??
  • ??
  • AS_USER=`whoami` ??
  • LOG_DIR="$BASE_DIR/logs"??
  • LOG_FILE="$LOG_DIR/server.log"??
  • PID_DIR="$BASE_DIR/logs"??
  • PID_FILE="$PID_DIR/.run.pid"??
  • ??
  • function?running(){ ??
  • ????if?[?-f?"$PID_FILE"?];?then ??
  • ????????pid=$(cat?"$PID_FILE") ??
  • ????????process=`ps?aux?|?grep?"?$pid?"?|?grep?-v?grep`; ??
  • ????????if?[?"$process"?==?""?];?then ??
  • ????????????return?1; ??
  • ????????else??
  • ????????????return?0; ??
  • ????????fi ??
  • ????else??
  • ????????return?1??
  • ????fi?? ??
  • } ??
  • ??
  • function?start_server()?{ ??
  • ????if?running;?then ??
  • ????????echo?"$SERVER_NAME?is?running."??
  • ????????exit?1??
  • ????fi ??
  • ??
  • ????mkdir?-p?$PID_DIR ??
  • ????touch?$LOG_FILE ??
  • ????mkdir?-p?$LOG_DIR ??
  • ????chown?-R?$AS_USER?$PID_DIR ??
  • ????chown?-R?$AS_USER?$LOG_DIR ??
  • ???? ??
  • ????echo?"$JAVA?$UEAP_JVM_ARGS?-Dcom.sun.management.jmxremote?-Dcom.sun.management.jmxremote.authenticate=false?-Dcom.sun.management.jmxremote.ssl=false?\ ??
  • ??????-Dcom.sun.management.jmxremote.port=$JMX_PORT?$STARTUP_CLASS" ??
  • ????sleep?1??
  • ????nohup?$JAVA?$UEAP_JVM_ARGS?-Dcom.sun.management.jmxremote?-Dcom.sun.management.jmxremote.authenticate=false?-Dcom.sun.management.jmxremote.ssl=false?\ ??
  • ??????-Dcom.sun.management.jmxremote.port=$JMX_PORT?$STARTUP_CLASS?2>&1?>>$LOG_FILE?&?? ??
  • ????echo?$!?>?$PID_FILE ??
  • ????chmod?755?$PID_FILE ??
  • ????sleep?1; ??
  • ????tail?-F?$LOG_FILE ??
  • } ??
  • ??
  • function?stop_server()?{ ??
  • ????if?!?running;?then ??
  • ????????echo?"$SERVER_NAME?is?not?running."??
  • ????????exit?1??
  • ????fi ??
  • ????count=0??
  • ????pid=$(cat?$PID_FILE) ??
  • ????while?running; ??
  • ????do??
  • ??????let?count=$count+1??
  • ??????echo?"Stopping?$SERVER_NAME?$count?times"??
  • ??????if?[?$count?-gt?5?];?then ??
  • ??????????echo?"kill?-9?$pid"??
  • ??????????kill?-9?$pid ??
  • ??????else??
  • ??????????$JAVA?$TOOLS_ARGS?com.starit.ueap.common.shell.StopServerTool?-host?127.0.0.1?-port?$JMX_PORT?$@ ??
  • ??????????kill?$pid ??
  • ??????fi ??
  • ??????sleep?3; ??
  • ????done???? ??
  • ????echo?"Stop?$SERVER_NAME?successfully."? ??
  • ????rm?$PID_FILE ??
  • } ??
  • ??
  • function?status(){ ??
  • ????if?running;?then ??
  • ???????echo?"$SERVER_NAME?is?running."??
  • ????else??
  • ???????echo?"$SERVER_NAME?was?stopped."?? ??
  • ????fi ??
  • } ??
  • ? ??
  • function?help()?{ ??
  • ????echo?"Usage:?server.sh?{start|status|stop|restart|reload}"?>&2??
  • ????echo?"???????start:?????????????start?the?$SERVER_NAME?server"??
  • ????echo?"???????stop:??????????????stop?the?$SERVER_NAME?server"??
  • ????echo?"???????restart:???????????restart?the?$SERVER_NAME?server"??
  • ????echo?"???????status:????????????get?$SERVER_NAME?current?status,running?or?stopped."??
  • } ??
  • ??
  • command=$1??
  • shift?1??
  • case?$command?in ??
  • ????start) ??
  • ????????start_server?$@; ??
  • ????????;;???? ??
  • ????stop) ??
  • ????????stop_server?$@; ??
  • ????????;; ??
  • ????status) ??
  • ????????status?$@; ??
  • ????????;;? ??
  • ????restart) ??
  • ????????$0?stop?$@ ??
  • ????????$0?start?$@ ??
  • ????????;; ??
  • ????help) ??
  • ????????help; ??
  • ????????;; ??
  • ????*) ??
  • ????????help; ??
  • ????????exit?1; ??
  • ????????;; ??
  • esac??
  • #!/bin/bashif [ -z "$BASE_DIR" ] ; thenPRG="$0"# need this for relative symlinkswhile [ -h "$PRG" ] ; dols=`ls -ld "$PRG"`link=`expr "$ls" : '.*-> \(.*\)$'`if expr "$link" : '/.*' > /dev/null; thenPRG="$link"elsePRG="`dirname "$PRG"`/$link"fidoneBASE_DIR=`dirname "$PRG"`/..# make it fully qualifiedBASE_DIR=`cd "$BASE_DIR" && pwd`#echo "collect master is at $BASE_DIR" fisource $BASE_DIR/bin/env.shAS_USER=`whoami` LOG_DIR="$BASE_DIR/logs" LOG_FILE="$LOG_DIR/server.log" PID_DIR="$BASE_DIR/logs" PID_FILE="$PID_DIR/.run.pid"function running(){if [ -f "$PID_FILE" ]; thenpid=$(cat "$PID_FILE")process=`ps aux | grep " $pid " | grep -v grep`;if [ "$process" == "" ]; thenreturn 1;elsereturn 0;fielsereturn 1fi }function start_server() {if running; thenecho "$SERVER_NAME is running."exit 1fimkdir -p $PID_DIRtouch $LOG_FILEmkdir -p $LOG_DIRchown -R $AS_USER $PID_DIRchown -R $AS_USER $LOG_DIRecho "$JAVA $UEAP_JVM_ARGS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false \-Dcom.sun.management.jmxremote.port=$JMX_PORT $STARTUP_CLASS"sleep 1nohup $JAVA $UEAP_JVM_ARGS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false \-Dcom.sun.management.jmxremote.port=$JMX_PORT $STARTUP_CLASS 2>&1 >>$LOG_FILE & echo $! > $PID_FILEchmod 755 $PID_FILEsleep 1;tail -F $LOG_FILE }function stop_server() {if ! running; thenecho "$SERVER_NAME is not running."exit 1ficount=0pid=$(cat $PID_FILE)while running;dolet count=$count+1echo "Stopping $SERVER_NAME $count times"if [ $count -gt 5 ]; thenecho "kill -9 $pid"kill -9 $pidelse$JAVA $TOOLS_ARGS com.starit.ueap.common.shell.StopServerTool -host 127.0.0.1 -port $JMX_PORT $@kill $pidfisleep 3;done echo "Stop $SERVER_NAME successfully." rm $PID_FILE }function status(){if running; thenecho "$SERVER_NAME is running."elseecho "$SERVER_NAME was stopped." fi }function help() {echo "Usage: server.sh {start|status|stop|restart|reload}" >&2echo " start: start the $SERVER_NAME server"echo " stop: stop the $SERVER_NAME server"echo " restart: restart the $SERVER_NAME server"echo " status: get $SERVER_NAME current status,running or stopped." }command=$1 shift 1 case $command instart)start_server $@;;; stop)stop_server $@;;;status)status $@;;; restart)$0 stop $@$0 start $@;;help)help;;;*)help;exit 1;;; esac

    ?4:執行mvn install或者mvn package 可以再target生成tar.gz后綴文件。提交到linux系統,解壓縮即可運行。(不考慮window環境)

    ?

    • 大小: 37 KB
    • 查看圖片附件

    總結

    以上是生活随笔為你收集整理的java项目构建部署包的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。