3

I have a working IntelliJ IDEA run configuration. It uses Spring Boot.

I'd like to execute the same run from the MacOS command line. How can I get IntelliJ IDEA to show the command (or commands) that I need execute the run configuration.

This is the gradle build.gradle file:

plugins {
    id 'org.springframework.boot' version '2.6.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'org.mountsinai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = "15"

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    runtimeOnly 'com.microsoft.sqlserver:mssql-jdbc'
    implementation group: 'org.springframework', name: 'spring-aspects', version: '5.3.15'
    implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '3.0.0'
    implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.7.0'
    implementation group: 'com.github.pcj', name: 'google-options', version: '1.0.0'
    implementation 'com.google.code.gson:gson:2.9.0'
}

tasks.named('test') {
    useJUnitPlatform()
    minHeapSize = "1024m" // initial heap size
    maxHeapSize = "2048m" // maximum heap size
}
targetCompatibility = JavaVersion.VERSION_15

And this is the configuration element in the ./.idea/workspace.xml corresponding to the run I'd like to automate on the command line:

    <configuration name="IrwMetadataIntegrationApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
      <module name="org.mountsinai.IRWMetadataIntegration.main" />
      <option name="SPRING_BOOT_MAIN_CLASS" value="org.mountsinai.IRWMetadataIntegration.IrwMetadataIntegrationApplication" />
      <option name="PROGRAM_PARAMETERS" value="--algorithm=batch --numOfStudiesToRetrieve=600" />
      <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
      <option name="ALTERNATIVE_JRE_PATH" value="15" />
      <method v="2">
        <option name="Make" enabled="true" />
      </method>
    </configuration>

My original question can now be asked more concretely How can one convert an IDEA configuration and workspace.xml file into a command (or script) that can be executed outside IntelliJ IDEA?

Using

IntelliJ IDEA 2021.3.2 (Ultimate Edition) Build #IU-213.6777.52, built on January 27, 2022

Thanks, Arthur

Arthur
  • 525
  • 7
  • 18
  • No IDE builds/executes source code on its own. It uses a combination of build tools like `maven` `gradle` etc., in combination with `compiler` `interpreter` etc., You didn't specify these in your question. Your answer lies in those. – Rohit Babu Sep 29 '22 at 16:52
  • you might be using mvn or mvnw & in that case executing `mvnw clean spring-boot:run` via mac terminal against your project will run your spring boot code – Ashish Patil Sep 29 '22 at 17:04
  • Fair point @RohitBabu. I've added `gradle` information to my question. – Arthur Sep 29 '22 at 20:44
  • Your run configuration is more of a GUI version of `gradle` command. As far as I know, you have to run the equivalent `gradle` command to achieve the job of run configuration. You may inspect your run configuration file in any editor (like notepad) to know more. – Rohit Babu Sep 29 '22 at 20:49
  • Thank you for that hint @RohitBabu. I found the run configuration file in `./.idea/workspace.xml`. It's a fairly large (337 lines) and complex file. I'll put the configuration for the run I care about in the question above. – Arthur Sep 29 '22 at 23:11

4 Answers4

10

At the top of your console Notice there's a command something something... java, normally it collapses into such short form, but if you click on that:

enter image description here

Notice it will expand and show you the full command: enter image description here

In this case, for example, ultimately, the command is something like this:

/Users/hoaphan/.sdkman/candidates/java/17.0.4-amzn/bin/java -XX:TieredStopAtLevel=1 
-Dspring.output.ansi.enabled=always 
-Dcom.sun.management.jmxremote 
-Dspring.jmx.enabled=true 
-Dspring.liveBeansView.mbeanDomain 
-Dspring.application.admin.enabled=true 
...blahblahblahblahblah...
com.example.bootgradlej17.Bootgradlej17Application

So you can mimic intelliJ by running such long command. But I would suggest if this is springboot, and build by gradle, what you actually want to do is at project root, in the dir where your build.gradle is do:

./gradlew bootRun

Now if you want to mimic the JVM option IntelliJ did, you can just capture such info from an IntelliJ run using tool like VisualVM: enter image description here

Then you can configure your bootRun to start with such config for JVM like:

bootRun {
   jvmArgs = "-Dspring.application.admin.enabled=true", "-Dnannana=nenenene" (etc)....
}
HoaPhan
  • 1,714
  • 1
  • 13
  • 35
  • Thank you, @HoaPhan, your response is quite helpful. Given their ridiculous size limit, I need to write multiple comments. 1. The gradle wrapper `gradlew` is an excellent idea. – Arthur Oct 04 '22 at 14:55
  • visualvm is just an example way of capturing what your final JVM state looks like. You should be able to use utility that comes with your JVM like `$JAVA_HOME/bin/jconsole` to do the same thing or ps command looking for java process, should jvisualvm is not reliable for your env for some reason. bootRun { ... } is the config for `./gradlew bootRun` you put into build.gradle and it persists and versioning/tracked. For example uses, looks at these on GitHub: https://github.com/search?l=Gradle&o=desc&q=bootRun&s=indexed&type=Code – HoaPhan Oct 04 '22 at 23:35
  • Thanks @HoaPhan. Please see another comment in an answer below. – Arthur Oct 10 '22 at 15:22
1

Execute your program through the run configuration and check the first line of the output. It should contain the call that is executed by the IDE, often something like java -jar ….

e.g. when running a Scratch file, I get the following in the output window as first line:

/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -javaagent:/home/user/bin/idea-IU-222.3345.47/lib/idea_rt.jar=37117:/home/user/bin/idea-IU-222.3345.47/bin -Dfile.encoding=UTF-8 -classpath /home/user/.cache/JetBrains/IntelliJIdea2022.3/compile-server/backend_dd2372d5/scratches/out:... Scratch
knittl
  • 246,190
  • 53
  • 318
  • 364
1

Continuing my comment above ...

  1. On my computer (running macOS Big Sur 11.6) VisualVM cannot find the JVM arguments:

enter image description here

Instead, they can be found in the java command that you explain how to reveal. E.g., mine are:

-Dspring.output.ansi.enabled=always
-Dcom.sun.management.jmxremote
-Dspring.jmx.enabled=true
-Dspring.liveBeansView.mbeanDomain
-Dspring.application.admin.enabled=true
...

Please explain your

bootRun {
   jvmArgs = "-Dspring.application.admin.enabled=true", "-Dnannana=nenenene" (etc)....
}

example. Where and how should this text be used?

  1. One also needs to provide any command-line arguments that are in the IDEA configuration. These can be passed via the gradlew --args option, like this:

    --args='--algorithm=batch --numOfStudiesToRetrieve=600'

Overall, however, this approach is more ad hoc than I'd like. It requires that one hack a) IDEA's java command to obtain the JVM arguments, b) the command-line arguments in via gradlew's --args. But I assume that the silence of IntelliJ IDEA's developers indicates that they do not support this important functionality.

Arthur
  • 525
  • 7
  • 18
0

Another comment in an answer because the formatting limitations of comments are so limiting:

Thanks, @HoaPhan. However, when I insert a bootRun section like this in build.gradle,

bootRun {
  jvmArgs = "-Dspring.output.ansi.enabled=always"
}

IntelliJ IDEA raises

Could not compile build file '<path>/build.gradle'.
> startup failed:
  build file '<path>/build.gradle': 1: Unexpected input: '{' @ line 1, column 9.
     bootRun {
             ^

This appears to occur wherever the section is placed in the file.

However, this does work:

bootRun {
    jvmArgs = [ "-Dspring.output.ansi.enabled=always",
                "-Dcom.sun.management.jmxremote",
                "-Dspring.jmx.enabled=true",
                "-Dspring.liveBeansView.mbeanDomain",
                "-Dspring.application.admin.enabled=true",
                "-Dfile.encoding=UTF-8"
    ]
}

It's unclear why the first bootRun { appears to generate a syntax error and this does not.

Arthur
  • 525
  • 7
  • 18