eclipse 使用 pom 将 Maven 输出发送到控制台和日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39799178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:18:15  来源:igfitidea点击:

Send Maven output to console and log file using pom

eclipsemavenmaven-3pom.xmleclipse-luna

提问by Zack

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

问题:在 Eclipse 中运行 Maven 时,如何将控制台输出发送到文件?

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

我想使用 pom 设置或 maven 插件来实现这一点。我希望修改运行配置或maven的系统设置。

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

作为参考,我使用的是 Windows 7、Eclipse Luna、Java 6、Maven 3。

回答by A_Di-Matteo

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

根据官方命令行选项,您可以使用-l,--log-file <arg>它提供:

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 outputto the output.logfile.

不会向控制台打印任何内容并自动将整个构建输出重定向到output.log文件。

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.1and have a .mvnfolder where the concerned pom.xmlfile is located and a maven.configfile in it simply providing the following line:

如果您不想每次都键入它(或者您实际上不想使用命令行)并且希望将其作为默认选项(尽管我认为这种情况很少见),您可以使用新的命令行选项行为从版本3.3.1 开始,并且有一个.mvn文件夹,该pom.xml文件夹位于相关文件中,其中一个maven.config文件只提供以下行:

-l output.log

That is, the .mvn/maven.configfile replaces MAVEN_OPTIONSjust for its project, locallywhere it has been created, with the options it provides, not impacting other builds as per Maven settings of MAVEN_OPTIONS.

也就是说,该.mvn/maven.config文件MAVEN_OPTIONS仅替换其在本地创建它的项目,使用它提供的选项,而不影响根据 .maven 设置的其他构建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 phasesis validate, which:

这是一个 IDE 不可知的解决方案(它是 Maven 的一个新的内置功能)并且是项目本地的,但仍然没有通过简单的 POM 编辑提供,这是无法实现的,因为Maven 默认生命周期阶段的第一阶段validate,其中:

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.xmlfile, hence too late to redirect build output at that stage based on some POM properties/plugin.

也就是说,在构建期间,因此当构建已经开始(并生成输出)时,它会验证pom.xml文件,因此在该阶段基于某些 POM 属性/插件重定向构建输出为时已晚。

回答by RITZ XAVI

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

转到 run as 并选择Run Configuration -> Commons -> Select a file.
这应该将您的输出重定向到您指定的文件。

回答by agnul

According to thisyou 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

根据这个你可以尝试编辑${MAVEN_HOME}/conf/logging/simplelogger.properties。我快速尝试了一下,maven 的输出被重定向,但任何其他写入stdout(例如测试)仍然写入控制台

回答by Bernhard Stadler

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

如何创建 M2E 的分支并修改它以从 pom.xml 读取启动配置的输出文件

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

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

回答by leuson

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.

一个可能的解决方案是在mvn file. 例如,在目录中/usr/bin,添加所需的输出,通知将在行尾保存日志的路径exec "$JAVACMD" \| 三通 /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.

但是,它仅在终端线调用 Maven 时有效;当被 IDE 调用时,比如 eclipse,这个解决方案不起作用。