Java project deployment script and java project deployment script
I remember my internship two years ago, Ji Ge said that if a programmer did perfect tasks in some categories, there would be fewer tasks for others, including testing, O & M, and convenience, for your convenience .. This time, I had the opportunity to refactor a project and separate the front and back ends. After analyzing some requirements and later planning, I decided to give up the springMVC set that was previously "fat, try using the increasingly popular microservice architecture over the past two years. Of course, the first thing to do is to use spring-boot to take on the burden. All the requirements in the early stage have been completed. This test has passed (we strongly recommend that java programmers write unit tests by themselves, which will simplify the development and maintenance behind us. Although it may take some time in the early stage, but it is worth noting that it is inconvenient to prepare and put it in the production environment. Because it is inconvenient to set parameters, start and stop, we decided to write the script for ease of use. Based on this willingness, with this article, I will share a common shell script (PS: I modified it from another version), and consider some people who are not familiar with the shell script, here I will select some for detailed explanation. Well, you can simply start the script first.
1. project directory structure
According to maven specifications, java scripts are generally placed under the directory src --> main --> scripts.
The directory after packaging the maven plug-in is usually as follows:
Root directory
| ---- Bin // place the shell script
| ---- Lib // place the dependent jar package of the java Project
| ---- *****-*****. Jar // core jar package to be released in this project
| ---- Conf // place the configuration file of the project
Generally, java projects place the dependent jar packages, jar packages to be executed, and configuration files under the classpath path.
2. Start Script 1 #! /Bin/bash 2 # version: 1.0.0 3 # modify: 2016/07/13 4 5 cd 'dirname $ 0' # This command is only useful in script files, return to the directory where the script file is stored, and locate the relative location of the program to be run based on the directory (except for the absolute location ). 6 BIN_DIR = 'pwd' # obtain the current path, that is, the bin directory 7 cd under the root path of the project .. # Return to the project's root directory 8 DEPLOY_DIR = 'pwd' # Save the root directory 9 CONF_DIR = $ DEPLOY_DIR/conf # Save the configuration file directory 10 SERVER_NAME = 'basename $ DEPLOY_DIR '# obtain the name of the current directory, the basename command intercepts the root path, for example, basename/data/had/hadoop. The result is hadoop, the project name is 11 PIDS = 'ps-ef | grep java | grep "$ CONF_DIR" | awk '{print $2} ''# Check whether the project has been started, obtain the PID, print out all processes, filter out the java processes, and check whether there are any processes with the project path. If yes, extract PID12 if [-n "$ P IDS "]; then # determines whether The string PIDS is null. If it is not null, 13 echo" ERROR: The $ SERVER_NAME already started already exists in The process! "# Prompt message about the existence of a process 14 echo" PID: $ PIDS "# print the PID value 15 exit 1 # script execution end 16 fi17 18 LOGS_DIR = $ DEPLOY_DIR/logs # Set the log file output directory 19 20 if [! -D $ LOGS_DIR]; then # If the directory does not exist, create the directory 21 mkdir $ LOGS_DIR22 fi23 STDOUT_FILE = $ LOGS_DIR/stdout. log # create a standard log output file 24 25 LIB_DIR = $ DEPLOY_DIR/lib # obtain the storage directory of the jar package for the java project dependency 26 LIB_JARS = 'ls $ LIB_DIR | grep. jar | awk '{print "' $ LIB_DIR '/" $0}' | tr "\ n" ":" '# traverse the jar package of the entire directory, add it in 27 MAIN_CLASS_JAR = 'ls $ DEPLOY_DIR | grep. jar | awk '{print "' $ DEPLOY_DIR '/" $0}' | tr "\ n": "'# obtain the jar package to be deployed 28 JAVA_OPTS =" -Djava.net. prefer12004stack = tru E-Dlog. home = $ LOGS_DIR "# Set java startup parameter 29 JAVA_MEM_OPTS =" "# Set java JVM parameter 30 BITS = 'java-version 2> & 1 | grep-I 64-bit '31 if [-n "$ BITS"]; then32 JAVA_MEM_OPTS = "-server-Xms2g-Xmx2g-XX: PermSize = 128 m-XX: MaxPermSize = 128 m-XX: Export vorratio = 6-XX: + UseConcMarkSweepGC-XX: + CMSParallelRemarkEnabled-XX: + UseCMSInitiatingOccupancyOnly-XX: CMSInitiatingOccupancyFraction = 80-XX: + ScavengeBeforeFullGC-XX: + CM SScavengeBeforeRemark-XX: + PrintGCDateStamps-verbose: gc-XX: + PrintGCDetails-Xloggc: gc. log-XX: + UseGCLogFileRotation-XX: NumberOfGCLogFiles = 10-XX: GCLogFileSize = 100 M-XX: + HeapDumpOnOutOfMemoryError-XX: HeapDumpPath = dump. hprof "33 else34 JAVA_MEM_OPTS ="-server-Xms1g-Xmx1g-XX: PermSize = 128 m-XX: MaxPermSize = 128 m-XX: Export vorratio = 2-XX: + UseParallelGC "35 fi36 37 echo-e" Starting the $ SERVER_NAME... \ C "# print startup log 38 nohup java $ JAVA_OPTS $ JAVA_MEM_OPTS-classpath $ CONF_DIR: $ LIB_JARS: $ MAIN_CLASS_JAR com. juanpi. lux. trace. ws. luxTraceWsApplication> $ STDOUT_FILE 2> & 1 & # Start the java project. Set classpath and provide the main class of the project, and redirect the output to 39 40 COUNT = 041 while [$ COUNT-lt 1]; do # Check whether the regular check is successful 42 echo-e ". \ c "# hitting 43 sleep 1 # Wait for a second 44 COUNT = 'ps-f | grep java | grep" $ DEPLOY_DIR "| awk '{print $2}' | wc-l '45 if [$ COUNT -Gt 0]; then46 break47 fi48 done49 50 echo "OK! "51 PIDS = 'ps-f | grep java | grep" $ DEPLOY_DIR "| awk '{print $2}'' # obtain the PID52 echo "PID: $ PIDS "53 echo" STDOUT: $ STDOUT_FILE"View Code 3. Stop Script 1 #! /Bin/bash 2 # version: 1.0.0 3 # modify: 2016/07/13 4 5 cd 'dirname $ 0' 6 BIN_DIR = 'pwd' 7 cd .. 8 DEPLOY_DIR = 'pwd' 9 CONF_DIR = $ DEPLOY_DIR/conf10 11 SERVER_NAME = 'basename $ DEPLOY_DIR '12 13 PIDS = 'ps-ef | grep java | grep "$ CONF_DIR" | awk '{print $2}' '14 if [-z "$ PIDS"]; then15 echo "ERROR: The $ SERVER_NAME does not started! "16 exit 117 fi18 19 if [" $1 "=" dump "]; then20 $ BIN_DIR/dump. sh21 fi22 23 echo-e "Stopping the $ SERVER_NAME... \ c "24 for PID in $ PIDS; do25 kill $ PID>/dev/null 2> & 126 done27 28 COUNT = 029 while [$ COUNT-lt 1]; do30 echo-e ". \ c "31 sleep 132 COUNT = 133 for PID in $ PIDS; do34 PID_EXIST = 'ps-f-p $ PID | grep java '35 if [-n "$ PID_EXIST"]; then36 COUNT = 037 break38 fi39 done40 done41 42 echo" OK! "43 echo" PID: $ PIDS"View Code 4, dump Script 1 #! /Bin/bash 2 # version: 1.0.0 3 # modify: 2016/07/13 4 5 cd 'dirname $ 0' 6 BIN_DIR = 'pwd' 7 cd .. 8 DEPLOY_DIR = 'pwd' 9 CONF_DIR = $ DEPLOY_DIR/conf10 11 SERVER_NAME = 'basename $ DEPLOY_DIR '12 13 PIDS = 'ps-ef | grep java | grep "$ CONF_DIR" | awk '{print $2}' '14 if [-z "$ PIDS"]; then15 echo "ERROR: The $ SERVER_NAME does not started! "16 exit 117 fi18 19 LOGS_DIR = $ DEPLOY_DIR/logs20 if [! -D $ LOGS_DIR]; then21 mkdir $ LOGS_DIR22 fi23 DUMP_DIR = $ LOGS_DIR/dump24 if [! -D $ DUMP_DIR]; then25 mkdir $ DUMP_DIR26 fi27 DUMP_DATE = 'date + % Y % m % d % H % M % s' 28 DATE_DIR = $ DUMP_DIR/$ DUMP_DATE29 if [! -D $ DATE_DIR]; then30 mkdir $ DATE_DIR31 fi32 33 echo-e "Dumping the $ SERVER_NAME... \ c "34 for PID in $ PIDS; do35 jstack $ PID> $ DATE_DIR/jstack-$ PID. dump 2 & gt; & amp; 136 echo-e ". \ c "37 jinfo $ PID> $ DATE_DIR/jinfo-$ PID. dump 2 & gt; & amp; 138 echo-e ". \ c "39 jstat-gcutil $ PID> $ DATE_DIR/jstat-gcutil-$ PID. dump 2 & gt; & amp; 140 echo-e ". \ c "41 jstat-gccapacity $ PID> $ DATE_DIR/jstat-gccapacity-$ PID. dump 2 & gt; & amp; 142 echo-e ". \ c "43 jmap $ PID> $ DATE_DIR/jmap-$ PID. dump 2 & gt; & amp; 144 echo-e ". \ c "45 jmap-heap $ PID> $ DATE_DIR/jmap-heap-$ PID. dump 2 & gt; & amp; 146 echo-e ". \ c "47 jmap-histo $ PID> $ DATE_DIR/jmap-histo-$ PID. dump 2 & gt; & amp; 148 echo-e ". \ c "49 if [-r/usr/sbin/lsof]; then50/usr/sbin/lsof-p $ PID> $ DATE_DIR/lsof-$ PID. dump51 echo-e ". \ c "52 fi53 done54 55 if [-r/bin/netstat]; then56/bin/netstat-an> $ DATE_DIR/netstat. dump 2> & 157 echo-e ". \ c "58 fi59 if [-r/usr/bin/iostat]; then60/usr/bin/iostat> $ DATE_DIR/iostat. dump 2 & gt; & amp; 161 echo-e ". \ c "62 fi63 if [-r/usr/bin/mpstat]; then64/usr/bin/mpstat> $ DATE_DIR/mpstat. dump 2 & gt; & amp; 165 echo-e ". \ c "66 fi67 if [-r/usr/bin/vmstat]; then68/usr/bin/vmstat> $ DATE_DIR/vmstat. dump 2 & gt; & amp; 169 echo-e ". \ c "70 fi71 if [-r/usr/bin/free]; then72/usr/bin/free-t> $ DATE_DIR/free. dum P 2> & 173 echo-e ". \ c "74 fi75 if [-r/usr/bin/sar]; then76/usr/bin/sar> $ DATE_DIR/sar. dump 2 & gt; & amp; 177 echo-e ". \ c "78 fi79 if [-r/usr/bin/uptime]; then80/usr/bin/uptime> $ DATE_DIR/uptime. dump 2 & gt; & amp; 181 echo-e ". \ c "82 fi83 84 echo" OK! "85 echo" DUMP: $ DATE_DIR"View Code
Note: All scripts in this article are close to maven plug-in configuration and are packaged in the attachment. You can download them directly. Http://files.cnblogs.com/files/yanyamin/%E8%84%9A%E6%9C%AC%E5%92%8C%E6%8F%92%E4%BB%B6%E9%85%8D%E7%BD% AE .zip