Merging 2 jars into one avoiding duplicate classes - java

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

Duplicate code is error prone. There are a few ways to resolve this duplication.
Project dependency. Have one project depend on the output of the other.
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.
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.
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.

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.

Related

Can I create an Eclipse Project that produces multiple executable Jars?

I have a project with a library JAR and a set of applications. Right now the applications (all 5 of them) are in a single project. These tools each have one or two classes, they run from the command line with one exception. Even the GUI tool has only about 8 classes. I am struggling with how best to structure this in Eclipse.
I have seen that I can use maven-shade to create multiple executable JARs from one project. I would need to convert the project to Maven, but I assume that is not a big problem.
I can create a separate project for each tool. This seems like overkill to have several projects with one or two classes in each.
Additionally, both of these solutions will leave me with either a bare class file or a JAR plus the dependent library JAR. Not ideal for deployment.
Before I pick one of these, I thought I would ask here if there is a different approach that I am missing for packaging these tools.
In eclipse, Export --> Archive file, You can select/deselect what classes you want to export into your Jar File
Yes, but You could/need to create using Maven, and for each build building jar you need to set the main Class.
Checkout the documentation of Maven.
I think You could to the same with Gradle, but not sure.
https://maven.apache.org/plugins/maven-jar-plugin/examples/attached-jar.html

Could not create a maven script with such a dependency (interdependent projects)

I have a case where there are multiple projects and each are interdependent and I could not create a maven script with such a dependency because jars wont be ready to use as both are interdependent. Do we have a solution for such cases in maven? The solution what i see here are below:
Remove the dependency between the projects means change the code to remove such interdependent.
Merge two projects into one in the build - means create single jar by picking up source from 2 projects.
I dont want to do either of do as it needs additional efforts to do so.
Is there a way source of one project can be used in another project and not being part of JAR?
In Maven you cannot build interdependent jars. This problem also arose in our company when we moved legacy projects to Maven. In my opinion one should really put effort into solving this situation, so use your proposals (1) or (2) or generate a third project which contained the shared classes on which both jars depend. Any "trick" to avoid this would be as complicated and lead to a bad project structure.

Well structured organization of jars - is it possible for a set of projects?

Here and on the net I see much advice about how to put jars into a project. There are many ways for it. But a real life Project consists of many eclipse projects. And many of them have same jars.
I have one real project consisting of a few (a dozen) java projects. And the Ant is used for building.
I want to keep my and standard jars so:
It should be some understandable structure,
Jars should be easy to reach for referencing (excellent if I can include them by groups)
Jars should be easy and safe to update. I would like to minimize the possibility of having two jars of different versions in different projects
If I reference the projects from each other, I will quickly come to loop references. If I put every jar physically into each project, it is bad because of duplicity - later I will surely forget to update the jar in some of the projects. If I put a jar into some project physically and reference it from others, later I will have to search, where the appropriate jar is, in order to update is. The only structure that I had managed to think of, is to divide jar into some subsystems, make extra projects (library projects), that will contain these jars and to reference these projects from the real ones.
But maybe Eclipse or Ant have some automated solution?
(If it is impossible, can IntelliJIdea or Maven or Gradle or something else do the thing?)
Again, please, notice, I am NOT talking about how to put jars into a project, but about how to organize them in a set of projects.

How to configure Maven for a single codebase but multiple artifacts

I have a single very large codebase that compiles down to a JAR. I also use the shade plugin to compile it down and package it up with all dependencies. I also use the war plugin to get a WAR file.
With respect to the WAR file, once the goal is created I have a post-build event that simply copies the WAR file to its destination, so technically I'm good with that.
The problem I have is with the other two JARs. They both share the same artifactId since they're both built in a single pom.xml and this isn't acceptable for a number of reasons (including, but not limited to the fact that some caching of dependencies is pretty dumb about realizing that one JAR is the thin one and one is the full-dependency one).
What I need to do is create a pom.xml (or group thereof) suitable for builds (by Jenkins) that can use the same codebase (pulled from Github) but create two separate JAR files, each with their own artifactId.
Being a Maven novice, I've read through the beginning book and it seems to me that what I want is a parent pom.xml with two modules. But from what I can tell, each module means a separate directory with separate code. As I said, this is built from the same codebase. The only difference is one is built from the "regular" build, and the other is built using the "shade" plugin and goal.
The only other thing I can think of is build the "regular" JAR and then build the shaded JAR with a classifier of "full?" If this is the answer, may I humbly ask for some adult supervision on how to do this, as I'm not seeing how.
If that's not the answer, I suspect this must be a common problem, so again, some guidance would be very helpful!
The solution I came up with was to use a classifier for the "shaded" jar. Thus, the artifacts don't collide.
I then had an issue accessing it, but found the solution to that issue as well - How do I access a jar with a classifier?

Separate class paths for test code and main code

I have two source trees in my Java application in Eclipse. One is "src", and one is "test". Each one contains a directory tree of .java files. So far, so good.
However, I would like the classes in "test" to be able to see the classes in "src", but for the classes in "src" to not be able to see the classes in "test". Similarly, I want to include some test libraries. I don't want any of those libraries to be available in the "main" tree, lest I accidentally use one without thinking.
Is there an easy way to set this up in Eclipse?
There is no way to solve this in Eclipse without splitting the two source trees into separate projects (with one depending on the other). Maven could handle that at build time though.
You don't need separate projects if you use IntelliJ. You just specify application and test packages. It's possible to mark libraries as part of application and test as well.
Eclipse ought to have equivalent buried under all those plugins. If not, IntelliJ has a community edition now.
Or just use Ant. Personally, I prefer it to Maven. I find Maven to be too complex for such a simple job.

Categories