如何更改 Maven 日志记录级别以仅显示警告和错误?
我想阻止 Maven 显示信息消息,我只想看到警告和错误(如果有)。
我怎样才能实现这一点,最好是通过更改调用 Maven 的命令行?
I want to prevent maven from displaying INFO messages, I want to see only WARNINGS and ERRORS (if any).
How can I achieve this, preferably by changing the command line that calls maven?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我在使用 2.20.1 版本的 maven sunfire 插件 时注意到,所有警告都会写入转储流文件中。例如 /myproject/target/surefire-reports/2017-11-11T23-02-19_850.dumpstream
I have noticed when using the 2.20.1 version of the maven sunfire plugin, all warnings are written down to a dumpstream file. e.g.
/myproject/target/surefire-reports/2017-11-11T23-02-19_850.dumpstream
回答你的问题
我做了一个小调查,因为我也对解决方案感兴趣。
Maven 命令行详细选项
根据 http://books.sonatype.com/mvnref-book/reference/running-sect-options.html#running-sect-verbose-option
Maven 日志记录配置文件
目前,maven 3.1.x 使用 SLF4J 登录到 System.out 。
您可以在文件中修改日志记录设置:
根据页面:http://maven.apache。 org/maven-logging.html
命令行设置
我认为您应该能够通过命令行参数设置简单记录器的默认日志级别,如下所示:
但是我无法让它工作。我想唯一的问题是,maven 从类路径上的配置文件中获取默认级别。我还通过 System.properties 尝试了其他一些设置,但全部都不成功。
附录
您可以在 github 上找到 slf4j 的源代码: slf4j github
simplelogger 的源代码这里:slf4j/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SimpleLog.java
plexus 加载器加载
simplelogger.properties< /代码>。
Answering your question
I made a small investigation because I am also interested in the solution.
Maven command line verbosity options
According to http://books.sonatype.com/mvnref-book/reference/running-sect-options.html#running-sect-verbose-option
Maven logging config file
Currently maven 3.1.x uses SLF4J to log to the System.out .
You can modify the logging settings at the file:
According to the page : http://maven.apache.org/maven-logging.html
Command line setup
I think you should be able to setup the default Log level of the simple logger via a command line parameter, like this:
But I could not get it to work. I guess the only problem with this is, maven picks up the default level from the config file on the classpath. I also tried a couple of other settings via System.properties, but all of them were unsuccessful.
Appendix
You can find the source of slf4j on github here : slf4j github
The source of the simplelogger here : slf4j/jcl-over-slf4j/src/main/java/org/apache/commons/logging/impl/SimpleLog.java
The plexus loader loads the
simplelogger.properties
.Linux:
或
Windows:
Linux:
or
Windows:
例如,您可以使用 MAVEN_OPTS 来实现此目的
MAVEN_OPTS=-Dorg.slf4j.simpleLogger.defaultLogLevel=warn mvn clean
而不是直接将系统属性放在命令行上。 (至少对于 maven 3.3.1。)
如果您希望在所有 maven 调用中更改登录日志记录,请考虑使用
~/.mavenrc
来设置MAVEN_OPTS
。You can achieve this with MAVEN_OPTS, for example
MAVEN_OPTS=-Dorg.slf4j.simpleLogger.defaultLogLevel=warn mvn clean
Rather than putting the system property directly on the command line. (At least for maven 3.3.1.)
Consider using
~/.mavenrc
for settingMAVEN_OPTS
if you would like logging changed for your login across all maven invocations.如果您使用 Logback,只需将此
logback-test.xml
文件放入src/test/resources
目录中:If you are using Logback, just put this
logback-test.xml
file intosrc/test/resources
directory:您可以通过在命令行本身中使用以下内容来实现此目的
例如:
you can achieve this by using below in the commandline itself
e.g :
最简单的方法是升级到 Maven 3.3.1 或更高版本以利用
${maven.projectBasedir}/.mvn/jvm.config
支持。然后,您可以使用 Maven 的 SL4FJ 的 SimpleLogger 支持中的任何选项来配置所有记录器或特定记录器。例如,以下是如何将所有警告设置为
warn
级别,但配置为在error
处记录的 PMD 除外:请参阅 此处 了解有关使用 Maven 进行日志记录的更多详细信息。
The simplest way is to upgrade to Maven 3.3.1 or higher to take advantage of the
${maven.projectBasedir}/.mvn/jvm.config
support.Then you can use any options from Maven's SL4FJ's SimpleLogger support to configure all loggers or particular loggers. For example, here is a how to make all warning at
warn
level, except for a the PMD which is configured to log aterror
:See here for more details on logging with Maven.
在 Debian 中,这对我来说就像一个魅力,可以在运行时更改 Maven 3.6 的日志级别,而无需更改
simplelogger.properties
文件:In Debian this worked for me like a charm to change the log level for Maven 3.6 at runtime without having to change the
simplelogger.properties
file:不幸的是,即使使用 Maven 3,唯一的方法就是修补源代码。
这是如何执行此操作的简短说明。
克隆或分叉 Maven 3 存储库:“git clone https://github.com/apache/maven- 3.git”
编辑 org.apache.maven.cli.MavenCli#logging,并更改
为
In current snapshot version it's at line 270
然后只需运行“mvn install”,您的新 maven 发行版将位于“apache -maven\target\" 文件夹
请参阅此差异以获取参考:https://github。 com/ushkinaz/maven-3/commit/cc079aa75ca8c82658c7ff53f18c6caaa32d2131
Unfortunately, even with maven 3 the only way to do that is to patch source code.
Here is short instruction how to do that.
Clone or fork Maven 3 repo: "git clone https://github.com/apache/maven-3.git"
Edit org.apache.maven.cli.MavenCli#logging, and change
to
In current snapshot version it's at line 270
Then just run "mvn install", your new maven distro will be located in "apache-maven\target\" folder
See this diff for the reference: https://github.com/ushkinaz/maven-3/commit/cc079aa75ca8c82658c7ff53f18c6caaa32d2131
将
simplelogging.properties
文件中的info
更改为error
将有助于实现您的要求。只需将下面一行的值更改
为
Changing the
info
toerror
insimplelogging.properties
file will help in achieving your requirement.Just change the value of the below line
to
转到
${MAVEN_HOME}/conf/logging/
中的simplelogger.properties
并设置以下属性:并注意:
warn
,而不是警告
Go to
simplelogger.properties
in${MAVEN_HOME}/conf/logging/
and set the following properties:And beware:
warn
, notwarning