我们的 Spring Boot(带 Gradle)应用程序有上游,托管在云端。因此,当我们需要从本地机器与他们通信时,我们使用 SSH 隧道在本地测试应用程序。
因此,通常情况下,如果我需要测试需要来自受限区域的服务器的内容,我会执行以下操作:
1. 从 cmd 控制台使用 ssh -N myhost
启动 SSH 隧道
2. 使用 Soring Boot Run/Debug 配置从 Intellij Idea 运行应用程序。
3. 完成后关闭隧道(在命令行窗口中按Ctrl+C)。
我的问题:
- 我可以直接从 IntelliJ Idea 执行这些步骤,设置一些运行/调试配置来启动隧道,运行应用程序,并在我的应用程序停止运行后关闭它吗?
- 我可以在 Gradle 中设置相同的内容,创建一些隧道任务并像这样运行它吗:
./gradlew startTunnel bootRun
提前致谢。
最佳答案
您可以尝试使用 Gradle spawn plugin像描述的那样 here
定义这些任务(需要 -v
选项来匹配 Authentication succeeded 字符串):
import com.wiredforcode.gradle.spawn.*
task startSSHTunnel(type: SpawnProcessTask) {
command "ssh -N -v myhost"
ready 'Authentication succeeded' // this is printed by the SSH session's debug trace when connection has been successful
}
task stopSSHTunnel(type: KillProcessTask)
bootRun.finalizedBy stopSSHTunnel
然后运行它:
./gradlew startSSHTunnel bootRun
也许你需要设置一些调整,但主要精神就在这里。
关于java - Intellij Idea 运行/调试配置和 Gradle 任务中的 SSH 隧道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53636863/