Can I have multiple source roots in a freeform project?
Yes. But there are at least three possible meanings of this question:
- I have two source dirs which are basically interchangeable and all get compiled together. I just put them in different trees because I felt like it - easier to diff, whatever.
- I have two source dirs but they are really separate. The first one gets built with some classpath, then the second one gets built with some other classpath which includes the *.class files from the first one. I.e. the second source root depends on the first but not vice-versa. (This is quite a common case. You could use two projects to represent it but sometimes that does not seem natural.)
- I have some pseudo-Java sources or templates or something which get processed by XDoclet, macros, etc. into some temporary sources, which then get compiled, and then I have some other sources which compile against the *.class files from the generated sources.
"Plain Java projects" (j2seprojects in our parlance) support scenario #1, and as of NB 6.7 also #3. It's not particularly recommended to do #1, since you can more simply collapse the source trees, though sometimes people keep "source" roots containing only classpath-structured resources (*.properties, *.png, etc.), for which this works fine.
Freeform projects support #1 and #2 fine. To choose #1, uncheck "Separate Classpath for Each Source Package Folder" in the Java Sources Classpath category of the Project Properties; to choose #2, check the box. The box is disabled (and checked, i.e. #2) if you have defined any Test Package Folders; put your tests in the Source Package Folders instead.
Please note that in case #2:
- The second source root must specify the output directory or JAR of the first source root in its classpath, just as the Ant script would do.
- The first source root should specify its output directory and/or JAR, so the association to the source files can be made when editing files from the second source root. (The project.xml actually supports more exotic scenarios still, but the GUI offers somewhat more restricted possibilities since these are the common cases. Generally, every <compilation-unit> will correspond pretty directly with one <javac> in your build script.)
Applies to: NetBeans 4.0 and later