There are two types of projects that include multiple sub projects, they are projects that use inheritance and projects that aggregate multiple sub projects (also called multi-module projects). Let's review the difference between these two. Inheritance has a parent project that passes its values to its children.
- [Narrator] There are two types of projects that include multiple sub-projects. They are projects that use inheritance, and projects that aggregate multiple sub-projects, also called multi-module projects. Let's review the difference between these two. We'll start with inheritance. Maven makes project inheritance explicit to the POM, or the Project Object Model. Inheritance has a parent project that passes its values to its children. If you have several Maven projects, and they all have similar configurations, you can reduce redundancy in your projects by pulling out those similar configurations and making a parent project.
Once this is set up, and the projects are defined, to identify this as the parent project, those common configurations would then be applied to all of the child nodes. The parent-child relationship is defined from the leaf node upwards. In this case, I have four leaf nodes, or child nodes, that point to one parent node. The parent-child relationship deals more with the definition of a particular project. When you associate a child with its parent, you are telling Maven that a projects POM is derived from the parent POM, and then, additional elements can be added.
Now, let's take a look at a sample parent POM file. As you can see, it has a set of coordinates. Remember, the coordinates must be unique. The coordinates include the group ID, in this case, com.lynda, the artifact ID, in this case, I'm calling it parent, the version, this is version two. Those three things are the coordinates. Also notice that the packaging type is POM, so this project only requires a Project Object Model, which is an XML file.
Now, here is a sample child POM. Notice it specifies the coordinates of the parent. It has the group ID, the artifact ID, and the version of the parent, then it has the artifact ID, which is the name of this child POM, which is child. It's interesting to note that it does not explicitly specify its own coordinates, other than the artifact ID. That is, because it automatically has the same values as the parent. Now, let's talk about the multi-module project.
A multi-module project simply manages a group of other sub-projects, or modules. The multi-module relationship is defined from the topmost level downwards. When setting up a multi-module project, you are simply telling a project that its build should include the specified modules. Multi-module builds are to be used to group modules together in a single build. The parent POM in a multi-module build defines the list of modules to be included.
Let's take a look at an example of a parent POM for a multi-module build in one of the sub-modules. Here is the parent. Again, we have our group ID, our artifact ID, and our version, which make up our coordinates. Packaging type is POM, but the big difference from the inheritance POM is the tag that says modules. This include two modules, helloWorld and getName. As you can see, a multi-module project still has a parent POM, and it defines the list of modules included in the project.
In the root directory, you will find the parent POM. In the sub-folders, you will find all the sub-modules used by the project, and each sub-module also has its own POM file. Here's an example of a sub-module for this parent. To save space, I did not include the coordinates of the parent, but they would be the group ID, com.lynda, the artifact ID, multiParent, and the version, one. The artifact ID of the sub-module, in this case, is helloWorld, and notice that the packaging type is jar, a Java Archive file.
The dependencies, the helloWorld project depends on the getName project, so right now, it only has one dependency. When a Maven project includes multiple modules, it will add dependencies of child projects to the dependencies section defined in the parent POM. In addition to identifying the list of sub-modules, this parent POM can specify widely used dependencies across all projects, which inherit from a top-level POM.
For example, if your system makes universal use of the Log4j logging framework, or maybe the JUnit testing framework, you can list these dependencies in your top-level POM. Any projects that inherit POM information from this project will automatically have Log4j and JUnit dependencies. Although, both projects have a parent POM, and this allows them to share certain attributes, the biggest difference is that projects that use inheritance, the child node automatically includes everything from the parent node.
A multi-modular project, on the other hand, identifies the sub-modules in the parent POM file.
- Creating parent project
- Creating submodules
- Building a multi-module project
- Working with different IDEs
- Understanding Maven build lifecycles and build profiles
- Writing plugins