1. Overview
1.概述
Sometimes, we might want to print some extra information during Maven’s execution. However, there’s no built-in way to output values to the console in the Maven build lifecycles.
有时,我们可能想在Maven的执行过程中打印一些额外的信息。然而,在Maven的构建生命周期中,并没有内置的方法来向控制台输出数值。
In this tutorial, we’ll explore plugins that enable printing messages during Maven execution. We’ll discuss three different plugins, each of which can be bound to a specific Maven phase of our choosing.
在本教程中,我们将探讨能在Maven执行过程中打印信息的插件。我们将讨论三个不同的插件,每个插件都可以与我们选择的特定Maven阶段绑定。
2. AntRun Plugin
2.AntRun插件
First, we’ll discuss the AntRun plugin. It provides the ability to run Ant tasks from within Maven. To make use of the plugin in our project, we need to add the maven-antrun-plugin to our pom.xml:
首先,我们将讨论AntRun插件。它提供了从Maven中运行Ant任务的能力。为了在我们的项目中使用该插件,我们需要将maven-antrun-plugin加入我们的pom.xml中。
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
Let’s define the goal and phase in the execution tag. Moreover, we’ll add the configuration tag that holds the target with echo messages:
让我们在execution标签中定义目标和阶段。此外,我们将添加configuration标签,该标签持有target与echo信息。
<executions>
<execution>
<id>antrun-plugin</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="Hello, world"/>
<echo message="Embed a line break: ${line.separator}"/>
<echo message="Build dir: ${project.build.directory}" level="info"/>
<echo file="${basedir}/logs/log-ant-run.txt" append="true" message="Save to file!"/>
</target>
</configuration>
</execution>
</executions>
We can print regular strings as well as property values. The echo tags send messages to the current loggers and listeners, which correspond to System.out unless overridden. We can also specify a level, which tells the plugin at what logging level it should filter the message.
我们可以打印常规字符串以及属性值。echo标签向当前的记录器和监听器发送消息,除非被覆盖,否则它们对应于System.out。我们还可以指定一个级别,它告诉插件应该在哪个日志级别过滤message。
The task can also echo to a file. We can either append to a file or overwrite it by setting the append attribute to true or false, respectively. If we choose to log into a file, we should omit the logging level. Only messages marked with file tags will be logged to the file.
该任务还可以回声到一个文件。我们可以通过将append属性分别设置为true或false,来追加到一个文件或覆盖它。如果我们选择记录到一个文件中,我们应该省略记录级别。只有标有file标签的信息才会被记录到文件中。
3. Echo Maven Plugin
3.echo maven插件
If we don’t want to use a plugin based on Ant, we can instead add the echo-maven-plugin dependency to our pom.xml:
如果我们不想使用基于Ant的插件,我们可以在pom.xml中添加echo-maven-plugin依赖。
<plugin>
<groupId>com.github.ekryd.echo-maven-plugin</groupId>
<artifactId>echo-maven-plugin</artifactId>
<version>1.3.2</version>
</plugin>
Like we saw in the previous plugin example, we’ll declare the goal and phase in the execution tag. Next, we’ll fill the configuration tag:
就像我们在前面的插件例子中看到的那样,我们将在execution标签中声明目标和阶段。接下来,我们将填充configuration标签。
<executions>
<execution>
<id>echo-maven-plugin-1</id>
<phase>package</phase>
<goals>
<goal>echo</goal>
</goals>
<configuration>
<message>
Hello, world
Embed a line break: ${line.separator}
ArtifactId is ${project.artifactId}
</message>
<level>INFO</level>
<toFile>/logs/log-echo.txt</toFile>
<append>true</append>
</configuration>
</execution>
</executions>
Similarly, we can print simple strings and properties. We can also set the log level using the level tag. Using the toFile tag, we can indicate the path to the file to which the logs will be saved. Finally, if we want to print multiple messages, we should add a separate execution tag for each one.
同样地,我们可以打印简单的字符串和属性。我们还可以使用level标签设置日志级别。使用toFile标签,我们可以指明保存日志的文件的路径。最后,如果我们想打印多条信息,我们应该为每条信息添加一个单独的execution标签。
4. Groovy Maven Plugin
4.groovy maven插件
To use groovy-maven-plugin, we have to put the dependency in our pom.xml:
要使用groovy-maven-plugin,我们必须在pom.xml中放入该依赖项。
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
</plugin>
Further, let’s add phase and goal in the execution tag. Next, we’ll put the source tag in the configuration section. It contains Groovy code:
此外,让我们在执行标签中添加阶段和目标。接下来,我们将把source标签放在configuration部分。它包含Groovy代码。
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
log.info('Test message: {}', 'Hello, World!')
log.info('Embed a line break {}', System.lineSeparator())
log.info('ArtifactId is: ${project.artifactId}')
log.warn('Message only in debug mode')
</source>
</configuration>
</execution>
</executions>
Similar to the previous solutions, the Groovy logger allows us to set the logging level. From the code level, we can also easily access Maven properties. Further, we can write messages to a file using the Groovy script.
与之前的解决方案类似,Groovy日志器允许我们设置日志级别。从代码层面来看,我们也可以轻松访问Maven属性。此外,我们还可以使用Groovy脚本将信息写入文件。
Thanks to Groovy scripts, we can add more complex logic to the messages. Groovy scripts can also be loaded from a file, so we don’t have to clutter our pom.xml with long, inline scripts.
多亏了Groovy脚本,我们可以为消息添加更复杂的逻辑。Groovy脚本也可以从文件中加载,所以我们不必在pom.xml中塞满长长的内联脚本。
5. Conclusion
5.总结
In this quick tutorial, we saw how to print using various plugins. We described how to print using maven-antrun-plugin, echo-maven-plugin, and groovy-maven-plugin. In addition, we covered several use cases.
在这个快速教程中,我们看到了如何使用各种插件进行打印。我们介绍了如何使用maven-antrun-plugin、echo-maven-plugin和groovy-maven-plugin打印。此外,我们还介绍了几个用例。
Finally, all the source code for the article is available over on GitHub.
最后,该文章的所有源代码都可以在GitHub上找到。