1. Overview

In this tutorial, we'll explain how we can add multiple source directories in the Maven-based Java project.

2. Extra Source Directory

Let's assume we need to add a /newsrc source directory inside src/main:

First, let's create a simple Java class file DataConnection.java inside the src/main/newsrc/ folder:

public class DataConnection {

    public static String temp() {
        return "secondary source directory";
    }
}

After that, let's create another class file in the src/main/java directory that is using our DataConnection class created in the other folder:

public class MainApp {
    public static void main(String args[]){
        System.out.println(DataConnection.temp());
    }
}

Before we try to compile our Maven project, let's have a quick look at the project's structure:

Now, if we try to compile it, we'll get a compilation error:

[ERROR] BuilderHelper/src/main/java/com/baeldung/maven/plugin/MainApp.java:[3,29] package com.baeldung.database does not exist
[ERROR] BuilderHelper/src/main/java/com/baeldung/database/MainApp.java:[9,28] cannot find symbol
[ERROR] symbol: variable DataConnection
[ERROR] location: class com.baeldung.MainApp

We can understand the root cause of the error message – we've defined the DataConnection class outside of the general project directory configuration.

Maven supports only one source folder by default. To configure more than one source directory, we'll need to use a Maven plugin called build-helper-maven-plugin.

3. Add Source Directory with build-helper-maven-plugin

We'll use build-helper-maven-plugin to add a source directory in order to resolve the above error. This plugin helps us to achieve our goal with a minimum amount of configuration.

Since we have a sibling directory next to the src/main folder, we'll now add a second source directory:

  <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/newsrc/</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Here, we're running the add-source goal in the generate-sources phase. Also, we specified the source directory in a configuration.sources.source tag.

As we know, Maven's default lifecycle contains several phases prior to compilation: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, and compile. So, here, we're adding a new source directory before Maven compiles the source code.

Now, we'll compile the project, and then the build succeeds. After this, when we check the target folder, we'll see that the plugin generates classes from both source directories:

We can find the latest version of this plugin on Maven Central. We've only added one source directory in our example, but the plugin allows us to add as many as we want.

4. Conclusion

In this tutorial, we've learned how we can add multiple source directories using build-helper-maven-plugin.

As always, the full source code of the examples is available over on GitHub.

Generic bottom

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

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments