Ev Galois
Ev Galois

Reputation: 11

Merging 2 jars into one avoiding duplicate classes

I have two netbeans project and i need to merge the 2 output jars in only one "big" jar. The problem is that these 2 project share a lot of classes and this is obviously a problem. I'm already using ANT with the zipgroupfileset attribute but it copy all the classes. Is there a way i can choose all the classes from project 1 THEN the missing class from project 2? thank you very much for ur help

Edit: i'm forced to have 2 project (sometimes i don't even have the second project but just the jar) so i kinda have to make an ANT script i think. Is strange that there is no attribute like don't copy the already existing class

Upvotes: 1

Views: 212

Answers (2)

Daniel Moses
Daniel Moses

Reputation: 5858

Duplicate code is error prone. There are a few ways to resolve this duplication.

  1. Project dependency. Have one project depend on the output of the other.
  2. Combine the projects. If you are creating a single jar anyways, these projects might be candidates for merging into a single project. You can still manage code separation through modules.
  3. Third project that both depend on. Often we find ourselves using the same StringUtils or the likes among all of our projects. Instead of maintaining code in multiple locations you can pull out the util/misc code into another project that both of the other projects share.
  4. Rename classes. If for some reason the classes are named the same, but are not identical, then you need to decide if they are two things that should be namespaced/named differently, or if you want to merge and maintain 1 copy.

Upvotes: 1

btafarelo
btafarelo

Reputation: 627

Eclipse make it for you.

You can create one new project in eclipse, import yours jars inside project and export him.

When you select Export / Java / Runnable Jar, check the option "Extract required libraries into generated jar". Just one copy of each jar are copied.

Upvotes: 0

Related Questions