Java project structure: module dependencies in sources vs binaries - java

I've seen projects where one single project divided into modules, and each module is maven project itself. These modules integrated through one module which contains references to all other modules. To launch project user must import all modules in IDE. So, why people using this approach? Isn't much easier to package all modules to jar and include as dependencies to some module? Is there any benefit to use projects instead jars? Drawbacks of using projects are: user needs to keep all modules in IDE, may accidentally change source code, and if IDE starts to compile all that modules it takes a lot of time.

If you accidentally change one file, only this file gets recompiled, that's not a big deal.
Usually, you need to modularize your project to cut interdependencies and make everything more controllable. Often, the parent project doesn't even have any source files of its own; instead, it is only used to aggregate the modules. Modules are just used here to separate a big project into pieces.
You could develop those pieces as separate projects, but to implement a change in one module and make it available for other modules that use it, you'd have to build that module and actualize a dependency in the client module. That's cumbersome. It's much more practical to keep them as one big project where you just change the code you need to change, and it's available to all the modules that depend on it.

Related

Is there a way to use different Repository for a specific maven module project?

I've a maven multi module project in following structure. Our project is in GitHub corporate page.
I'm planning to make one of the maven module project public to let it be open source. However, because of maven multi-module project structure it needs to be under parent project hierarchy in file system, isn't it ?
What I want to do is creating a separate repository for publicModule2 and pushing it to public repo. It'll be an open source project. The rest will remain the same. Is there any way doing it without breaking maven multi module project structure ?
-- Maven parent project
-- commonsModule
--cusomModule1
--cusomModuleN
--publicModule2
P.S.As you know when you remove it from modules they're not compiling together each time and I don't want this. Without module structure I already separated another project but I don't look for that for this project. Please consider this in your reply
If you are serious about making it open source, that includes giving people an easy, reproducible way to build it. If it is required to be built as part of a larger project which they don't have access to, they won't be able to do that.
What you are looking for is possible with Git submodules. You would need to make the public one a submodule of the original parent.
From the docs, this feature is to be used when
you want to be able to treat the two projects as separate yet still be
able to use one from within the other.
However I would strongly advocate separating the projects so that the public one can be built independently.

Maven: How to split an existing maven project into different projects?

So I have this big spring boot project with hundreds of APIs and corresponding models. I was asked to seperate it into three different modules. Storefront, Order Management System and Utilities. As per my basic, plan I sorted, filtered and moved Storefront and OMS APIs to their corresponding projects. I moved all the model classes into Utilities, created a package, added this package to the local repository and included it as dependencies of Storefront and OMS. Further Iexported these two projects as a runnable jar with copying required libraries into a sub folder next to the generated jar. And I did this because the sub folder will include the package for Utilities, and if in future, I have to update something from Utilities, I could just replace this package and restart the server.
Everything is working fine, the problem is with the size of the final package. The jar size of the original project is 175 MBs. All three projects have similar .pom files. So all three project export to a size which is almost 175 MBs. And as I said, I included the package for Utilities in other two projects. So the size of the sub folder for Storefront and OMS became around 350 MBs.
Finally, my question is, is there any way to split a maven project into 3 different sub projects which can be built and deployed independently and, is there any way these 3 projects share a set of libraries which can be stored remotely and accessed by them independently so to decrese the size of the final runnable jar?
I think there are some deeper issues involved. If your artifact has 175MB and most of this are dependencies, than you have very very many dependencies.
So first of all, you should ask yourself, if all of those dependencies are really necessary. It is not unusual that people add a dependency just to use one simple class from that dependency. And this dependency than has a transitive burden. 175MB really calls for a deeper analysis of this fact.
Next, if you see you cannot really reduce the dependencies any more, you can split the project into several ones (like you started to do). But, then most of the dependencies should be in just one of the resulting projects. If all of your resulting projects use all of the dependencies, then these projects are all doing similar, probably overlapping things, which is not good.

Dependency between two classes

Is it possible to create something like "dependency" between two classes from different projects in one Eclipse. The point is that in both of this projects I need to have exactly the same class. I'd like to do something which allow me to make a changes in one of them and all the changes will automatically put in second. I hope that I explained it well ;)
I would-
Create a core project
Add core project to Project A
Add core project to Project B
Pretty much like how we use external libraries.
Create your common classes in a separate project. In eclipse set your build path for the other two projects to depend on your common project. When you release your projects, you build your common project as a separate jar and add it to the class path of your other projects.

java world terminology

I come from the .NET world, so I will ask in .NET terms in order to understand what is the java-world terms. I have experience with java.
I need to create asolution with two projects: library project and web application.
The library project uses hibernate and the web application should have reference to the library application.
I am using intellij and I saw there are various things like project and module. I don't know this terminology - can anyone explain me what king of 'projects' I need?
I saw that if I create a new project while working on project it opens a new window.
While I add a new module it adds the module project in the big project.
The terminology is IntelliJ's rather than a Java standard. (Other IDEs use similar terminology)
There is not much more to projects and modules than you have realised already. A single projects which is opened at once contains several modules which break up your work logically. A single modules can appear in multiple projects, but this can be more confusing than useful.
You can have one project with one module which is the simplest way to start.
You may want to look at using maven as this a portable way to manage your dependencies and build your project (all IDEs support maven and it can be run stand alone)
Even though I am a heavy IntelliJ IDEA user I always start from maven project, which I then import to IDE.
In maven you create one parent project (with pom packaging) and two submodules: one with war packaging and dependent one with jar (default). After importing you'll see two modules in IntelliJ.

Where should I put the main implementation in a multi module Maven project?

I have a small (4 module) maven project with the root pom in pom packaging. Right now, I have a impl module that contains the main methods and setup. This does lead to some issues, eg building a single jar for the entire project, (possible) huge dependency list containing all modules, and confusion trying to trace back to the main method for future developers.
So I'm thinking: Is it best practice to put this kind of stuff in the root project, or should it stay in its own module? Would there be any disadvantages of putting it in root (eg plugin issues)?
This does lead to some issues, eg building a single jar for the entire project
There are solutions for this (e.g. using a dedicated module and the Maven Assembly Plugin or the Maven Shade Plugin).
(possible) huge dependency list containing all modules,
I didn't understand what you're referring to here.
and confusion trying to trace back to the main method for future developers.
Just document things if nobody can transmit the information.
So I'm thinking: Is it best practice to put this kind of stuff in the root project, or should it stay in its own module? Would there be any disadvantages of putting it in root (eg plugin issues)?
Stephen is correct, aggregating and/or parent POMs must have a packaging of type pom and you can't put any code in them.
AFAIK, the parent module of a multi-module project has to have type "pom" and that would preclude putting any code or other resources into it.
Certainly, I wouldn't try this.
If you want to have all of your classes, etc assembled into a single JAR, there are other ways of doing this. For example, take a look at the Maven Shade Plugin.

Categories

Resources