首先配置本地启动项
在IDEA中对启动项做一些配置:
添加一个新的启动项:
填写配置信息:
自动生成的脚本:
生成的脚本在虚拟机中最终运行项目时会转化为:
java jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 xxx.jar
远程调试部署脚本
仅仅本地配置还不够,我们还需要在虚拟机中部署时,添加一段配置到部署脚本中,这段配置IDEA已经提供给我们了,见上文最后一图,但是以后的项目可能会基于各种部署工具,我们以Jenkins+docker为例介绍一下怎么用,进入到Jenkins的工作台启动该工程
该工程主要是执行一段shell脚本,构建docker镜像并运行
shell脚本中的startup.sh如下:
#! /bin/sh
cd /usr/local/src/script || exit 1
BASE_PATH='/usr/local/src/jenkins/workspace/tjxt-dev-build'
PROJECT_NAME=""
PROJECT_PATH=''
CONTAINER_NAME=""
JAVA_OPTS="-Xms300m -Xmx300m"
PORT=8080
DEBUG_PORT=0
while getopts "c:n:d:p:o:a:" opt; do
case $opt in
c)
CONTAINER_NAME=$OPTARG
;;
n)
PROJECT_NAME=$OPTARG
;;
d)
PROJECT_PATH=$OPTARG
;;
p)
PORT=$OPTARG
;;
o)
[ -n "$OPTARG" ] && JAVA_OPTS=$OPTARG
;;
a)
[ -n "$OPTARG" ] && DEBUG_PORT=$OPTARG
;;
?)
echo "unkonw argument"
exit 1
;;
esac
done
if [ "$DEBUG_PORT" = "0" ]; then
JAVA_OPTS=$JAVA_OPTS
else
JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
fi
IMAGE_NAME="${CONTAINER_NAME}:latest"
echo "copy xx.jar from ${BASE_PATH}/${PROJECT_PATH}"
rm -f app.jar
cp ${BASE_PATH}/${PROJECT_PATH}/target/${PROJECT_NAME}.jar ./app.jar || exit 1
echo "begin to build ${PROJECT_NAME} image !!"
[ -n "`docker ps -a | grep ${CONTAINER_NAME}`" ] && docker rm -f ${CONTAINER_NAME}
[ -n "`docker images | grep ${CONTAINER_NAME}`" ] && docker rmi ${IMAGE_NAME}
docker build -t ${IMAGE_NAME} . || exit 1
echo "${PROJECT_NAME} image build success,java_opts = $JAVA_OPTS !!^_^"
echo "begin to create container ${CONTAINER_NAME},port: ${PORT} !!"
if [ "$DEBUG_PORT" = "0" ]; then
echo "run in normal mode"
docker run -d --name ${CONTAINER_NAME} \
-p "${PORT}:${PORT}" \
-e JAVA_OPTS="${JAVA_OPTS}" \
--memory 300m --memory-swap -1 \
--network heima-net ${IMAGE_NAME} \
|| exit 1
else
echo "run in debug mode"
docker run -d --name ${CONTAINER_NAME} \
-p "${PORT}:${PORT}" \
-p ${DEBUG_PORT}:5005 \
-e JAVA_OPTS="${JAVA_OPTS}" \
--network heima-net ${IMAGE_NAME} \
|| exit 1
fi
echo "container is running now !! ^_^"
exit 0
最终的目的也是执行:
java jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 xxx.jar
最后在IDEA中启动
运行后可以看到本地正在连接虚拟机中的JVM:
最后,就可以远程调试了。
温馨提示:本文最后更新于
2024-02-04
,若文件或内容有错误或已失效,请在下方留言。© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容