Sonatype Blog: Latest Posts

Adding additional source folders to your Maven build

Occasionally you have a need to add additional source folders to your Maven build, usually when you are generating source via some external method or AntTask. Maven plugins that generate sources are expected to add the source folder automatically, but that’s of little consequence if your generation method is not supported by an existing Maven plugin. Fortunately, there is the build-helper-maven-plugin to assist you. The usage page gives plenty of examples. Adding an additional source folder is as simple as:

Several threads cited from Inderjeet’s blog suggest tweaking the compiler plugin’s includes settings to add the additional source folder. While this will work in simple cases, this method does not actually add an additional sourceFolder element in the project model. This means other plugins, particularly IDE integrations such as M2eclipse won’t see these folders and will cause you trouble down the road. We therefore suggest that you use the build-helper instead.

The build-helper lets you do some other handy things such as attach additional artifacts (attachedArtifacts), purge the project’s artifacts from the local repository and add additional testSource folders.