Start IntelliJ run-configuration from command line - kotlin

I work on a Kotlin-based cloud microservice. From time to time I need to run it in the IntelliJ debugger, but we often accidentally break the "local debug flow". There is no pipeline that checks if the IntelliJ run-configuration and the somewhat complex set of cloud-simulating proxies and mock-servers are in sync with the code base.
I would like to write a script that helps me check if the local debug flow works. The script should do the following:
build the project
start up the "mock cloud"
start the service locally using an IntelliJ run-configuration
run a Postman-collection against the locally running service
I need help with step 3 (and possibly step 1): How can I start the IntelliJ run-configuration via command line? I am using IntelliJ IDEA 2021.2 and MacOS 12.3.1.

There is no built-in way to start a Run Configuration from the command line. Instead, you should execute java/kotlin binary with classpath etc.
For example, for "Application" configuration it is needed to run the command like that:

Related

Unable to add External Tool to run Before Configuration Launch IntelliJ

Introduction
I've looked around and haven't seen anyone else having my issue.
Basically, I am wanting to run a start.bat file to start up my server before I start up my debugging session. I currently have my debugging inside of an IntelliJ Configuration and it works well. The problem is that in the options for my configuration I can't seem to add an "External Tools" task to the Before Launch section. I was able to add a maven task here previously. However, when I follow the following steps, the "start.bat" is not added.
First I go into Settings->Tools->External Tools to add my Spigot-Server tool which basically calls the start.bat file inside of a test-server directory.
Snippet of External Tools
Second, I go into my "Debug Spigot" configuration and at the bottom, I see a "Before Launch" Section.
Snippet of Spigot Debug options
I click on the + inside of that section to add a new task, and I click on "External Tools"
Snippet of adding External Tools Task
And then I make sure my Spigot-Server task is selected.
Snippet of select external tool
And then once I hit okay, the window closes but I don't see a new task added to the before launch section. It looks the same it did before I clicked on anything. I also don't see any status message at the bottom of IntelliJ
I basically expect the task "Spigot-Server" to show under the Before Launch section. This would basically start up the server and once the server was completely started, it would then launch the debugging configuration.
I think it may be an issue with IntelliJ's UI. I searched IntelliJ's issue tracker But couldn't find any that matched.
I'm wanting to know if there are any work arounds to get this setup in IntelliJ>
I was able to achieve my goal by doing some workarounds that I discovered with the help of CrazyCoder. I found this stackoverflow question that told me about the batch plugin.
I was able to create a Batch run configuration for a wrapper batch file that gets around the limitation CrazyCoder mentioned. He said that even if I did get my setup where my server runs before my debugging is launched, my debugging would never launch. This is because the terminal process needs to exit before the next task starts.
To get around this this is the wrapper batch file I created.
#echo OFF
START "Spigot-Server" /D C:\Users\rocke\Documents\Programming\Minecraft\Spigot\capture-the-carrot\test-server "start.bat"
ping -n 3 127.0.0.1 >nul
It basically uses the "START" command to run the task asynchronously and then waits 2 seconds. This is because all I need to do is wait for the start.bat command to run the very first part so that it establishes remote debugging. And then the wrapper ends after 2 seconds (the 3 represents the number of seconds I want to wait +1). And this allows the Debugging process to start while also ensuring that the server has setup remote debugging!
I'll just share screenshots of my configuration if anyone wants to do the same.
Maven Build Configuration
Spigot Server Start Configuration
Remote Debugging Configuration
The way this is setup is once I run the Remote Debugging Configuration, it has a "Before Launch" Task of Spigot Server. Spigot Server also has a "Before Launch" Task of Maven Build Project.
So my Maven Build Project runs, then my Spigot Server wrapper runs and after 2 seconds terminates, and then my Debugging Configuration runs!
If you have any questions regarding remote debugging with spigot, use this resource: https://www.spigotmc.org/wiki/intellij-debug-your-plugin/

ActiveMQ in-memory broker not starting when tests running in IntelliJ IDEA

So I'm running an integration test/spec using configuration for an ActiveMQ in-memory broker.
SomeSpec.groovy:
#SpringApplicationConfiguration(SomeApplication.class)
#WebIntegrationTest(randomPort = true)
class SomeSpec extends Specification {
application.properties
spring.activemq.in-memory=true
spring.activemq.pooled=false
The in memory broker starts up and runs fine when I do gradle test and
also runs fine when used with gradle bootRun at the command line. However when I run inside IntelliJ without explicitly having it run gradle test the in memory broker does not start and the tests fail.
How can I take and advantage of the nice test/spec running features in IntelliJ but still have it initialize the in memory queue properly? I know with Grails you could run with JUnit or Grails. Is there something similar with Spring/SpringBoot so everything starts up properly.
It's probably because your config files are not refreshed under project/out/production/config/ location.
When you run it from cmd line, it takes the latest application.properties, so every thing is fine.
But Idea takes the already compiled config files, and if they are not rebuilt inside Idea then it still loads the load configuration.

Run IntelliJ code formatter from the command line

I am trying to run IntelliJ code formatter from command line.
I know that inspections can be run from CLI, however, they didn't provide the same for the source formatting (lame!).
Now, I started with CE edition of IntelliJ. Source code is on github.
However, since the code base is not small, I am having problems encapsulating the formatter.
Is there anyone who can point me in right direction and showing which classes to start with?
You can run the formatter from the terminal now. Like the code inspection.
https://confluence.jetbrains.com/display/IDEADEV/Command-Line+Source+Code+Formatter
/bin/format.sh [-h] [-r|-R] [-s|-settings settingsPath] [-m|-mask masks] [path1 [path2]...]
On Windows it's /bin/format.bat
To do this in the new versions of Intellij
You need to make sure the command line launcher is installed via the Jetbrain Toolbox.
Toolbox App Announcement
And run the following command from your shell:
idea format -s /path/to/your/style.xml /path/to/your/folder
I would recommend that you set the shell script location to /usr/local/bin/

Debugging SBT project with Play in IntelliJ IDEA

I have a SBT project
in this project i have a sub play project and other projects
example from my build file :
lazy val subProj1 = Project(id = "sub-proj-1", base = file("sub1"))
.settings(...)...
lazy val subProjPlay = play.Project("play-proj", 1.0 , path = file("web"))
need to debug the play server from IntelliJ IDEA.
To run the project I use sbt run on the command line.
How can I debug the project in IDEA?
I found this to be the easiest solution : (using IntelliJ IDEA )
in IntelliJ :
Go to "edit run configurations"
Create a new remote configuration (port 9999, all other details leave with default values)
Go back to IntelliJ and run the new debug configuration (don't forget to put a break point)
From command line run :
sbt -jvm-debug 9999 run
The easiest solution.
Edit Configurations... -> add SBT Task (not Remote task).
Specify SBT Task: ~run.
Run created SBT Task using - Debug button
Provided you've Play distribution installed locally, use play debug run on the command line and connect to localhost on the port 9999 in IDEA.
From Debugging section in Using the Play console in the official Play 2.2.x documentation:
You can ask Play to start a JPDA debug port when starting the console.
You can then connect using Java debugger. Use the play debug command
to do that
If however you don't have it (and for a reason don't want to install it), add Remote Run configuration in IDEA that will give you a hint for the command line arguments you should be using when launching SBT, e.g.
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
When you launch SBT that may or may not be as simple as launching SBT jar, just use the above to configure JVM to run in debug mode.
IntelliJ IDEA 2016.1.1 && Play Framework 2.5.3
For me, no matter how I set(create new Run/Debug Configuration for Play 2 App or SBT Task, specify the debug port, execute in Run or Debug mode) in the IntelliJ IDEA 2016.1.1 Enterprise Edtion, the IDEA can not open the debug port(default 9999), so the debug is impossible.
After disable the sbt-fork-run-plugin(comment it in /project/paly-fork-run.sbt), it works!!!
I am newer to Play framework,and have found many bugs...Compare to RoR, it's so hard to learn, to run, to use, to debug...
Below is my steps:
disable the sbt-fork-run-plugin(comment it in /project/paly-fork-run.sbt)
execute activator -jvm-debug 9999 "run 11111" (I use port 9999 to debug, port 11111 to run my Play project)
In IDEA, add an new Run/Debug configuration, Choose, set debug port to 9999
debug the new created configutation

groovy (java): exec() do not detach from process (Intellij IDEA vs Maven+TestNG)

I have Groovy Maven2 test project with TestNG and Surefire plugin enabled.
I want to launch external process (*.cmd file which start some *.exe file) in last test method, finish my tests and left process running after tests.
I tried the following codes to do it:
1 attempt
def builder = new ProcessBuilder('cmd','/c <name>.cmd')
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()
2 attempt (with and without start cmd option)
Runtime.getRuntime().exec( "cmd /c start <name>.cmd", null , ( new File( <path_to_working_directory> ) ) )
3 attempt
( new AntBuilder() ).exec(
dir: "<path_to_working_directory>",
executable: "<name>.cmd"
)
Where .cmd is:
set path=<path_to_execFile>;%path%
start <execFileName>.exe
When I launch each of these codes from Intellij IDEA via 'Run' functionality (Alt+Shift+F10) codes execute successfully, process started and run after test finishes.
When I launch each of these codes both from Intellij IDEA Maven task, clean Maven installation (and even Maven task from Jenkins) process started successfully but test remains running. I need to kill it manually. When I kill test process (Maven process) manually my launched external process continue to work as I expect.
This hung test process is my headache for the moment.
I looked through a lot of materials but didn't find any root cause, fix and even workaround for this issue. I see that all my attempts (perhaps, except of AntBuilder()) create deattached processes. I suppose that this can be connected with JVM settings. But I coudnl't find to which one.
Also, I tried
"full command to run my cmd".execute()
but it didn't help me too.
Could you please help me resolve the issue?
Thanks In Advance!
So, I do not see any answers for my issue here. But I have some updates.
I found that I can use PsExec tool instead of direct cmd calling:
def builder = new ProcessBuilder( 'psexec', 'cmd', '/c', '<name>.cmd' )
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()
And this code works fine when I launch it from clean Maven only (not from Jenkins): process started, Maven task completes successfully, the process continues to run.
But during execute this code as part of some Maven2 Jenkins task I faced to issue again: psexec started but Jenkins task is running and my process does not started before I terminate Jenkins task manually.
To avoid this issue I created simple additional Groovy service script I launch in listen mode (and Writing a TCP Server) on target machine manually during initial machine preparation. This script is running on machine always.
I send to this listener name of command file to execute from my test I launch from Jenkins and it executes all cmds successfully: processes start, Jenkins task completes successfully, processes continue to run. I use processbuilder inside this listener.
For name sending I use simple socket (Writing a TCP Client)
Also, I found how to detach child from process tree on win32?. But for me system my way looks more Groovy I think.