Prev Next

Maven / Maven Interview questions

1. Types of maven repository. 2. maven repository central. 3. Explain dependency:copy-dependencies goal. 4. How do you rename a maven project? 5. Can we use different name for POM.xml? 6. What is Apache Maven? 7. What are the minimum requirements for a POM? 8. What jar:jar goal does? 9. Explain the highlevel directory structure of a maven project. 10. Maven artifact. 11. MOJO. 12. Build phases in Maven. 13. How do I skip the tests? 14. How can I run a single unit test? 15. Maven plugin. 16. POM. 17. Different types of dependency scope. 18. What makes a fully qualified name for the artifact? what uniquely identifies an artifact? 19. Maven Repository. 20. Why Maven is used? 21. Where do we find .class files of a Maven project? 22. Super POM. 23. Transitive Dependency. 24. What is Maven's order of inheritance? 25. How do I find the Installed maven Version? 26. Difference between compile and install. 27. Find details on the dependencies for the project. 28. What is a Maven project's fully qualified artifact name? 29. Order by which Maven searches for the dependency. 30. groupId. 31. artifactId. 32. Maven snapshot. 33. Explain the difference phases in Maven build Lifecycle. 34. How do I specify packaging/distributable format in Maven? 35. How Maven searches for dependency JAR? 36. Explain how Maven uses Convention over Configuration? 37. How to run unit tests using Maven? 38. How do I run a Single Test using Maven? 39. How to run a single test method using maven. 40. Difference between Gradle and Maven. 41. How do I install a 3rd party jar into the Maven repository? 42. Explain the advantages of using Maven. 43. Drawbacks of using Maven.
Could not find what you were looking for? send us the question and we would be happy to answer your question.

Types of maven repository.

types are,

  • local.
  • central.
  • and remote.
maven repository central.

It is the Maven established repository. For example, your POM specify the dependencies and it is not available in the configured local and the remote repository then maven looks for the resource in Maven Central. Maven provides most of the generic dependency resources at this remote location.

Explain dependency:copy-dependencies goal.

This goal when executed copies all the project's dependencies along with the transitive dependencies to the specified folder.

How do you rename a maven project?

1) Rename the project using Eclipse or other IDE.

2) Update the artifactId in your pom.xml

Can we use different name for POM.xml?

     Yes. You could mention file name using the -f option.

     mvn -f parent-pom.xml

What is Apache Maven?

Apache Maven is a software project management and comprehension tool. A build automation tool that helps managing the software build lifecycle.

What are the minimum requirements for a POM?

The minimum requirement for a POM are the following,
project root
modelVersion
groupId
artifactId
version
Test directory is src/main/test that has all the unit testing codes.

What jar:jar goal does?

It creates the jar files from the target/classes directory without recompiling any source classes.

Explain the highlevel directory structure of a maven project.

target folder holds the compiled unit of code as part of the build process.

Source folder usually src/main/java holds java source codes.

Test directory is src/main/test that has all the unit testing codes.

Maven artifact.

An artifact is a JAR, that gets deployed to a Maven repository.

Each artifact has a group ID , an artifact ID (artifact name) and a version string.

MOJO.

Maven plain Old Java Object. Each mojo is an executable goal in Maven.

Build phases in Maven.

There are 6 build phases.

Validate.
Compile.
Test.
Package.
Install.
Deploy.

How do I skip the tests?

Include the parameter -Dmaven.test.skip=true or -DskipTests=true in the command line.

How can I run a single unit test?

Use the parameter -Dtest=MyTestClassName at the command line.

Maven plugin.

plugin is a distribution of one or more related mojos.

POM.

POM (Project Object Model) is the fundamental unit of work. It is an XML file which holds the information about the project and configuration details used to build a project by Maven along with its dependencies.

Different types of dependency scope.

The element allows one of the 6 scopes: compile, provided, runtime, test, system and import.

Compile

default scope;it indicates what dependency is available in the classpath of the project.

Provided

indicates that the dependency is provided by JDK or web server or container at runtime.

Runtime

the dependency is not needed for compilation but is required during execution.

Test

indicates that the dependency is not required for normal usage of the application however it is only available for the test compilation and execution phases.

system

You need to provide the JAR which contains it explicitly.

import

Only used on the dependency of type pom in the . indicates that the specified POM gets replaced or imported with the dependencies in that POM.

What makes a fully qualified name for the artifact? what uniquely identifies an artifact?

Three properties group ID, artifact ID and the version string together identifies the artifact.

Maven Repository.

The location where all the project jars, library jars, plugins or any other project related artifacts that are stored and can be easily used by Maven.

Why Maven is used?

• Create a jar file
• Create war file
• Compile code
• Unit testing of code
• Documenting projects
• Reporting

Where do we find .class files of a Maven project?

Under the folder ${basedir}/target/classes/.

Super POM.

All models implicitly inherit from a super-POM. All Maven project POMs extend the Super POM, which defines a set of default configurarion shared by all projects.

Transitive Dependency.

This feature eliminates necessity to discover and specify the libraries that your own dependencies require and includes them automatically.

What is Maven's order of inheritance?

1. parent pom
2. project pom
3. settings
4. CLI parameters

How do I find the Installed maven Version?

mvn --version

Difference between compile and install.

Compile compiles the source code of the project whereas Install installs the package into the local repository, for use as a dependency in other projects locally.

Find details on the dependencies for the project.

using command,
mvn dependency:tree

What is a Maven project's fully qualified artifact name?

 <groupId>:<artifactId>:<version>

Order by which Maven searches for the dependency.

Local -> Remote -> Maven Central

groupId.

Identifies a project uniquely across all projects.

artifactId.

it becomes the name of the jar file.

Maven snapshot.

A snapshot version in Maven is not a real version and that will not been released. The same version may get many updates. Usually, snapshot dependencies should only exist during development and no released version (non-snapshot) should have a dependency on a snapshot version.

Explain the difference phases in Maven build Lifecycle.

Maven build lifecycle is defined by a list of build phases, where a build phase represents a stage in the lifecycle.

The default lifecycle comprises of the following phases.

validate - validates the project is correct and all necessary information is available.

compile - compile the source code of the project.

test - tests the compiled source code using a suitable unit testing framework. These tests does not require the code to be packaged or deployed.

package - take the compiled code and package it to its distributable format, for example, JAR.

verify - runs any checks on results of integration tests to ensure desired quality criteria are met.

install - installs the package into the local repository, for using it as a dependency in other projects locally.

deploy - performed in the build environment, copies the final package to the remote repository for sharing and collaboration with other members of the team and projects.

How do I specify packaging/distributable format in Maven?

The packaging for your project can be specified via the POM element <packaging>.

Some of the valid packaging values are jar, war, ear and pom. If no packaging value has been specified, it will default to jar.

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <packaging>war</packaging>
  ...
</project>
How Maven searches for dependency JAR?

Maven searches first for a dependency JAR in local repository. If found it is used else maven looks st the remote repository and download the corresponding version of JAR file and then stores it into local repository.

Explain how Maven uses Convention over Configuration?

Maven's features and plugins are initialized with default conventions and the basic functionality of Maven requires minimum or no configuration.

How to run unit tests using Maven?

Maven uses Surefire Plugin to execute unit tests. By default, the Surefire Plugin will automatically include all test classes with the wildcard patterns containing "Test".

Run tests using mvn test command.

How do I run a Single Test using Maven?

During development, you may run a single test class repeatedly. To run this through Maven, set the test property to a specific test case. Run the mvn test command with test property set to name of the test class you want to run.

mvn -Dtest=TestClass test
How to run a single test method using maven.

junit 4.x provides support for running specific test methods. You must use the following syntax by specifying the test class and method name. This also accepts patterns for method names.

mvn -Dtest=TestClass#mytest test

you may also select multiple methods using the below syntax.

mvn -Dtest=TestClass#testMOne+testMTwo test
Difference between Gradle and Maven.

Gradle uses domain-specific language based on the programming language Groovy, while Apache Maven uses XML for its project configuration.

Gradle is based on a graph of task dependencies while Maven is based on a fixed and linear model of phases.

Gradle allows incremental builds because it checks which tasks are updated or not.

How do I install a 3rd party jar into the Maven repository?

Use the below command to install external dependency into the maven repository.

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
    -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
Explain the advantages of using Maven.

Dependency management is simple. Maven automatically takes care of the transitive dependency for your direct dependencies.

Easy Source management. Maven does not require dependencies to be tracked in source control that makes the project simple and easy to maintain.

Powerful builds and easy build management.

Better colaboration.

Drawbacks of using Maven.

Learning Maven consumes time.

Remote repositories are not safe if it is not maintained propertly.

Integration required if the project's dependent jar is not mavenized.

«
»
Maven Interview questions II

Comments & Discussions