NetBeans Quick Tip #16 - Using Dependent Projects



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.
Comments:

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?

Posted by Greg Trasuk on listopad 06, 2005 at 09:46 dop. CET #

What do you mean by "non-netbeans" project?

Posted by Roumen on listopad 06, 2005 at 09:50 dop. CET #

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"/> </code> and that would build the library jar file.

Posted by Greg Trasuk on listopad 06, 2005 at 05:44 odp. CET #

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).

Posted by Roumen on listopad 07, 2005 at 02:39 dop. CET #

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.

Posted by David Caldwell on listopad 07, 2005 at 10:22 dop. CET #

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.

Posted by Charles Simon on listopad 14, 2005 at 11:14 dop. CET #

Hi, 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 thanks,

Posted by Matthieu on únor 18, 2006 at 06:07 dop. CET #

Post a Comment:
Comments are closed for this entry.
About

Roman Strobl

Search

Archives
« duben 2014
PoÚtStČtSoNe
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today