Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have Netbeans automatically copy 3rd party jars from an included class-library to my project's dist/lib directory?

I have a Java Application in NetBeans 7.1 (let's call it myApp) that depends on a Java Class-Library Project [more than one, actually] where I keep some utility-classes I share among projects.

This Class-Library Project (let's call it myLib) depends on [many] third party libraries (e.g. apache-commons-younameit.jar).

I included myLib as a library, and NetBeans' Ant script pulls myLib.jar in the dist/lib directory of my myApp project; however, the jars upon which myLib depends are not pulled in together with myLib, so my project gets runtime exceptions due to the missing apache-commons-youtnameit.jar.

I would like to have both myLib.jar and apache-commons-younameit.jar automatically pulled into myApp's dist/lib folder, so that I don't have to manually check all of my libraries' dependencies and add them to my main project.

  • Is there an obvious way to accomplish this through NetBeans dialogs that I'm missing?
  • or is it necessary to manually tweak my build.xml? If so, could you kindly post an example? (I'm not that well up on Ant)

What I'd like to avoid is the following:

  1. I need to add a new utility library, picking from my Java Class-Library Projects.
  2. I lookup into that library's libraries what jars are used and write them down.
  3. I go back to my main project and add those 3rd party libraries (only the ones that I haven't included already for direct use from within my main project itself).

Thank you in advance for any help, or for pointing me in the right direction.

like image 410
Unai Vivi Avatar asked Feb 05 '12 23:02

Unai Vivi


2 Answers

I found a way to copy "dependant libraries' dependant libraries" as you said in one of the comments. Hopefully this helps.

Add the following code to your NetBean project's build.xml file, after the <import file="nbproject/build-impl.xml"/> line. You will need to fill out a proper relative path in the <fileset> tags below.

<!--Copy over any "dependant libraries' dependant libraries" -->
<target name="-post-jar">
    <echo>Copying over extra jars into PROJECTNAME</echo>
    <copy todir="./dist/lib">
        <!-- put in order from least important to most important to prevent file overwrites -->
        <fileset dir="path-to-other-nb-project/dist/lib"></fileset>
        <fileset dir="path-to-second-nb-project/dist/lib"></fileset>
    </copy>
</target>

Not the cleanest code, but it solved the problem for me. You'll need to manually update this as you add extra dependant projects to your main project.

like image 191
Gregory Peck Avatar answered Oct 02 '22 16:10

Gregory Peck


This is what worked for me in Netbeans 7.3:

Right-click on your project and select Properties, then select Libraries on the right side.

Then under "Libraries Folder" click the "Browse..." button: Properties dialog

Then choose your lib folder, unless it is already chosen automatically by Netbeans: Add Libraries Folder dialog

Then click Next, Finish as needed and when you come back to the Properties dialog, you should have: Properties dialog final Setup

After that, Netbeans should both copy the lib folder into dest and, most importantly, will reference all your libraries in the MANIFEST.MF file in your main JAR.

As a test, look if there is a new CopyLibs folder in the lib folder. Inside CopyLibs there should be a org-netbeans-modules-java-j2seproject-copylibstask.jar file. Netbeans added these and this is, I presume, what drives the copying of the lib folder into dist.

like image 31
Predrag Stojadinović Avatar answered Oct 02 '22 16:10

Predrag Stojadinović