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

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

项目目录结构

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

pom.xml

 <build>
<plugins> <plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<!--G:\IdeaWorkspace\sic_platform\probe\chromProbe\src\assembly\assembly.xml-->
<!--描述文件路径-->
<descriptor>src/assembly/assembly.xml</descriptor>
<!-- 生成的tar.gz文件的名字,如果没有设置就默认用pom文件里的artifactId+version-->
<finalName>${project.name}-${project.version}</finalName>
<!--属性控制是否在生成的打包文件的文件名中包含assembly id-->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- 绑定到package生命周期阶段上 -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin> </plugins>
</build>

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

二、assembly配置

如下:还有很多配置可以参考官网:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

 <!-- - Copyright 1999-2011 Alibaba Group. - - Licensed under the Apache License,
Version 2.0 (the "License"); - you may not use this file except in compliance
with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0
- - Unless required by applicable law or agreed to in writing, software -
distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the
License for the specific language governing permissions and - limitations
under the License. -->
<assembly>
<id>assembly</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/assembly/bin</directory>
<outputDirectory>bin</outputDirectory>
<!-- <fileMode>0100</fileMode> -->
</fileSet>
<fileSet>
<directory>src/assembly/conf</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<!--如果工程依赖其他项目的conf,可以在这里添加-->
<!--<fileSet> <directory>../pre-api/src/main/assembly/conf</directory>
<outputDirectory>conf</outputDirectory>
</fileSet>
<fileSet>
<directory>../pre-api/src/main/assembly/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>-->
</fileSets>
<dependencySets>
<dependencySet>
<!-- 将工程的依赖包打包到lib目录下。 -->
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>

三、start.sh的脚本,

只需改动启动的main方法的路径即可 : com.topsec.sic.collector.urlProbe.HeadlessChromeTest

会输出日志logs,可以查看logs里的日志文件判断main方法是否执行!

 #!/bin/bash
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf SERVER_NAME=`sed '/application.host.name/!d;s/.*=//' conf/config.properties | tr -d '\r'` LOGS_DIR=$DEPLOY_DIR/logs
if [ ! -d $LOGS_DIR ]; then
mkdir $LOGS_DIR
fi
STDOUT_FILE=$LOGS_DIR/stdout.log LIB_DIR=$DEPLOY_DIR/lib
LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"` JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Duser.timezone=Asia/Shanghai "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
# 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 "
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 "
else
# JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
JAVA_MEM_OPTS=" -server -Xms256m -Xmx256m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi echo -e "Starting the $SERVER_NAME ...\c"
# 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 &
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 COUNT=0
while [ $COUNT -lt 1 ]; do
echo -e ".\c"
sleep 1 COUNT=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l` if [ $COUNT -gt 0 ]; then
break
fi
done echo "OK!"
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
echo "PID: $PIDS"
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解压,解压后的目录结构如下:

 drwxr-xr-x 6 sqy root       48 May  9 17:31 sic-probe-chromProbe
-rw-r--r-- 1 sqy root 34145112 May 9 17:30 sic-probe-chromProbe-assembly.tar.gz
[root@node105 prep-url-sqy]# cd sic-probe-chromProbe
[root@node105 sic-probe-chromProbe]# ll
total 4
drwxr-xr-x 2 sqy root 21 May 9 11:35 bin
drwxr-xr-x 2 sqy root 30 May 9 17:00 conf
drwxr-xr-x 2 sqy root 4096 May 9 17:30 lib

bin下就是启动脚本,需要注意的是,脚本只有读写权限,需要执行chmod命令给脚本加上执行权限

 -rw-r--r-- 1 root root 2534 May  9 11:35 start.sh
[root@node105 bin]# chmod 755 start.sh
[root@node105 bin]# ll
total 4
-rwxr-xr-x 1 root root 2534 May 9 11:35 start.sh

启动脚本查看日志就行了

 $ ./start.sh
上一篇:使用Docker构建企业Jenkins CI平台


下一篇:基于Docker构建企业Jenkins CI平台