Course – LS – All

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


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:

mavenProject 1

First, let’s create a simple Java class file 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[]){

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/[3,29] package com.baeldung.database does not exist
[ERROR] BuilderHelper/src/main/java/com/baeldung/database/[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:


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.

Course – LS – All

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

res – Maven (eBook) (cat=Maven)
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.