【IDEA】自动部署SpringBoot Jar包到远程服务器并通过脚本启动jar

文章目录

前言

IDEA版本: 2019.3

一.Deployment使用

IDRA原生的Deployment不需要离开idea就可以直接将项目部署到远程运行,同时拥有远程视图窗口…

温馨小提示:其它的一些连接软件还有如下等

1.打开连接远程服务器信息:Tools->Deployment->Configuration

2. 选择为SFTP,并设置配置信息名称

  1. 配置连接远程服务器信息

配置好之后Tools->Deployment->Configuration->Browse Romote Host查看服务器文件目录文件

并且可以右键服务器上的文件进行增删改查

也可以通过项目右击Deployment上传文件到服务器,此时文件会上传到之前 Deployment path 映射的目录下。

也可以直接通过idea查看映射到服务器上的文件和本地路径中的文件是否相同

二.ssh使用

Tools->Start SSH session 配置ssh开启远程终端连接

  1. 配置ssh信息

  2. 可沿用Deployment配置信息

三.启动脚本

将如下脚本拷贝到远程服务器jar包同目录,然后运行sh start.sh命令启动脚本即可启动服务(此处有个坑,如果自己在idea中编写脚本请将换行符设置为linux下的LF格式)。

#!/usr/bin/env bash
#!/bin/sh
RESOURCE_NAME=cnntest-1.0.0.jar #设置启动jar文件名
 
tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Stop Process...'
kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpid
else
echo 'Stop Success!'
fi
 
tpid=`ps -ef|grep $RESOURCE_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'App is running.'
else
    echo 'App is NOT running.'
fi
 
rm -f tpid
nohup java -jar ./$RESOURCE_NAME  & tail -f nohup.out
echo $! > tpid
echo Start Success!

  1. chmod +x start.sh(第一次运行的时候获取一下超级管理员权限)然后以后每次运行直接./start.sh 即可

  2. 拷贝脚本的时候运行的时候如果报错,应该是文件格式转换的问题

bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory

可以 yum -y install dos2unix 然后 dos2unix start.sh重新运行脚本

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 如果您在使用 Spring Boot 时依赖了两个具有相同类路径的 JAR 包,您可以通过在 Maven 或 Gradle 的依赖中指定版本号,来选择特定的 JAR 包。 例如,假设您有以下 Maven 依赖声明: ``` <dependency> <groupId>com.example</groupId> <artifactId>example-lib</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>example-lib</artifactId> <version>2.0.0</version> </dependency> ``` 在这个例子中,您可以选择使用 1.0.0 版本的 `example-lib` JAR 包,如下所示: ``` <dependency> <groupId>com.example</groupId> <artifactId>example-lib</artifactId> <version>1.0.0</version> </dependency> ``` 或者,您可以选择使用 2.0.0 版本的 `example-lib` JAR 包,如下所示: ``` <dependency> <groupId>com.example</groupId> <artifactId>example-lib</artifactId> <version>2.0.0</version> </dependency> ``` 请注意,如果您选择使用一个特定版本的 JAR 包,则在代码中使用的类应该来自您选择的版本,而不是其他版本的 JAR 包。 ### 回答2: 在使用Spring Boot时,如果依赖了两个具有相同类路径的jar包,我们可以通过以下几种方式指定要使用的jar包: 1. 排除不需要的jar包:在pom.xml文件中,可以通过<exclusions>标签排除不需要的jar包。 例如: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> ``` 通过以上配置,我们将排除了spring-boot-starter-web的默认tomcat依赖。 2. 明确指定需要的jar包版本:在pom.xml文件中,可以通过<dependencyManagement>标签指定特定的jar包版本。 例如: ``` <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.5.2</version> </dependency> </dependencies> </dependencyManagement> ``` 通过以上配置,我们明确指定了spring-boot-starter-tomcat的版本为2.5.2,保证了使用的是指定版本的jar包。 3. 调整jar包的顺序:在构建项目时,可以调整jar包的顺序,使指定的jar包被优先加载。这可以通过在IDE的构建路径或构建脚本中进行配置。 需要注意的是,对于类路径相同的jar包,我们需要根据需求来选择合适的jar包,确保项目的正确运行和功能实现。在选择jar包时,可以参考官方文档、社区推荐或者项目的特殊要求。 ### 回答3: 当依赖的两个jar包的类路径一样,可以通过在pom.xml文件中使用<exclusions>标签来排除某个jar包的依赖,从而选择指定的jar包。 首先,在pom.xml文件中定位到依赖的部分。找到需要排除的jar包的依赖项,然后在此依赖项上方,添加<exclusions>标签,如下所示: <dependency> <groupId>com.example</groupId> <artifactId>dependency1</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>com.example</groupId> <artifactId>dependency2</artifactId> </exclusion> </exclusions> </dependency> 在<exclusions>标签内指定需要排除的jar包的groupId和artifactId。这样在构建项目时,Maven会排除这个特定的jar包依赖。 另外,也可以通过在<dependency>标签内指定需要依赖的jar包的版本号来选择指定的jar包,如下所示: <dependency> <groupId>com.example</groupId> <artifactId>dependency1</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>dependency2</artifactId> <version>2.0.0</version> </dependency> 以上是两种常见的方法,根据具体的项目需求决定使用哪种方法来选择指定的jar包

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星哲最开心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值