且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用pom将Maven输出发送到控制台并记录文件

更新时间:2023-11-27 14:34:10

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

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

所有构建输出将保存到的日志文件.

Log file where all build output will go.

这样,运行:

mvn clean install -l output.log

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

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

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

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

也就是说,.mvn/maven.config文件仅使用创建的项目本地替换MAVEN_OPTIONS,并使用其提供的选项替换MAVEN_OPTIONS,而不会影响根据MAVEN_OPTIONS.

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.

这是一个与IDE无关的解决方案(它是Maven的一项新内置功能),在项目本地使用,但仍无法通过简单的POM编辑来提供,这是自

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

也就是说,在构建期间,因此,当构建已经开始(并生成输出)时,它会验证pom.xml文件,因此为时已晚,无法根据以下情况重定向构建输出:一些POM属性/插件.

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.