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

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.

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

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?

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

Refactor classes into a separate project

I intend to extract several classes and packages from one Java project and place them into another (which will have a distributable jar). That much isn't too difficult, but of course with such a large refactoring there are consequences. Namely there are many classes in the original project that are subclasses of the classes I want to extract. What's the best method for approaching this sort of refactoring?
You can create separate projects and the main project will have dependencies for all these projects. So in your IDE you can navigate through source code easily.
When building your application, each dependency could be built into a jar and the main application will be bundled with all the dependents jars in its classpath.
Let take as example a web app using plugins, and plugins using common classes, utilities and so on stored in a project named common-plugins.
project/webapp: having dependency on plugin1, plugin2 and common-plugin
project/plugin1: having dependency on common-plugins
project/plugin2: having dependency on common-plugins
project/common-plugins: having no dependencies
When building your project, you could build the plugins and the common-plugins into jars, bundled with your web app
project/webapp.war/WEB-INF/lib/plugin1.jar
project/webapp.war/WEB-INF/lib/plugin2.jar
project/webapp.war/WEB-INF/lib/common-plugins.jar
This way in your IDE, I will take eclipse for instance, you will have a workspace having 4 projects with dependencies as described above. At build using maven, ant, ivy, or what you want, you will build the 3 projects that the webapp project depends on, then bundle the whole stuff.
So basically this is what I did:
Create a new project
Copy over the appropriate classes from the old project to a new package in the new project, reconfigure until everything builds
Test that project separately and build it in to a jar
add jar as a dependency
Delete the classes from the original project
Manually change all the imports from the old packages to the new packages
What I was really looking for was some way to automate or streamline step 6 to make sure I didn't break anything, but I'm not sure it exists beyond mass find/replace.

Categories