M. Justin
M. Justin

Reputation: 21123

Can I exclude build output when running a Gradle project from IntelliJ?

I have a Gradle project being run from the latest version of IntelliJ IDEA (2021.1.1 Ultimate Edition). The project has a Java class with a main method that outputs its results to the console.

When I run this main method from IntelliJ IDEA, outputs the results of the program in the "Run" tab. However, it also outputs the Gradle build output as well. This is not desirable to me, since it makes the program output harder to visually distinguish, plus I have to carefully select just the program output, and can't just copy/paste the full output (click, A, C, click, V).

build.gradle

plugins {
    id 'java'
}

Main.java

package com.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Program output line 1");
        System.out.println("Program output line 2");
        System.out.println("Program output line 3");
    }
}

output

1:20:08 PM: Executing task 'Main.main()'...

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :Main.main()
Program output line 1
Program output line 2
Program output line 3

BUILD SUCCESSFUL in 508ms
2 actionable tasks: 2 executed
1:20:09 PM: Task execution finished 'Main.main()'.

Aside

Similar behavior occurs if I switch the project settings to build and run using IntelliJ IDEA rather than Gradle, in that it outputs the Java command and a "process finished" message. Even if I wanted to switch to running in this manner (which I don't for this project), I still run into a variant of this issue.

/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=61588:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/Example/GradleMainOutput/out/production/classes com.example.Main
Program output line 1
Program output line 2
Program output line 3

Process finished with exit code 0

That all being said, is there a way to output only the program output in an IntelliJ IDEA Gradle project, and not the build output?

Upvotes: 5

Views: 1879

Answers (4)

Kai Sellgren
Kai Sellgren

Reputation: 30212

Here's how to do it in IntelliJ without hacks. Open up your run configurations dialog:

enter image description here

Choose "Edit configuration templates" from the bottom left:

enter image description here

Modify the default Gradle run configuration with flags --console-plain --quiet:

enter image description here

Important! After you press OK, you must delete any existing run configs you have, because this change does not alter existing run configs:

enter image description here

Another important note! Do not press "Re-run" if you have an existing run/debug/test:

enter image description here

If you press that Re-run button, it will run with the old configuration. So, make sure you start a new run/debug/test:

enter image description here

And you're done!

Upvotes: 1

Victor Spivak
Victor Spivak

Reputation: 1

The --quiet flag helps, but it is not enough. For me the following flags combination works:

gradle --console-plain --quiet ...

Upvotes: 0

LionH
LionH

Reputation: 324

Old question, but for anybody still interested to just get the output of the script, you can use the --quiet flag (https://docs.gradle.org/current/userguide/logging.html)

So having this in the build.gradle :

task(run, dependsOn: 'classes', type: JavaExec) {
    main = 'com.multimodule.module.MainClass'
    classpath = sourceSets.main.runtimeClasspath
}

and execturing the task:

./gradlew multimodule:module:run --quiet

you'll get just the output (.e.g any System.out.println you added on the main)

Upvotes: 3

CrazyCoder
CrazyCoder

Reputation: 401965

There is no way to hide Gradle build output when running in this mode.

The only way is to disable Gradle run delegation so that app is started directly by IntelliJ IDEA.

no delegation

Hiding the command line and the exit code is also not possible.

You can only fold it.

Upvotes: 3

Related Questions