Prev Next

Maven / Maven Interview questions II

1. Explain Build Profile in Maven. 2. What are different types of Build Profiles in Maven? 3. What are the build lifecycles of Maven? 4. Explain package phase in Maven build lifecycle. 5. What are the phases of 'site' lifecycle in Maven? 6. What is local repository in Maven? 7. What is Maven Central Repository? 8. Explain Maven remote repository. 9. How do I include dependencies in a jar using Maven? 10. Difference between Apache Ant and Maven. 11. Create an executable JAR with dependencies using Maven. 12. Configuring resource directories in Maven. 13. How do I enable connection to the central repository through the firewall in my organization? 14. Difference between repository and dependency in Maven. 15. What does the POM comprise of in Maven? 16. What is an Archetype in Maven framework? 17. What are the uses of Maven Plugins? 18. What are the types of Maven Plugins? 19. maven-surefire-plugin. 20. maven-surefire-plugin report directory. 21. List the report file formats. 22. What is goal in Maven? 23. Goals of Surefire Plugin. 24. How do you invoke the surefire Plugin? 25. Difference between Snapshot and Version in Maven. 26. What are the phases of 'clean' lifecycle in Maven? 27. Difference between install and deploy command in Maven. 28. Difference between package and install in Maven. 29. Difference between POM and Effective POM in Maven. 30. How do I exclude all the transitive dependencies of a single dependency in Maven? 31. Maven: How do I rename the war file for the project? 32. List out the aspects does Maven Manages? 33. Maven: how do you display execution debug output or error messages? 34. Difference between Maven and Jenkins/Teamcity/Hudson. 35. How do I prevent the propagation of plugins to child POMs? 36. How to find the POM that contains missing transitive dependency? 37. What does the Maven command mvn clean do? 38. How a new archetype based project be created from command line? 39. How do I compile the application sources using Maven from command line. 40. Can a Maven project have multiple parents? 41. Explain 'pom' packaging in Maven. 42. What is Maven relocation? 43. Can we change the .m2 folder to any other name like .p2 or something? How? 44. Difference between mvn install and package. 45. What is JAXB2 maven plugin and where will you use it? 46. What is Maven BOM?
Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. Explain Build Profile in Maven.

A Build profile represents a set of configuration that is used to set or override default values of a Maven build. Using a build profile, a maven build can be customized for various application environments such as Production, Testing, Staging and development.

There are 4 different types of build profile.

  • Per Project. Defined in the project's POM itself (pom.xml).
  • Per User. Defined in the Maven-settings xml file (%USER_HOME%/.m2/settings.xml).
  • Global. Defined in the global Maven-settings (${maven.home}/conf/settings.xml).
  • Profile descriptor. a descriptor located in project basedir (profiles.xml).

A profile can be triggered or activated in several ways.

  • Explicitly.
  • Through Maven settings.
  • Based on environment variables.
  • OS settings.
  • Present or missing files.
2. What are different types of Build Profiles in Maven?

There are 4 different types of build profile.

  • Per Project. Defined in the project's POM itself (pom.xml).
  • Per User. Defined in the Maven-settings xml file (%USER_HOME%/.m2/settings.xml).
  • Global. Defined in the global Maven-settings (${maven.home}/conf/settings.xml).
  • Profile descriptor. a descriptor located in project basedir (profiles.xml).
3. What are the build lifecycles of Maven?

There are 3 built-in build lifecycles.

  • The default lifecycle handles your project deployment.
  • the clean lifecycle handles project cleaning.
  • the site lifecycle handles the creation of the project's site documentation.

A Build Lifecycle is Made Up of one or more phases.

4. Explain package phase in Maven build lifecycle.

package phase pulls the compiled code and package it to a distributable format, such as a JAR.

The below is the command to package a maven project.

mvn package  

5. What are the phases of 'site' lifecycle in Maven?

The following are the phases of site lifecycle in Maven.

  • pre-site,
  • site,
  • post-site,
  • and site-deploy.
6. What is local repository in Maven?

Maven local repository is located in your local system and is created by the maven when you run any maven command.

By default, maven local repository is %USER_HOME%/.m2 directory.

We can change the location of maven local repository by changing the settings.xml file. It is located in MAVEN_HOME/conf.

7. What is Maven Central Repository?

Maven central repository is located on the web created by the apache maven community.

The path of central repository is: http://repo1.maven.org/maven2/.

The central repository serves a lot of common libraries that can be browsed using this url http://search.maven.org/#browse.

8. Explain Maven remote repository.

Remote repositories refer to any other type of repository, accessed by a variety of protocols such as file:// and http://. These repositories are set up by a third party to provide their artifacts for downloading.

A "remote" repository may also be an internal repository set up on a file or HTTP server within an organization, used to share private artifacts between development teams and for releases.

9. How do I include dependencies in a jar using Maven?

Using jar-with-dependencies as the descriptorRef of your assembly-plugin configuration, we can create a JAR along its dependencies.

This built-in descriptor creates an assembly with the classifier jar-with-dependencies using the JAR archive format.

Below is the subset of pom.xml that includes assembly-plugin configuration along with jar-with-dependencies descriptor.

<build>
    <plugins>
      <!-- other plugin configuration -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

10. Difference between Apache Ant and Maven.

Ant and Maven are build tools provided by Apache Foundation. The purpose of these tools is to simplify the build process of a project.

Apache Ant Maven
Apache Ant is a toolbox.Maven is a framework.
Ant does not provide a formal conventions for project directory structure. Maven has formal conventions.
Ant does not have lifecycle; you have to add sequence of tasks manually.Maven build has lifecycle.
Ant is procedural and requires explicit information on what to do and when to do through code and also the order of the steps.Maven is declarative and the process can be configured in the pom.xml file.
Ant scripts are not reusable.Maven plugins are reusable.
Ant is mainly a build tool.Maven is a project management tool.
11. Create an executable JAR with dependencies using Maven.

To build an executable jar using maven, we need to add manifest configuration to the maven-assembly-plugin.

The below subset of pom.xml configure maven-assembly-plugin to create a single executable jar.

<build>
	<plugins>
		<!-- Build an executable JAR -->
		<plugin>
		<artifactId>maven-assembly-plugin</artifactId>
		<executions>
		<execution>
		<phase>package</phase>
		<goals>
			<goal>single</goal>
		</goals>
		</execution>
		</executions>
		<configuration>
		<archive>
		<manifest>
			<addClasspath>true</addClasspath>

			<mainClass>fully-qualified-MainClass</mainClass>
		</manifest>
		</archive>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
		</configuration>
		</plugin>
	</plugins>
</build>

12. Configuring resource directories in Maven.

By default, Maven configures and lookup into the src/main/resources directory for your project resources.

Also additional resources directories could be specified by adding the configuration to the project's Pom.xml.

<project>
 ...
 <build>
   ...
  <resources>
   <resource>
    <directory>
     src/proj_resources
    </directory>
   </resource>
</resources>
 ...
 </build>
 ...
</project>

We may configure several resources directories by adding multiple resource elements.

13. How do I enable connection to the central repository through the firewall in my organization?

The HTTP proxy settings need to be specified in the file settings.xml. The encrypted passord could be specified in the file. Also there is a need to add the dependency jar wagon-http-lightweight-2.2.jar that retrieves the artifacts through http using Apache httpclient -4.x. The jar has to be added to the maven folder lib/ext.

14. Difference between repository and dependency in Maven.

Repository is a collection of artifacts (eg: jars). You can think of it as a mere storage / cache of various artifacts.

Dependency is a situation where your project dependent on another artifact to perform its task. (For example., compile, run, unit test etc.)

15. What does the POM comprise of in Maven?

Maven POM contains the some of the following configuration.

  • project version.
  • project dependencies.
  • plugins.
  • goals.
  • build profiles.
  • developers and contributers.
  • mailing list.
16. What is an Archetype in Maven framework?

Archetype is a Maven plugin that creates a project structure as per its template.

The below is the command to create a new maven project based on an archetype.

mvn archetype:generate

17. What are the uses of Maven Plugins?

Maven plugin helps in,

  • Creating jar and war.
  • Compiling source code.
  • unit testing of code.
  • Creating project documentation and reports.
18. What are the types of Maven Plugins?

There are 2 types of maven plugins.

  • Build plugins execute during the build and should be configured in the element of pom.xml.
  • Reporting plugins execute during the site generation and it should be configured in the <reporting/> element of the pom.xml.
19. maven-surefire-plugin.

The Surefire Plugin is applied during the test phase of the build lifecycle to execute the unit tests of an application. It also generates reports.

20. maven-surefire-plugin report directory.

By default, the reports are generated at ${basedir}/target/surefire-reports.

21. List the report file formats.
  • Plain text file (*.txt)
  • XML file (*.xml)
22. What is goal in Maven?

A maven goal represents a specific task that contributes to the building and managing of a Maven project. It may be bound to zero or more build phases.

A goal that does not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

23. Goals of Surefire Plugin.

surefire:test, the only goal of the surefire plugin, runs the unit tests of an application.

24. How do you invoke the surefire Plugin?

By calling,

mvn test

25. Difference between Snapshot and Version in Maven.

In terms of version, when maven has downloaded a dependency project version 1.0, it will not download again the latest version of 1.0 while building the project. To facilitate the latest version the dependent project need to do upgrades it's version to 1.1.

In case of SNAPSHOT, Maven will automatically download the latest SNAPSHOT (:1.0-SNAPSHOT) of the dependency project everytime the project is built.

26. What are the phases of 'clean' lifecycle in Maven?

The clean lifecycle consists of the following phases.

  • pre-clean,
  • clean,
  • and post-clean.
27. Difference between install and deploy command in Maven.

mvn:install copies your packaged Maven module to your local repository (by default, in ~/.m2/repository), to be accessed by other local Maven builds.

mvn:deploy uploads your packaged Maven module to another (usually remote) repository, to be accessed by others.

28. Difference between package and install in Maven.

package: takes the compiled code and package it to its distributable format, such as a JAR.

install: installs the package into the local repository, for use as a dependency in other projects locally.

29. Difference between POM and Effective POM in Maven.

All Maven POMs inherit defaults from the Super POM.

For a basic Java project, we just need the Simplest POM, that defines a groupId, artifactId, and version, the three required coordinates for every project. you don't have to customize else as the other configurations will be inherited form the Super POM.

The Effective POM refers the merge between the Super POM and the POM from The Simplest POM.

30. How do I exclude all the transitive dependencies of a single dependency in Maven?

In Maven2, exclude a every single transitive dependency using separate <exclusion> list which is complex.

In Maven3 the use of wildcard is allowed on both groupId and artifactId that exclude all dependencies that propagates through to the module using this dependency.

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>TestApp</artifactId>
    <version>${project.version}</version>
    <exclusions>
        <exclusion>
            <groupId>*</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

31. Maven: How do I rename the war file for the project?

use the finalName tag to define/change the name of the war file in the web module that produces the war. See the below example.

<build>
  <finalName>MyWebApp</finalName>
 . . .
</build>

32. List out the aspects does Maven Manages?

Maven handles following aspects,

  • Build.
  • Documentation.
  • Reporting.
  • Dependencies.
  • SCMs.
  • Releases.
  • Distribution.
  • Mailing list.
33. Maven: how do you display execution debug output or error messages?

To view/display execution debug output call Maven with X parameter or e parameter.

34. Difference between Maven and Jenkins/Teamcity/Hudson.

Maven is a build tool and also provides dependency management, standard project layout and project management.

Jenkins/Teamcity/Hudson is a continuous integration tool which is much more than build tool. You can setup your CI environment using CI tools and automatically build, test and deploy your Java project.

35. How do I prevent the propagation of plugins to child POMs?

Setting inherited tag to false prevents the propagation to child POMs.

36. How to find the POM that contains missing transitive dependency?

Run "mvn -X" command.

37. What does the Maven command mvn clean do?

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

38. How a new archetype based project be created from command line?

A new project can be created using the command mvn archetype:generate .

39. How do I compile the application sources using Maven from command line.

Run the mvn compile command from the folder location where Pom.xml exists.

40. Can a Maven project have multiple parents?

Maven does not support multiple inheritance and a project can have only one parent. The parent project can have its own hierarchy.

41. Explain 'pom' packaging in Maven.

Pom packaging is used aggregate other projects and it acts as a container for sub modules within that project.

Pom packaging is also used to identify the parent in non-modular context.

42. What is Maven relocation?

Maven relocation identifies the projects/artifacts that are relocated or moved on the repository.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>bar</groupId>
  <artifactId>foo</artifactId>
  <version>1.0</version>
  <distributionManagement>
    <relocation>
      <groupId>org.bar</groupId>
    </relocation>
  </distributionManagement>
</project>

43. Can we change the .m2 folder to any other name like .p2 or something? How?

Yes. The local repo folder name can be changed by updating in the settings.xml. localRepository property in the file holds the path of the build system's local repository. The default value is ${user.home}/.m2/repository and it can be changed.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                  http://maven.apache.org/xsd/settings-1.0.0.xsd">
   <localRepository>${user.home}/.m2/repository</localRepository>
 
 </settings>

44. Difference between mvn install and package.

mvn package takes the compiled code and package it in its distributable format, such as a JAR.

mvn install installs the package into the local repository, for use as a dependency in other projects locally.

45. What is JAXB2 maven plugin and where will you use it?

JAXB2 maven plugin wraps and enhances the JAXB Schema Compiler (XJC) and allows compiling XML Schemas (as well as WSDL, DTDs, RELAX NG) into Java classes in Maven builds.

This is plugin is used to generate web service clients from WSDL by generating domain Java classes. Plugin can marshall (Java to xml ) as well as unmarshal (xml to Java).

46. What is Maven BOM?

BOM (Bill Of Materials) is a special kind of POM used to control the versions of a project's dependencies and provide a central place to define and update those versions.

BOM provides the flexibility to add a dependency to our module without worrying about the version that we should depend on.

«
»
Gradle interview questions

Comments & Discussions