Gangnus
Gangnus

Reputation: 24484

How can I output from java code to the Jenkins console?

I have java code with many System.out.println() added. One of them is at the start of the "main" method. When I start the code in NetBeans, I see the console output with all these println.

When I start the class in Jenkins/ant with fork&spawn set to true (as a separate task), it runs, but no println is put to the Jenkins console.

Can I make System.out.println to write to Jenkins console?

I have found an interesting repaired Jenkins issue, they say "a variable 'out' of the object model can be used to write messages to the build console" How?

Edit: In the same project, the applications that are not spawned output their stdout into the Jenkins console OK.

Edit 2. Please, Notice that I want to output to Jenkins console, not to a file.

Upvotes: 5

Views: 7610

Answers (3)

David Georg Reichelt
David Georg Reichelt

Reputation: 1003

If you want to redirect the output from a process to the Jenkins console, e.g. because you call some library code that writes to System.out, you can redirect the output of anything you call by using System.setOut / System.setErr.

@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
     PrintStream outOriginal = System.out;
     PrintStream errOriginal = System.err;
     try {
        System.setOut(listener.getLogger());
        System.setErr(listener.getLogger());
        System.out.println("Testing System.out");
        // Call your code that writes to System.out by default
     } finally {
        System.setOut(outOriginal);
        System.setErr(errOriginal);
     }
}

Upvotes: 0

Mayur Lokare
Mayur Lokare

Reputation: 31

If you overriding the method perform then, use BuildListener instance to get logger instance and write using print method. Like this

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {

    // This is where you 'build' the project.       /
    // This also shows how you can consult the global configuration of the builder
    listener.getLogger().println("Inside the Perform Method");
    return true;
}

Upvotes: 2

bram
bram

Reputation: 1338

TaskListener of Jenkins has a method getLogger() using which you can print in the console output. System.out.println will not be redirected to Console output in Jenkins.

Upvotes: 1

Related Questions