10

I need to see the Maven logs in the file and also in the console.

If I try using mvn clean install > log.txt. Logs are storing in the file only, console output is not displaying.

Can anyone please suggest how to display the logs in the console and also to store in the file in the Maven Build?

Thanks in advance.

Regards, Karthik

Auscyber
  • 72
  • 1
  • 2
  • 13
user2400564
  • 4,619
  • 7
  • 24
  • 27
  • 1
    Note that mvn has a `-l out.txt` option you could use instead of `> log.txt` – Daniel Kaplan May 31 '13 at 19:07
  • Hi....thanks for your reply. Can u please breif it. Is it possilble to use mvn clean install -l out.txt. Will it work in windows? Is it the valid command? – user2400564 Jun 04 '13 at 05:53
  • possible duplicate of [How to log maven output to a file and without hiding console?](http://stackoverflow.com/questions/4744874/how-to-log-maven-output-to-a-file-and-without-hiding-console) – Nathan Sep 23 '15 at 22:56

3 Answers3

17

Taken you are running on a UNIX/Linux environment the following should solve your problem:

mvn clean install | tee log.txt

Windows users can use a Cygwin shell to use this command as well. There is also Wintee available for this purpose.

You can find usage instructions on the tee manpage:

NAME
   tee - read from standard input and write to standard output and files

SYNOPSIS
   tee [OPTION]... [FILE]...

DESCRIPTION
   Copy standard input to each FILE, and also to standard output.

   -a, --append
          append to the given FILEs, do not overwrite

   -i, --ignore-interrupts
          ignore interrupt signals

   --help display this help and exit

   --version
          output version information and exit

   If a FILE is -, copy again to standard output.
Torsten
  • 6,184
  • 1
  • 34
  • 31
  • Hi, Thanks for your reploy. Actually i am using windows maching. I think tee command will not work in windows. Can u able to suggest a solution for windows? – user2400564 May 31 '13 at 09:32
  • @Torsten can you please explain little bit about `tee` argument ? any link / resources ? – Aman Gupta Apr 07 '14 at 06:17
  • Actually, the answer also works for Windows using PowerShell. I try it using Windows 2012 – Harun Nov 24 '15 at 03:29
  • Piping output of the "mvn" command to "tee" will accomplish what OP is asking, but be aware, if you need to evaluate the return code of the "mvn" command, "$?" will hold the return code from the "tee" command, NOT the "mvn" command, which may impact your error checking. – NoobSkywalker Jan 09 '20 at 17:05
1

mvn clean install -l

You can use -l OR --log-file option to write the logs in file.

Mohan Ahire
  • 214
  • 2
  • 9
0

This is what I do. It has the advantage being able to highlight search terms and scroll back while it is still working. It is two lines, the maven runs in the background with &, the less command +F follows the tail of the log as it is written and displays it on the console:

mvn clean install -l log01.txt & 
less +F log01.txt

or with highlighting errors, case insensitively:

less -i +/error +F log01.txt

Ctrl-C to stop following, Shift-N to find the error, q to quit less.

If I really want my files to be organized, I add a time-stamp in the name, then use tab to do bash file name completion:

mvn clean install  -l 02log$(date +%Y%m%d%H%M).txt &
less +F 02<tab>

I increment the file's initial number each time to make the name completion simple. If I forget, it doesn't matter.

David Lotts
  • 550
  • 5
  • 10