How will you run test classes in Maven?

We need Surefire plugin to run the test classes in Maven.

To run a single test we can call following command:

mvn -Dtest=TestCaseA test

We can also use patterns to run multiple test cases:

mvn -Dtest=TestCase* test

or

mvn -Dtest=TestCaseA,TestCaseB,TestImportant* test

Sometimes Maven compiles the test classes but doesn’t run them? What could be the reason for it?

In Maven, Surefire plugin is used for running the Tests.

We can configure it to run certain test classes. Sometimes we you may have unintentionally specified an incorrect value to ${test} in settings.xml or pom.xml.

We need to look for following in pom.xml/settings.xml and fix it:

<properties>
<property>
<name>test</name>
<value>some-value</value>
</property>
</properties>

How can we skip the running of tests in Maven?

We can use the parameter -Dmaven.test.skip=true or -DskipTests=true in the command line for skipping the tests.

The parameter -Dmaven.test.skip=true skips the compilation of tests.

The parameter -DskipTests=true skips the execution of tests

Surefire plugin of Maven honors these parameters.

Can we create our own directory structure for a project in Maven?

Yes, Maven gives us the flexibility of creating our own directory structure. We just need to configure the elements like <sourceDirectory>, <resources> etc. in the <build> section of pom.xml.

What are the differences between Gradle and Maven?

Gradle is nowadays getting more popular. Google uses it for Android development and release. Companies like LinkedIn also use Gradle.

Gradle is based on Domain Specific Language (DSL). Maven is based on XML.

Gradle gives more flexibility to do custom tasks similar to ANT. Maven scripts have predefined structure. So it is less flexible.

Maven is mainly used for Java based systems. Gradle is used for a variety of languages. It is a Polyglot build tool.

What is the difference between Inheritance and Multi-module in Maven?

In Maven, we can create a parent project that will pass its values to its children projects.

A multi-module project is created to manage a group of other sub-projects or modules. The multi-module relationship is like a tree that starts from the topmost level to the bottom level. In a multi-module project, we specify that a project should include the specific modules for build. Multi-module builds are used to group modules together in a single build.

Whereas in Inheritance, the parent-child project relationship starts from the leaf node and goes upwards. It deals more with the definition of a specific project. In this case a child’s pom is derived from its parent’s pom.

What is Build portability in Maven?

In Maven, the portability of a build is the measure of how easy it is to take a particular project and build it in different environments.

A build that does not require any custom configuration or customization of properties files is more portable than a build that requires a lot of custom work to build it from scratch.

Open source projects from Apache Commons are one of the most portable projects. These build can work just out of the box.

What is the difference between a Release version and SNAPSHOT version in Maven?

A SNAPSHOT version in Maven is the one that has not been released.

Before every release version there is a SNAPSHOT version. Before 1.0 release there will be 1.0-SNAPSHOT.

If we download 1.0-SNAPSHOT today then we may get different set of files than the one we get on downloading it yesterday. SNAPSHOT version can keep getting changes in it since it is under development.

But release version always gives exactly same set files with each download.

How can we exclude a dependency in Maven?

To exclude a dependency we can add the tag under the section of the pom.

E.g.

<dependencies>
<dependency>
<groupId>test.ProjectX</groupId>
<artifactId>ProjectX</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!– exclusion is mentioned here –>
<groupId>test.ProjectY</groupId>
<artifactId>ProjectY</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

How Maven searches for JAR corresponding to a dependency?

Maven first looks for a JAR related to a dependency in the local repository. If it finds it there then it stops.

If it does not find it in local repo, it looks for the JAR in the remote repository and downloads the corresponding version of JAR file. From remote repository it stores the JAR into local repository.