Maven compile fails with generated sources

Issue

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>