Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

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.

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.

2. AntRun Plugin

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:

<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:

<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.

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.

3. Echo Maven Plugin

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:

<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:

<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.

4. Groovy Maven Plugin

To use groovy-maven-plugin, we have to put the dependency in our 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:

<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.

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.

5. Conclusion

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.

Finally, all the source code for the article is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Maven (eBook) (cat=Maven)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.