A multi-module Maven project is one that manages a group of submodules and is built from a parent POM. The submodules are normal Maven projects, and they can be built independently or from the parent POM.
The parent POM is located in the root directory of the project. We can include all configurations with dependencies in the Parent POM file which can be used across multiple submodules because submodules inherit everything from the Parent POM file.
Besides inheritance, Maven also provides the notion of aggregation. This POM also declares its submodules explicitly in its pom.xml file.
Submodules or subprojects are regular Maven projects that inherit configuration and dependencies from the parent POM. Each submodule also declares its parent module explicitly in its pom.xml file.
The main difference between multi-module parent POM and regular POM is the packaging type. For a multi-module parent pom, the packaging type is ‘pom’.
Application and advantages of using multi-module in a Test Automation framework
Real-life applications are built as a result of multiple layers, it is not just built with UI or API or Data layer. From a development point of view, any application consists of at least 3 components: data layer; web services; web app. From a test automation point of view, most of the companies have frameworks for different types: API automation; web automation; mobile automation.
So we can leverage a multi-module structure for the creation of our automation framework in order to have all types of automation tests in a single repo.
- A single command is needed to build all projects, including the submodules.
- Define common dependencies in the Parent POM; which will be implicitly inherited by all submodule.
Ex TestNG; log4j; apache-commons dependencies can be added in Parent POM; and it can be utilized in all submodules without declaring again in the submodules POM files.
- Helper classes / Utilities etc. can be written in one of the modules; and can directly be used in other modules. It leads to less code duplication and ease in code refactoring.
Steps to create a multi-module project
- Click on New Project -> Select Maven -> Enter the name of the project & click on the Finish button.
In this instance, the POM file is just like that of any normal maven project.
- Now delete the src folder as we don’t write code in the parent module, all coding parts will be done in submodules.
- Right-click on the project name -> Click on New -> Click on Module.
4. Select maven type and click on the Next button
5. Now provide module name (‘api-automation’), and select parent project in which you want to create this module. And click on Finish button
6. Now repeat steps 3 to 6 in order to create another sub-module (‘web-automation’).
At this point in time, the changes would be automatically reflected in Parent POM as well as sub modules’ POM as shown below.
Parent POM would be referring to its submodules via <module> tag.
<modules> <module>api-automation</module> <module>web-automation</module> </modules>
And submodule POM would be referring to its parent via <parent> tag.
<parent> <artifactId>multi-module-automation</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent>
- Now add required configurations & dependencies in the Parent POM that can be used in multiple submodules (ex TestNG, maven compiler plugin, log4j).
- Add tests and required utilities in the respective submodules.
Steps to create JAR of a module and use it in another module
- List of changes required in the ‘api-automation module’ for which we want to create executable JAR :
- Define groupId , artifactId & version of the module in pom.xml file so that it can be referred in the other module
- Add maven-jar-plugin in pom.xml file to specify the jar-related customizations.
- Add maven-dependency-plugin in pom.xml file that takes the list of project direct dependencies and copies them to a specified location.
- Now got to API module base directory.
- Run the following command —> mvn clean install -Dmaven.test.skip=true
The above command will create a jar file, and also copy the dependencies in the specified output folder at the path specified in pom.xml file.
2. List of changes required in the ‘web-automation module’ where we want to use JAR :
a) Add dependency of the module for which jar has been created. Also, define the jar path via systemPath tag in pom.xml file
After making the above-mentioned changes in both modules, you will be able to refer API module code (utilities, properties files, resource files, etc.) in the web module.
📌 NOTE: Whenever making code changes in the module for which JAR is already created, you need to make the JAR again in order to reflect the new changes in the module where JAR is being consumed.
Please check the sample multi-module Maven project with all the pom XML file changes from the GitHub link.