Ant Java Objective: How to Get Console Output and File-Output Recording Records Without Shell Redirect

I use ant to run a Java program.

I do not want to "free" the output after the program ends. Therefore, I use the " output " property to store the output in a file.

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

Unfortunately, I no longer have console output. What would be a good way to get output on the console and in a txt file.

I am looking for an alternative

 ant mytast > myFile.txt 

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

+6
source share
3 answers

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

The recorder is a listener of the current build process, which writes the output to a file.

Several recorders may exist at the same time. Each recorder is associated with a file. The file name is used as a unique identifier for recorders. The first call to the recorder task with an unused filename will create the recorder (using the provided parameters) and add it to the assembly listeners. All subsequent calls the task of the recorder using this file name will change this state of the registrars (record or not) or other properties (for example, logging level).

It seems to fit your needs.

  <compile > <record name="log.txt" action="start"/> <javac ... <record name="log.txt" action="stop"/> <compile/> 
+7
source

If a program writes to a single file descriptor (for example, standard output), you can redirect it to only one place (so either the console or the file, not both).

To achieve multiple redirection, you have two options:

  • duplicate this file descriptor using external means (for example, the tee command does just that - even for Windows there are implementations: see here and here .
  • write the same information to two different exits from the java program (for example, both standard and standard errors), then you can redirect it using traditional means, and the other to the console

I would not recommend the second approach, because it makes your code dirty, and you often do not have full control over what is written to say a standard error (think about calling third-party functions).

+1
source

you can use the write task. This will fit your needs, I think.

https://ant.apache.org/manual/Tasks/recorder.html

0
source

Source: https://habr.com/ru/post/913019/


All Articles