Hi everyone.
Welcome back.
In this lecture we are going to learn about how to build a modular application using the Jpms.
So for this specific section we'll be working on the modules project.
So if you open the modules project it will have four different modules module one, module two, module
three and module four.
For this specific lecture we are going to be working on the module one.
Expand this project and then you will have a source directory over here.
And then in here you will have these different packages inside this module one.
And then if you take a look at it this one has a Dtos which is module one DTO which is a very simple
module record type.
In this case the name of this one is module one DTO and it is of type record and it has only one property
named name.
And if you take a look at the HTTP package we have HTTP client class is really empty.
It just have an instance of the client over here.
And if you take a look at the secret package.
So this is has a secret class and it returns a secret using the secret function.
And then we have another package named module one service.
So this one actually has a function named Retrieve Data.
And then this is the one which returns a module one DTO.
So this is all about this particular module.
What we are going to do here is that we are going to make this specific project to be modular.
So we are going to use the Jpms concepts and then make this project to be modular.
So very first thing in order to make this project modular, what we need to do is we need to add a class
named module Hyphen Info dot Java.
So I'm going to right click on the Java folder.
And then I'm going to create a file.
So you need to have the option selected as file over here.
And then we are going to mark this file name as module hyphen info dot Java okay.
So this is the name that you need to provide.
And you can see this is a regular Java file.
And the code will compile without any issues.
And one thing you need to make sure is this needs to be just below the Java directory.
So after this what I'll do is I'll quickly do a refresh over here.
Just to make sure the change that we made is going to be reflected in our workspace.
As you can see, the model refresh is happening over here and it just got completed.
And then what I'm going to do is that I'm going to be introducing you to the modular concepts now.
So the very first thing is in order to define a module you need to create a module like this.
And then you have to give a name for this module.
So I'm going to name this one as module one and then open and close parentheses.
So once this change is done you might be noticing there is a compilation issue right.
So this one is complaining about something.
This was working until now but we started seeing this problem.
Let's go ahead and open this HTTP client.
And then what it says is that if I mouse over on that one package Java dot.
Net dot HTTP is declared in module, but module does not read it.
So this means we need to explicitly add that module.
Let's see.
The IntelliJ is giving the recommendation.
The IntelliJ is giving the recommendation to add this one.
So this is what I said.
Any time you use a code that's not part of the base, the IDE has the ability to tell you what you need
to do in order to make this work.
So in this case, select this one.
And then if you go to the module file you will notice this specific directive added.
So in this case it is requires Java dot.
Net dot HTTP.
This is to make sure the HTTP module is available for our specific project.
So if you want a quick reference on the actual hierarchy if you go here.
So base is what that comes with out of the box.
So we don't have to add any additional module.
As soon as you add that module in for Java the base automatically comes with it.
But HTTP is something which is not going to come out of the box.
So you need to explicitly define hey, I need this specific module.
So make that module available for my project.
So that's exactly what we did over here.
So in order to make that work you need to provide the requires and then provide that base module or
the module that you need actually.
So this is the first thing you need to know about when you are using modular project.
So the next concept that I'm going to introduce you to is that it's going to be exports.
So in this case I have four packages here.
Right.
I have Dtos http secret and service.
So let's say I don't want to be exposing this secret service at all, because by default none of these
packages will get exported unless and until you call it out.
So you need to explicitly call out what this project is going to export or this library is going to
export.
So in this case I want to export comm dot module one dot Dtos.
I want to export this specific package.
When I say export, when you publish this as a library, these package and the classes that's inside
this package is going to be visible to the user of this library.
So the next thing that I'm going to export is a service.
And the next thing that I'm going to export is a HTTP or I don't want HTTP to be exported, so I just
want to export just these two packages.
So this is how you make your code visible to the outside world.
So when you build a Jar file The jar file is just going to have this dto's and the service package and
the classes under those packages.
So this is the first look at how do you build a modular Java application using the modules concept.
So what we learned in this particular lecture is that we learned about how do you add additional modules
other than the Java base by using this requires directive.
And number two is we learned about how do you export packages from this particular project to the outside
world, or any other client library that's going to use module one as a dependency using the exports
directive.
So with this information, I'm going to wrap up this specific lecture.
In the next lecture we will learn about how do you use module one as a dependency to the other module.
So basically we are going to learn about how to work with multiple modules.
When you have a modular project like this is available.
I hope you all were able to successfully implement this.
If you have any questions or clarifications, please don't hesitate to write it in Q&A.
I would be happy to help.