java,maven工程打tar.gz包執行main方法
阿新 • • 發佈:2018-05-09
sport desc pre remote without pil deploy can 周期
一,需要在pom.xml文件添加plugin,
項目目錄結構
1 <build> 2 <plugins> 3 4 <plugin> 5 <artifactId>maven-assembly-plugin</artifactId> 6 <version>2.6</version> 7 <configuration> 8 <!--G:\IdeaWorkspace\sic_platform\probe\chromProbe\src\assembly\assembly.xml--> 9<!--描述文件路徑--> 10 <descriptor>src/assembly/assembly.xml</descriptor> 11 </configuration> 12 <executions> 13 <execution> 14 <id>make-assembly</id> 15<!-- 綁定到package生命周期階段上 --> 16 <phase>package</phase> 17 <goals> 18 <goal>single</goal> 19 </goals> 20 </execution> 21</executions> 22 </plugin> 23 24 </plugins> 25 </build>
bin下是啟動腳本,conf下是配置文件,裏面可以配置腳本參數
二,assembly.xml文件如下:還有很多配置可以參考官網:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
1 <!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License, 2 Version 2.0 (the "License"); - you may not use this file except in compliance 3 with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 4 - - Unless required by applicable law or agreed to in writing, software - 5 distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 6 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 7 License for the specific language governing permissions and - limitations 8 under the License. --> 9 <assembly> 10 <id>assembly</id> 11 <formats> 12 <format>tar.gz</format> 13 </formats> 14 <includeBaseDirectory>true</includeBaseDirectory> 15 <fileSets> 16 <fileSet> 17 <directory>src/assembly/bin</directory> 18 <outputDirectory>bin</outputDirectory> 19 <!-- <fileMode>0100</fileMode> --> 20 </fileSet> 21 <fileSet> 22 <directory>src/assembly/conf</directory> 23 <outputDirectory>conf</outputDirectory> 24 </fileSet> 25 <!--如果工程依賴其他項目的conf,可以在這裏添加--> 26 <!--<fileSet> 27 28 <directory>../pre-api/src/main/assembly/conf</directory> 29 <outputDirectory>conf</outputDirectory> 30 </fileSet> 31 <fileSet> 32 <directory>../pre-api/src/main/assembly/lib</directory> 33 <outputDirectory>lib</outputDirectory> 34 </fileSet>--> 35 </fileSets> 36 <dependencySets> 37 <dependencySet>
<!-- 將工程的依賴包打包到lib目錄下。 --> 38 <outputDirectory>lib</outputDirectory> 39 </dependencySet> 40 </dependencySets> 41 </assembly>
三,下面是start.sh的腳本,只需改動啟動的main方法的路徑即可 : com.topsec.sic.collector.urlProbe.HeadlessChromeTest
會輸出日誌logs,可以查看logs裏的日誌文件判斷main方法是否執行!
1 #!/bin/bash 2 cd `dirname $0` 3 BIN_DIR=`pwd` 4 cd .. 5 DEPLOY_DIR=`pwd` 6 CONF_DIR=$DEPLOY_DIR/conf 7 8 SERVER_NAME=`sed ‘/application.host.name/!d;s/.*=//‘ conf/config.properties | tr -d ‘\r‘` 9 10 LOGS_DIR=$DEPLOY_DIR/logs 11 if [ ! -d $LOGS_DIR ]; then 12 mkdir $LOGS_DIR 13 fi 14 STDOUT_FILE=$LOGS_DIR/stdout.log 15 16 LIB_DIR=$DEPLOY_DIR/lib 17 LIB_JARS=`ls $LIB_DIR|grep .jar|awk ‘{print "‘$LIB_DIR‘/"$0}‘|tr "\n" ":"` 18 19 JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Duser.timezone=Asia/Shanghai " 20 JAVA_DEBUG_OPTS="" 21 if [ "$1" = "debug" ]; then 22 JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n " 23 fi 24 JAVA_JMX_OPTS="" 25 if [ "$1" = "jmx" ]; then 26 JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false " 27 fi 28 JAVA_MEM_OPTS="" 29 BITS=`java -version 2>&1 | grep -i 64-bit` 30 if [ -n "$BITS" ]; then 31 # JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 " 32 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 " 33 else 34 # JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC " 35 JAVA_MEM_OPTS=" -server -Xms256m -Xmx256m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC " 36 fi 37 38 echo -e "Starting the $SERVER_NAME ...\c" 39 # nohup java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS com.topsec.trd.scanner.leack.check.Main > $STDOUT_FILE 2>&1 & 40 java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $CONF_DIR:$LIB_JARS com.topsec.sic.collector.urlProbe.HeadlessChromeTest > $STDOUT_FILE 2>&1 41 42 COUNT=0 43 while [ $COUNT -lt 1 ]; do 44 echo -e ".\c" 45 sleep 1 46 47 COUNT=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk ‘{print $2}‘ | wc -l` 48 49 if [ $COUNT -gt 0 ]; then 50 break 51 fi 52 done 53 54 echo "OK!" 55 PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk ‘{print $2}‘` 56 echo "PID: $PIDS" 57 echo "STDOUT: $STDOUT_FILE"
四,上傳文件到服務器,執行腳本
使用idea點擊clean,package打包,在target目錄下生成sic-probe-chromProbe-assembly.tar,將文件上傳到服務器,執行命令:tar -zxvf sic-probe-chromProbe-assembly.tar.gz解壓,解壓後的目錄結構如下:
1 drwxr-xr-x 6 sqy root 48 May 9 17:31 sic-probe-chromProbe 2 -rw-r--r-- 1 sqy root 34145112 May 9 17:30 sic-probe-chromProbe-assembly.tar.gz 3 [root@node105 prep-url-sqy]# cd sic-probe-chromProbe 4 [root@node105 sic-probe-chromProbe]# ll 5 total 4 6 drwxr-xr-x 2 sqy root 21 May 9 11:35 bin 7 drwxr-xr-x 2 sqy root 30 May 9 17:00 conf 8 drwxr-xr-x 2 sqy root 4096 May 9 17:30 lib
bin下就是啟動腳本,需要註意的是,腳本只有讀寫權限,需要執行chmod命令給腳本加上執行權限
1 -rw-r--r-- 1 root root 2534 May 9 11:35 start.sh 2 [root@node105 bin]# chmod 755 start.sh 3 [root@node105 bin]# ll 4 total 4 5 -rwxr-xr-x 1 root root 2534 May 9 11:35 start.sh
啟動腳本查看日誌就行了
1 $ ./start.sh
java,maven工程打tar.gz包執行main方法