简体   繁体   中英

Command line version of IntelliJ IDEA run configuration

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

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:

在此处输入图像描述

Notice it will expand and show you the full command: 在此处输入图像描述

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: 在此处输入图像描述

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

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

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 … .

eg 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

Continuing my comment above...

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

在此处输入图像描述

Instead, they can be found in the java command that you explain how to reveal. Eg, 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM