12 - Maven Interview questions

Q. What features should  a build tool have?

A. A build tool should have:

  • Compile java code and build jar, war and ear files for deployment and release.
  • Versioning and dependency management
  • Run tests and report test results
  • Run code quality check with Sonar, Checkstyle, Findbugs, etc.
  • Environment property substitution
  • Files generations ( wsdl, AspectJ, XSL,etc)
  • Support for cross platform (UNIX Windows) and IDEs(eclipse, Netbeasn, Intellij)
  • Proper documentation and support.

Q. What is Maven Build Lifecycle?

A. A Build Lifecycle is a well defined sequence of phases which define the order in which the goals are executed. A phase represents a stage in a life cycle.

Q. Which are the 3 build lifecycles of Maven?

A.The three build lifecycles of Maven are:

  • clean : erase artifacts created previous
  • default : is used to build the application
  • site: generates site documentation for the project

Q. How we can you verify if Maven is installed on Windows?

A. In command prompt, type mvn –version to verify the installation detail.

Q. What is POM?

A. POM the short name for Project Object Model is an xml file which contains information about the project and configuration details used to build a project with Maven.

Q. What is a Maven artifact?

A. An artifact is a JAR file which is located in the Maven repository. Maven build produces one or more artifacts such as compiled JAR and sources JAR. Each artifact includes a groupID, an artifact ID and a version string.

Q. Where are located the two settings files of Maven?

A. The settings.xml files are located at:

  • Maven installation directory : $M2_HOME/conf/settings.xml
  • ${user.home}/.m2/settings.xml

Q. How Maven searches for dependency JAR?

A. Maven searches first for a dependency JAR in local repository. If it discover that is used goes to the remote repository and download the corresponding version of JAR file and then stores it into local repository.

Q. How to enable connection to the central repository through the firewall ?

A. Quite all the companies have a firewall set up which determines the developers to connect to internet via HTTP proxy. They have to enable the proxy settings in the file settings.xml. The passord saved there has to be encrypted. Sometimes is need also to add the jar wagon-http-lightweight-2.2.jar that has the role to put and get artifacts through http using Apache httpclient -4.x. The jar has to be added to the maven folder lib/ext .

<proxies>
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
</proxies>

Q. Which are the build, source and test source directories for POM in Maven?

A. Build = Target

Source= src/main/java

Test = src/main/test

Q. Where are placed the compiled java class files?

A. They are located in ${basedir}/target/classes/.

Q. Which command line does transform a project into eclipse project?

A. mvn eclipse:ecplise

Q. What is a Maven Repository?

A. A Maven repository is a location where all the plugins, project jars, library jars or other particular project related artifacts are stored and used then  by Maven.

Q. What is the default location for the local repository?

A.~/.m2/repository.

Q. Which are the Maven repositories?

A. Maven repositoryes are: local, central, remote.

Q. What are the types of Maven Plugins?

A. Maven has two types of Plugins:

Build plugins – They execute during the build and should be configured in pom.xml file inside the build tags:

<build>
<plugins>
<plugin>
. <groupId>… </groupId> 
  <artifactId>… </artifactId> 
  <version>…</version> 
</plugin>
<plugins>
</build>

Reporting plugins – They execute during the site generation and should be configured in pom.xml file inside the reporting tags:

<reporting>
<plugins>
<plugin>
. <groupId>… </groupId> 
  <artifactId>… </artifactId> 
  <version>…</version> 
</plugin>
<plugins>
</reporting>

Q. What does it mean  “Maven uses “Convention over Configuration”?

A. This means that Maven’s features and plugins are initialized with default conventions and the basic functionality of Maven requires minimum or no configuration.

Q. What does the Maven command mvn clean do?

A. This command erase the target directory with all the build data before starting the building process.

Q. What are the phases of the Maven Build Lifecycle?

A. The phases of the Maven Build Lifecycle are:

  • Validate
  • Compile
  • Test
  • Package
  • Integration-tests
  • Verify
  • Install
  • Deploy

 

Q. What value can have packaging element in pom for a project that is only meta-data?

A. pom

Q. What is a Mojo?

A. A mojo is a Maven plain Old Java Object . Mojo is associated with a Maven goal. A custom plugin is a set of related Mojos (or goals) in a single plugin artifact.

Q. How a dependency can be excluded?

A. In pom.xml it can be used the exclusion element.

Q. How a project can be build offline?

A. The following command can be used:

mvn o package

Q. Why profile is used in Maven?

A. With profile the project gets portabilty . (on windows, linux, etc or in different enviroments development, test and production).

Q. How  selects Maven  the version of a dependency to be used when multiple versions of the same dependency of an artifact are found in POM?

A. If two dependency versions are at the same depth in the dependency tree, the first declared dependency is used. This mechanism is called dependency mediation.

Q. What is Archetype?

A. Archetype is a Maven plugin which has the task of creating a maven project structure.

Q. How can be created from command line a new project based on an archetype?

A. A new project can be created with the command:

mvn archetype:generate

Q. What means SNAPSHOT in Maven?

A. SNAPSHOT is a type of version that indicates a current deployment copy. Maven checks during each build for a new SNAPSHOT version in the remote repository.

Q. What is the difference between version and SNAPSHOT ?

A. Maven will download  always the specified version. In case of SNAPSHOT Maven wil download the latest SNAPSHOT.

Q. What is a transitive dependency in Maven?

A. Transitive dependency in Maven means that it is not necessary to discover and specify the libraries that our own dependencies require, Maven includes them automatically.

Q. How can be avoided running the tests in a regular maven install ?

A. In Eclipse from Run Configuration window it can be checked the option Skip tests or in command line add the argument –DskipTests=true

Q. What’s the role of the packaging type in a pom?

A. The packaging type determines the basic build behaviour. The most common pakaging types are: jar(default archive), bundle(OSGi bundles), war(web applications archive), pom (parent pom files).

The projects will run different goals depending of the packaging type. For example a project with pom packaging type will run site:attach-descriptor goal during the package phase and the project with jar packaging will run jar:jar goal instead.

Like us on Facebook