Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant Java task : how to get output to console and a file-always record build output without shell redirection

Tags:

java

ant

I am using ant to start a Java program.

I do not want to "loose" the output, after the program terminates. So I use the property 'output' to store the output in a file.

<java classname="..." fork="true" output="....txt">

Unfortunately I do not have any console output anymore. What would be a nice way to have the output on the console and in a txt file.

I am looking for an alternative to

ant mytast > myFile.txt

because I do not want, that the "user" has to use shell redirection "> ..". . If he/she does not choose to redirect, the output is lost.

like image 743
user1328819 Avatar asked Apr 12 '12 10:04

user1328819


People also ask

What does * stands for in * Test Java in ant?

**\*.sql means "in the given directory and inside all of its subdirectories, all the files that end with .sql"

What is Classpathref in ant?

Ant classpath: discussion dir defined before this code is run, and that variable points to your lib directory, which contains all of the jar files needed by your application. This code also creates a variable named class. path which can be referenced later in your Ant script using the variable syntax ${class. path} .


2 Answers

Ant has a way to record output. http://ant.apache.org/manual/Tasks/recorder.html.

A recorder is a listener to the current build process that records the output to a file.

Several recorders can exist at the same time. Each recorder is associated with a file. The filename is used as a unique identifier for the recorders. The first call to the recorder task with an unused filename will create a recorder (using the parameters provided) and add it to the listeners of the build. All subsequent calls to the recorder task using this filename will modify that recorders state (recording or not) or other properties (like logging level).

It looks like meets your need.

    <compile >
        <record name="log.txt" action="start"/>
        <javac ...
        <record name="log.txt" action="stop"/>
    <compile/>
like image 146
Jayan Avatar answered Oct 22 '22 03:10

Jayan


If the program writes to one file descriptor (e.g. standard out), you can redirect it to only one place (so either console, or file, not both).

To achieve multiple redirection, you have two options:

  • duplicate that file descriptor using an external means (e.g. the tee command does exactly that -- even for Windows there are implementations: see here and here.
  • write the same information to two different outputs from within the java program (e.g. both to standard out and standard error), then you can redirect one using traditional means, the other goes to console

I would not recommend the second approach as it makes your code messy and you often do not have full control over what is written to say standard error (think of calls to 3rd party functions).

like image 1
Attila Avatar answered Oct 22 '22 04:10

Attila