Nick Veys
Nick Veys

Reputation: 23939

Capturing Ant <javadoc> task output

Is it possible to capture the warnings that Javadoc prints when run via the javadoc Ant task? I don't see an output attribute such as the one in the task. There seem to be warnings that Checkstyle just isn't catching and it'd be nice to snag that output in a file.

Seems strange this wouldn't be capturable, hopefully I'm missing something obvious.

~*~*~*~ EDIT (answer below) ~*~*~*~

It would appear the Ant <record> task is exactly what I was looking for. See the Ant docs.

<target name="generate.docs">
    <record name="javadoc.log" action="start"/>
    <javadoc ... />
    <record name="javadoc.log" action="stop"/>
<target/>

Upvotes: 4

Views: 2154

Answers (4)

haui
haui

Reputation: 617

Just in case somebody else is still interested - the Ant javadoc task can be very easily be retrofitted with an option outputFile to store the generated messages e.g. to a file:

public class MyJavaDoc extends org.apache.tools.ant.taskdefs.Javadoc{

    private File _outFile;

    private Writer _writer;

    /**
     * The file to write error output to.
     */
    public void setOutputFile(File outFile) {
        _outFile = outFile;
    }

    @Override
    public void execute() throws BuildException {
        if (_outFile == null) {
            throw new BuildException("Missing 'outputFile' parameter.");
        }
        try (OutputStream out = new FileOutputStream(_outFile)) {
            _writer = new OutputStreamWriter(out);
            try {
                super.execute();
            } finally {
                _writer.close();
            }
        } catch (IOException ex) {
            throw new BuildException("Cannot write error output: " + ex.getMessage(), ex);
        }
    }

    @Override
    public void log(String msg, int msgLevel) {
        if (msgLevel <= Project.MSG_WARN) {
            try {
                _writer.write(msg);
                _writer.write("\n");
            } catch (IOException ex) {
                throw new IOError(ex);
            }
        }
        super.log(msg, msgLevel);
    }

}

Upvotes: 1

Yishai
Yishai

Reputation: 91881

You can execute javadoc as a Java class with the com.sun.tools.javadoc.Main class, calling the execute method (it is in the tools.jar included in the JDK), so you could wrap a java class that you call from the Ant java task, which allows redirection of output. you will have to reconstruct the javadoc command line arguments yourself, instead of having the niceties of the Ant task, but it should work.

Upvotes: 1

Art Doler
Art Doler

Reputation: 720

It looks like this is possible using the <exec> tag (reference here)... it would probably be a royal pain, but it might be possible to exec the javadoc executable and reconstruct the command arguments necessary to generate the javadoc. As a great big honking strike against, though, it appears that only uses your natural shell redirect, so building on both Win32 and Linux would require some special-case mojo.

It may be worth it to write your own task to do the job; either the <redirect> tag as Roboprog has mentioned above, or extending the Javadoc task...

Upvotes: 1

Roboprog
Roboprog

Reputation: 3144

I assume the previous redirection answer was down voted due to unstated requirement to do this every time via the build.xml script commands.

Yep. The feature does not seem to be there in the task. A better question might be: Is there a task / tag in Ant that will redirect ALL output from ANY nested tags? Such a task/tag would save System.out and/or System.err, set them to create or append to a file, then restore them at the end of the block.

Something like:

<redirect file='foo.txt' append='true'>
  <anytag you='want' />
</redirect>

Upvotes: 1

Related Questions