16

Question: When running Maven in Eclipse, how do I send the console output to file?

I would like to achieve this using a pom setting or a maven plugin. I do not want to modify the run configurations or the maven system settings.

For reference, I am using Windows 7, Eclipse Luna, Java 6, Maven 3.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Zack
  • 3,819
  • 3
  • 27
  • 48
  • 2
    You can't do that, the answers you have are the only way to go. How would Maven be able to write a file, before it has read the POM? Consider multi-module project also, where that configuration would only be in a module. – Tunaki Oct 05 '16 at 07:43
  • 1
    Try using AOP. Was successful in redirecting logs to file by applying point cut on the default org.apache.maven.plugin.logging.Log. But too much effort for nothing. Give it a shot. – user1615664 Oct 05 '16 at 20:42

5 Answers5

27

As per official command line options you could use -l,--log-file <arg> which provide the:

Log file where all build output will go.

As such, running:

mvn clean install -l output.log

Would not print anything to the console and automatically redirect the whole build output to the output.log file.

If you don't want to type it every time (or you actually don't want to use the command line) and you want it as default option (although rare case I would suppose), you could use new command line options behavior available since version 3.3.1 and have a .mvn folder where the concerned pom.xml file is located and a maven.config file in it simply providing the following line:

-l output.log

That is, the .mvn/maven.config file replaces MAVEN_OPTIONS just for its project, locally where it has been created, with the options it provides, not impacting other builds as per Maven settings of MAVEN_OPTIONS.

This is an IDE agnostic solution (it's a new built-in feature of Maven) and local to a project, but still not provided via simple POM editing, which cannot be achieved since the first phase of Maven default life cycle phases is validate, which:

validate the project is correct and all necessary information is available

That is, during the build, hence when the build has already started (and generated output), it validates the pom.xml file, hence too late to redirect build output at that stage based on some POM properties/plugin.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • Even though logging in the Eclipse console window is lost, this is the most reasonable answer. – Zack Oct 11 '16 at 17:47
6

Go to run as and choose Run Configuration -> Commons -> Select a file.
This should redirect your output to the file you specified.

RITZ XAVI
  • 3,633
  • 1
  • 25
  • 35
  • This is a good answer, but I am looking for some way to achieve this by modifying the pom only. I have updated my question. – Zack Sep 30 '16 at 20:18
4

According to this you can try editing the ${MAVEN_HOME}/conf/logging/simplelogger.properties. I gave it a quick try and maven's output is redirected, but anything else writing to stdout (tests, for instance) still writes on the console

agnul
  • 12,608
  • 14
  • 63
  • 85
  • 1
    This is correct, but I am looking for a solution that does not modify the maven settings. – Zack Oct 04 '16 at 16:13
  • 1
    Yep, I hoped you could override those settings in the `` section of the project file but it doesn't seem to work... – agnul Oct 04 '16 at 20:23
2

What about creating a fork of M2E and modifying it to read the output file for the launch config from pom.xml

https://github.com/eclipse/m2e-core.git

Bernhard Stadler
  • 1,725
  • 14
  • 24
0

A possible solution is setting the output format in the mvn file. For example, in the directory /usr/bin, add the desired output informing the path the log will be saved at the end of exec "$JAVACMD" \ line: | tee /home/maven-log.log.

However, it only works when the maven is called by terminal line; when called by IDEs, like eclipse, this solutions does not work.

leuson
  • 11
  • 4