Skip to main content

Teaching Maven2 to use multiple source directories

Posted by inder on May 22, 2008 at 4:37 PM PDT

For project Gson, which is a library to convert Java objects to JSON and vice-versa, we use Maven2. We are also using Javacc to generate a JSON parser, but we had to modify the generated source-code to hide the generated classes. This meant that we had two source directories, one under src/main/java and another under src/generated/java. I tried hard to find out how to convince maven to use both the directories for compilation, but most websites really advise you to use multiple modules. In maven1.x, there seemed to be some support for it through maven:addPath but nothing in Maven2.

Finally, I did find a solution for Maven2 in this and this thread, and thought I would share it here. Essentially, you define an include section in your build section of pom.xml:






      <include implementation="java.lang.String">src/main/java/**/*.java</include>

      <include implementation="java.lang.String">src/generated/java/**/*.java</include>




See pom.xml of Gson for more details.

Have you run into a similar issue? Did you also find the answer hard to search? How did you solve this problem? Share your thoughts as comments to this blog. Thanks for reading.

Related Topics >>


This is one way to accomplish the goal, however the build-helper-maven-plugin is the generally accepted way to add additional source folders to a build. It also lets you do some things like attach random files to be installed/deployed, etc.
Apache Maven PMC

On second thought, the compiler config above may cause problems with other plugins such as IDE integrations. The build-helper-maven-plugin really is the correct way. I wrote more about it here.