Maven

Resume Maven Build From the Step it Failed

In this article, we delve into the intricacies of Maven, exploring how to navigate and overcome one of the common challenges developers face — resume a failed Maven build.

1. Introduction

Maven, the renowned build automation and project management tool in the Java ecosystem, is celebrated for its ability to streamline and simplify the software development process. It empowers developers to manage dependencies, compile source code, run tests, and package applications efficiently. Yet, like any tool, Maven is not immune to occasional hiccups. Build failures can occur for various reasons, whether due to misconfigurations, network issues, or errors in your code.

2. Setting Up the Project

In this scenario, we’ll create a simple Java project and deliberately introduce an error to simulate a build failure. Then, we’ll follow the steps to resume the failed build.

2.1 Creating the Modules

We will create a main module with the name mavenResume. Then we create two submodules, tools and logic. These are the corresponding pom.xml files for each one.

mavenResume pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mavenResume</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>logic</module>
        <module>tools</module>
    </modules>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

tools pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mavenResume</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>tools</artifactId>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

logic pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>mavenResume</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>logic</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>tools</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

2.2 Main.class of Module logic

We will create a Main class to help us demonstrate an error while using the maven commands.

Main.class

package org.example;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

3. Maven Build Failed Error

3.1 Recreating the Error

To demonstrate the error, we will create a typo error in the Main class. We can write prontln enstead of println. Then we wil use this command to install the module:

mvn clean install

This is the error:

Fig. 1: Maven Build Failled Error.
Fig. 1: Maven Build Failled Error.

3.2 Using the -rf Flags(For Multi-Module Projects)

In a multi-module Maven project, you may want to resume the build for a specific module rather than rebuilding the entire project. You can do this by using the -rf flag followed by the module’s name that failed, but first, we have to fix the error that we created in the Main class. Then we type this command:

mvn clean install -rf :logic

As we can see, maven install continued from the module logic that had failled, and finished all the steps.

Fig. 2: Maven -rf Flags.
Fig. 2: Maven -rf Flags.

4. Download the Source Code

This was an example of how to resume the maven build when it fails from the same step.

Download
You can download the full source code of this example here: Resume Maven Build From the Step it Failed

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button