Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH tunnel in Intellij Idea Run/Debug config and in Gradle tasks

Our Spring Boot (with Gradle) application has upstreams, that are hosted in the cloud. So, when we need to communicate with them from the local machine, we use SSH tunnels to test app locally.

So, usually, if I need to test something that requires a server from a restricted zone, I do following:
1. Start SSH tunnel using ssh -N myhost from cmd console
2. Run application from Intellij Idea using Soring Boot Run/Debug configuration.
3. Close tunnel (Ctrl+C in command line window) after I finished.

My questions:
- Can I do these steps directly from IntelliJ Idea, setting up some Run/Debug config that will start the tunnel, run the app, and close it after my application is dead?
- Can I set up the same in Gradle, create some task for tunneling and run it like:
./gradlew startTunnel bootRun

Thanks in advance.

like image 625
Viktor Molokanov Avatar asked Dec 05 '18 16:12

Viktor Molokanov


People also ask

How do I run Gradle task in debug mode?

Just run gradle run --debug-jvm . This starts the application in remote debug mode, and you can attach with any remote debugger, e.g., Eclipse, on port 5005 . Assuming you use Eclipse as IDE: In Eclipse, go on your Project -> Debug as... -> Debug Configuration -> Remote Java Application.

How do I get to the run debug configuration in IntelliJ?

From the main menu, select Run | Edit Configurations. Alternatively, press Alt+Shift+F10 , then 0 . on the toolbar or press Alt+Insert . The list shows the run/debug configuration templates.


1 Answers

You can try using the Gradle spawn plugin like described here

Define theses tasks (the -v option is needed to match the Authentication succeeded string) :

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

And then just run it :

./gradlew startSSHTunnel bootRun

Maybe you would have to set up some adjustments but the main spirit is here.

like image 70
ToYonos Avatar answered Oct 20 '22 16:10

ToYonos