Maven compile fails with generated sources

I'm using exec-maven-plugin to generate classes during the maven build in the folder target/generated-sources/java. When I compile, I see the classes are correctly created in this folder. However, the build fails because I cannot reference these classes from the code

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
              <goal>java</goal>
          </goals>
        </execution>
    </executions>
    <configuration>
      configuration here
    </configuration>
</plugin>

Solution

The issue is that the path where sources are generated is not within the maven source paths. You can fix this with build-helper-maven-plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
Alternative #1

I've run into this issue in multi-module Maven projects, and sometimes the problem is that the generated sources are not being picked up due to the order of plugin execution. Make sure that the plugin generating sources runs before the compile phase and that the build-helper-maven-plugin is configured in the correct module if you're using a parent POM.

You can also explicitly bind the add-source goal to the generate-sources phase:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-generated-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

This ensures the generated sources are always included before compilation starts.

Alternative #2

Another thing to check is your IDE configuration. I've seen cases where Maven builds fine on the command line, but the IDE (like IntelliJ IDEA or Eclipse) doesn't recognize the generated sources.

For IntelliJ IDEA:

  • Right-click the generated-sources/java folder and mark it as "Generated Sources Root".
  • Make sure "Delegate IDE build/run actions to Maven" is enabled in settings.
  • Reimport the Maven project after running mvn generate-sources.

For Eclipse:

  • Run mvn eclipse:eclipse to regenerate project files.
  • Refresh the project and check that the generated sources are included in the build path.

This ensures your IDE recognizes and compiles the generated sources just like Maven does.

Alternative #3

If you're using annotation processors or tools like Lombok, MapStruct, or JOOQ, make sure the annotation processor paths are set up correctly. Sometimes the generated sources are created in a different directory (like target/generated-sources/annotations).

You can add multiple source directories with build-helper-maven-plugin:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-annotation-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/java</source>
                    <source>${project.build.directory}/generated-sources/annotations</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

This ensures all generated sources are included, regardless of which tool created them.

Last modified: March 8, 2025
Stay in the loop
Subscribe to our newsletter to get the latest articles delivered to your inbox