NetBeans Quick Tip #16 - Using Dependent Projects

Guest Author

One NetBeans user asked me recently by e-mail when will NetBeans support dependent projects. So I replied how to define dependencies and asked if he wanted more from this features... and the reply was no, he just didn't know that the feature exists! So here's a tip for people who didn't find it yet, hopefully easily accessible via Google (which puts texts from blogs on top of search results).

Why would you want to use dependent projects? If your project gets big enough it is a good idea to divide it into smaller projects. By dividing them this way your code can be better structured by separating logical parts into individual projects. However if you do this, you need to define in first project how to access classes from the second project.

For Java SE projects this is quite straightforward, go to properties of the main project (right-click on the project and choose properties). Go to the Libraries section and choose Add Project. Now you need to find the project folder on your disk and by selecting it, the project's jar is added to the compile classpath:

If you compile the main project then all dependent projects are checked if they are up-to-date and any updated classes they contain are compiled as well.

There are other advantages of having dependent projects, especially if you work in a team, it's handy to have the projects divided than to work on one big project, because people can work on each of them separately. The only drawback I know of is that speed of compilation is slower if you divide your projects too much and define too many dependencies, then compiling the main project takes longer because NetBeans has to check status of all dependent projects.

Note that for NetBeans plug-in projects the dependence mechanism is quite different - either you depend on one of the NetBeans modules or you define dependence on an external library.

Join the discussion

Comments ( 7 )
  • Greg Trasuk Sunday, November 6, 2005

    Is there a way to add a dependency project that is a non-netbeans project with an Ant script? In other words, can I define the jar location and an Ant target to run? Or do I have to edit the dependent project's build.xml?

  • Roumen Sunday, November 6, 2005
    What do you mean by "non-netbeans" project?
  • Greg Trasuk Sunday, November 6, 2005
    Sorry, bad wording. I mean one of the following:
    • A project that started out not in netbeans, but has been imported as "Java Project with existing Ant Script", or
    • A directory that is in the file system and has an Ant script, but either hasn't been imported into Netbeans, or isn't open in netbeans. Further, we can assume I have an Ant target called "buildLib" or something like that, such that if I really wanted to edit the dependent project's build.xml, I could put in something like
      <code><ant inheritAll="false" dir="${SMTMonitor.home}" antfile="build.xml"/>
      and that would build the library jar file.
  • Roumen Monday, November 7, 2005
    I understand now. I don't know how to do that for free-form projects (I use them very rarely) or just for ant scripts, maybe somebody else can write about it. Looking at the dialogs it seems to me that you have to do it manually (the dialog only allows you to add jars to classpath).
  • David Caldwell Monday, November 7, 2005
    You can control this sort of stuff at a very low level, by hacking the project.xml file (Files tab in IDE, expand nbproject folder, it's in there). Under the java-data node, you can do things like specify the classpath of the project and even the boot class path (if you want to do things like develop vs. a different JDK version, or against a CLDC/MIDP profile, or something).
    This may only be a partial solution to what you're talking about; I don't use Ant to build projects (but rather a custom build tool that manages build dependencies itself and has an Ant wrapper which NetBeans uses). These file formats are documented somewhere; I don't know where, though ... I think I downloaded the schemas at some point.
  • Charles Simon Monday, November 14, 2005
    If you have an external Ant script project there is no place to add a library dependency (NetBeans 4.1), only classpath and source folders.
    I am not sure why this is true. An external Ant script project could surely be dependent on the outputs of another project.
  • Matthieu Saturday, February 18, 2006
    I am using netbeans 5.0 and I was able to set the dependencies of jar projects but not war. I mean that you can make a standard java application dependent but not a web application. I want to create a very simple installer which copy some war files from other netbeans web project than execute a some method.
    if any one of you know
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.