Maven interview questions and answers
Q. What is the difference between a reactor pom and a parent pom?
A. The mechanism in Maven that handles multi-module projects is referred to as the reactor. A reactor pom is a pom.xml file you define with <modules>, and Maven will read all of these modules and build then in the correct order based on dependencies.
A parent pom is a pom from which other poms inherit via a <parent> section. Poms that inherit from a parent are referred to as “child poms.” There are different types of parent poms.
Q. How do you handle multi-module projects in Maven?
A. A multi-module project is defined by a parent POM referencing one or more sub modules. You can have different structures like
Approach 1. parent pom is in the project root
myproject/ myproject-client/ myproject-model/ myproject-services/ pom.xml
Approach 2. separate project for the parent pom
myproject/ mypoject-parent/ pom.xml myproject-client/ myproject-model/ myproject-services/
The parent pom.xml will define the modules that needs to be aggregated
<project xmlns="...."> <description>MyApp</description> <modelVersion>4.0.0</modelVersion> <groupId>com.myapp</groupId> <artifactId>myproject</artifactId> <version>1.0.0-RC1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>MyProject</name> <scm> <url>http://sdlc/svn/myapp/myproject/trunk/</url> <connection>scm:svn:http://sdlc/svn/myapp/myproject/trunk</connection> <developerConnection>scm:svn:http://sdlc/svn/myapp/myproject/trunk</developerConnection> </scm> <modules> <module>model</module> <module>client</module> <module>services</module> </modules> ..... </project>
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="ns"> <modelVersion>4.0.0</modelVersion> <artifactId>myproject-model</artifactId> <name>MyProject Model</name> <packaging>jar</packaging> <parent> <groupId>com.myapp</groupId> <artifactId>myproject</artifactId> <version>1.0.0-RC1.0-SNAPSHOT<</version> <relativePath>../pom.xml</relativePath> </parent> ... </project>
Q. Which approach do you favor?
A. Approach 1 is the is the default maven convention for multi-module projects, and the intention is to be scalable to a large scale build. If parent pom has its own life cycle, and if it can be released separately of the other modules, then approach 2 may be an option.
Q: What are the different aspects managed by Maven in the SDLC?
A: SCMs (Source control), Dependencies (i.e transitive dependencies), Builds, Releases, Documentation, and Distribution.
Q. What is the difference between "Dependency Management" and "Dependencies" as shown below in poms?
Parent pom with version defined
<dependencyManagement> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies> </dependencyManagement>
and child pom with no version.
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> </dependencies>
A. Dependency Management allows to consolidate and centralize the management of dependency versions without adding dependencies which are inherited by all children. It will be a a maintenance night-mare to change version numbers of all child poms in a large commercial project. dependencyManagement allows you to define a standard version of an artifact to use across multiple projects.
Q. What format does a maven repository use to uniquely identify an artifact in its repository?
groupId: e.g. com.myapp
artifactId: e.g. myproject
version: e.g. 1.0.0-RC1.0-SNAPSHOT
classifier: e.g. sources, javadocs, bin and bundle (default: no classifier)
type: pom,jar,war,ear (default is jar)
Q. What is an "attached artifact" and why would you need a "classifier"?
A. Attached artifacts are additional related artifacts that get installed and deployed along with the “main” (e.g jar, war, or ear) artifact. These are most often, javadocs, sources, resouce bundles, properties files, etc, but can actually be any file.
Attached artifacts automatically share the same groupId, ArtifactId and version as the main artifact but they are distinguished with an additional Classifier field.
You may like:
- Maven interview questions and answers - inheritance vs aggregation
- Maven - setting up profiles and tokenizing properties file