So let's find out about transitive dependencies and the module graph then we're going to update that
project to use these transitive dependencies.
So what is a transitive dependency?
Let's say we've got three modules A, B and C.
Now, when module A requires module B, module A can read public and protect types exported in module B
Now, if module B also requires module C then module B
can have methods that return types exported in module C. So let's see within our project. So in
our project, we've got three modules; common, db and ui. Now the module db requires and reads common
while module ui reads both db and common.
Now module db, actually the data source class has some methods that return types from module common.
So every module that uses the data source class will require common module as well.
Now this is where transitive dependencies can help us.
So instead of adding a requires statement to the UI module we can use the keyword transitive with the
requires statement.
So that way every module that reads the DB module will then automatically be able to read the common module.
So let's see an image just to clarify this further. So this image, we've got the module graph.
Now the result of Java modules depending on each other is that we can now draw a complete graph of dependencies
with the modules nodes and the relationship between nodes as module dependencies.
So this kind of image is called a module graph.
Now keep in mind that this graph only shows our modules, not other modules that we're using look like javaFX.base
for example.
So what does this have to do with transitive dependencies?
Well, you can see here our ui module depends on both the common and db modules.
Remember that the data source class returns some types from the common module and the UI module is
using the data source class to get results from the database.
So instead of requiring the common module in the UI module we can declare it as a transitive dependency
in the DB module.
So that way whatever modules used db module again would also have access to the UI module.
So let's clarify this further with another image.
So this image is now showing the transitive dependency. As you can now see, the module db requires
the module common by using the transitive keyword. So that way, all modules that require the db
module now have access to packages that are exported by the common module.
And we don't then have to specify the requires common module in the UI module because it now reads it
automatically.
So let's hop back into IntelliJ and see if we can set this up.
So firstly, we need to open our module info java file from db module, I've got that open up here
Click it to the relevant module info file but we're in our db one here
So what we need to do is make a change here and use a transitive keyword for the common dependency.
So we come up here to line 4, and we're going to type requires where its got academy.learnprogramming.common
It's going to be transitive, space and then the academy.learnprogramming.common
So now, the ui module can have access to all exported packages by the common module.
So in other words, we don't need to depend on the common module anymore in the ui module.
Let's go ahead and open the UI module descriptor file. Again, just check you're in the right file there.
And what we want to do here is- we no longer need the requires for our common module.
This is what the transitive keyword is going to do for us.
So now that we've done that,
bearing in mind that we are requiring that db module.
Of course we just made the change to the db module.
So now that we've done that, we should see that we are still able to run our application.
So as you can see, that's working nicely.
Every thing's working as it should there.
So again with transitive dependencies,
we then don't have to require, we don't need to require every single module.
So what other requires statements can be removed?
Well, let's just have a look at the one we've got on screen here, the UI
module descriptor file
And you can see there, we've got five requires statements, four of are them JavaFX related, then we
got the db module. Now, we can hold down the Ctrl or command key and click on a module name,
for example javafx.base, let's just take a look at that.
So you could see here what packages are exported by default and you can actually see here that requires
java.desktop, so I can close that, and go back to our ui module descriptor file.
If you click on the javafx.fxml, have a look at that
you can see here that it requires a java
fx.base, and it's using the transitive keyword.
So in other words, this means now that we don't have to depend on the java fx.base module because
we're already depending on the javafx.fxml
module which is using the transitive keyword to require the javafx.base module.
You can see that here on line 34 -- let's just close that down we'll go back and have
a look at our next one; controls, java fx.controls
Click on to that, so that's got two transitive statements there.
So in other words, if we require javafx.controls we can use the types from both
javafx.base and javafx.graphics. Closing that down again.
So what that tells us is that we can remove some of these statements.
We don't need them because they're being basically used by a transitive statement elsewhere, so we can
delete out the javafx.base and the javafx.graphics, because again they're associated elsewhere.
So that's cleaned up things nicely and let's just have another look at our model structure now.
Alright, this is the same image that we've already seen in some of the previous videos.
It now represents the final project structure.
As you can see now at this point, the common module only depends on javafx.base, the module db
our db module depends on java.sql, sqlite.jdbc and the common module or the ui module depends
on javafx.fxml, javafx.controls and our db module, so three transitive dependencies
the UI module can now read and use common modules because that db module declared that the common module
as a transitive dependency. So back to intelliJ,
again, let's just do one final check to make sure that things are working.
Looks like things are working ok. I just clicked on that.
It's all good to go. Alright, so the application is running without any errors and you could see there that
I was able to display the artists and the very songs that I've got for albums as well.
Let's finish this video now and I'll see you in the next one.