Maven打包并生成运行脚本的示例代码

网友投稿 481 2022-11-30


Maven打包并生成运行脚本的示例代码

1.定义插件

2.4

2.4

3.7.0

org.apache.maven.plugins

maven-compiler-plugin

${maven-compiler-plugin.version}

${java.version}

${java.version}

${project.build.sourceEncoding}

compile

compile

org.apache.maven.plugins

maven-jar-plugin

${maven-jar-plugin.version}

true

true

org.apache.maven.plugins

maven-assembly-plugin

${maven-assembly-plugin.version}

${project.basedir}/../assembly/assembly.xml

make-assembly

package

single

2.assembly配置

bin

tar.gz

false

lib

false

runtime

false

lib

false

${project.basedir}/../scripts

bin

0644

0755

true

${project.basedir}/src/main/resources

config

0644

0755

*.xml

*.json

*.properties

true

lib

*.jar

${project.basedir}

doc

README*

LICENSE*

NOTICE*

&lthttp://;/fileSet>

3.脚本

#!/bin/sh

#server id -- change

SERVER_ID=

#java home

JAVA_HOME=

#java command

JAVA_CMD=`which java`

#jvm option

JVM_OPT="-Xmx1024M -Xms512M -server -XX:+PrintGCDetails -XX:+PrintGCDateStamps"

#jar name

JAR=${project.artifactId}-${project.version}.jar

#main class

MAIN_CLASS=${MainClass}

# main class args

ARGS="${StartArgs}"

#environment

ENVIRONMENT=${profiles.environment}

#cd working path

cd_working_path(){

cd `dirname $0`

cd ..

}

#jar

jar(){

WK_PATH=`pwd`

/usr/bin/nohup ${JAVA_CMD} -Denvironment=${ENVIRONMENT} -Dlog4j.configurationFile=${WK_PATH}/config/log4j2.xml ${JVM_OPT} -cp ${WK_PATH}/lib/${JAR}:${WK_PATH}/lib/* ${MAIN_CLASS} ${ARGS} >/dev/null 2>&1 &

}

#get pid

get_pid(){

echo `ps -ef | grep ${JAR} | grep server_id=${SERVER_ID} |grep -v 'grep' |awk '{print $2}'`

}

#check

check(){

#check server id

if [ ! -n "$SERVER_ID" ];then

echo "Please set up the server id 'SERVER_ID'"

exit

fi

}

#start service

start(){

#check

check

#check pid

PID=`get_pid`

if [ -n "$PID" ];then

echo "Process exists, PID >> "${PID}

exit

fi

#check jaxKhQUzrva

if [ -n "$JAVA_HOME" ];then

JAVA_CMD=${JAVA_HOME}/bin/java

fi

#start service

${JAVA_CMD} -version

jar

#check

if [ $? -ne 0 ];then

echo "Service startup failed."

exit

fi

#check service

PID=`get_pid`

if [ ! -n "$PID" ];then

echo "Service startup failed."

else

echo "Service startup success, Current environment is ${ENVIRONMENT} , PID >> "${PID}

fi

}

#stop service

stop(){

#check

check

#check pid

PID=`get_pid`

if [ ! -n "$PID" ];then

echo "Process not exists."

else

kill ${PID}

echo "Kill pid >> '$PID'"

if [ $? -ne 0 ];then

echo "Service shutdown failed."

exit

else

echo "Service shutdown success."

fi

fi

}

#restart service

restart(){

#stop service

stop

COUNT=0

while true

do

PID=`get_pid`

if [ ! -n "$PID" ];then

#start service

start

break

else

let COUNT++

echo "Restarting..."

if [ ${COUNT} -eq 3 ];then

echo "Restart error"

exit

fi

fi

sleep 3

done

}

#check state

state(){

PID=`get_pid`

if [ ! -n "$PID" ];then

echo "Service not exists."

else

echo "Service status is normal, PID >> '$PID'"

fi

}

#main

main(){

#cd working path

cd_working_path

if [ ! -n "$1" ];then

echo "***********************************************"

echo "* start : Start service *"

echo "* stop : Stop service *"

echo "* restart : Restart service *"

echo "* state : Check service state *"

echo "***********************************************"

read -p "Please choose >> ": CASE

PARAMETER=${CASE}

else

PARAMETER=$1

fi

case "$PARAMETER" in

start)

start

;;

stop)

stop

;;

restart)

restart

;;

state)

state

;;

*)

main

;;

esac

}

main $1

PS:下面看下Maven打包生成可运行bat/sh脚本文件

利用Maven的appassembler-maven-plugin插件,就可以实现自动打包可运行的脚本,还可以跨平台。

  org.codehaus.mojo

  appassembler-maven-plugin

1.1.1

  flat

  lib

  src/main/resources/conf

  

conf

true

true

startup

${project.build.directory}/server

-Xms768m -Xmx768m -XX:PermSize=128m

  -XX:MaxPermSize=256m -XX:NewSize=192m -XX:MaxNewSize=384m

  windows

   unix

  

  com.coderli.onecoder.server.HypervisorServer

    startup

  

然后选择要编译的工程,右键->maven build… 命令如下图:

package appassembler:assemble

然后执行run,一个可执行的脚本文件就生成好了。startup.bat是windows下的,startup.sh是linux下的

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Boot Actuator端点相关原理解析
下一篇:Java8新特性之接口中的默认方法和静态方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~