java,maven工程打tar.gz包执行main方法

允我心安 提交于 2020-12-18 06:43:51

一、需要在pom.xml文件添加plugin

项目目录结构

 

pom.xml

 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                        <!-- 生成的tar.gz文件的名字,如果没有设置就默认用pom文件里的artifactId+version-->
12                        <finalName>${project.name}-${project.version}</finalName>                       
13                        <!--属性控制是否在生成的打包文件的文件名中包含assembly id-->
14                        <appendAssemblyId>false</appendAssemblyId>
15                 </configuration>
16                 <executions>
17                     <execution>
18                         <id>make-assembly</id>
19                         <!-- 绑定到package生命周期阶段上 -->
20                         <phase>package</phase>
21                         <goals>
22                             <goal>single</goal>
23                         </goals>
24                     </execution>
25                 </executions>
26             </plugin>
27       
28         </plugins>
29 </build>

bin下是启动脚本,conf下是配置文件,里面可以配置脚本参数

二、assembly配置

如下:还有很多配置可以参考官网: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"

关于nohup的用法可参考  https://www.cnblogs.com/jinxiao-pu/p/9131057.html  ,使命令永久的在后台执行。

四、上传文件到服务器,执行脚本

使用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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!