Maven tips and tricks

Table of contents
Reading Time: 5 minutes

In this article, I would be talking about the tips and tricks for Maven which I learnt over the past few years. Maven is a build tool which is widely used with Java and It’s a favorite build tool of so many developers out there, so today I will be sharing some tips and tricks which could save a lot of time for you. These tips and tricks could be handy when you want to do something really fast. Alright, today I will share some commands which are going to be very helpful while development.

Have you ever wondered, How to run a single test file, or sinlge test case from a file or multiple test cases from a same file? Do you think if it is possible?

Yes, it is very much possible, you can do all of this. I am sure everybody knows How to run test cases in Maven, it is simple, right? mvn test

What if you want to do something like run a single test, multiple test or a single test file from a suite, we got it covered for you

Tip1 – How to execute test cases and test files different ways.

1.To run a single test file(single module project)

mvn -Dtest={TestClassName} test

2.To run a single test case in a test file(single module project)

mvn -Dtest={TestClassName}#{test case name} test

3.To run a single test file(multi module project)

mvn -pl {module-name} -Dtest={TestClassName} test

4.To run a single test case in a test file(multi module project)

mvn -pl {module-name} -Dtest={TestClassName}#{test case name} test

5.To run a multiple test case in a test file(single module project)

mvn -Dtest={TestClassName}#+{test case name2} test

6.To run multiple test case in a test file(multi module project)

mvn -pl {module-name} -Dtest={TestClassName}#{test case name1}+{test case name2} test

7.To run multiple test file (single module project)

mvn -Dtest={TestClassName1},{TestClassName2} test

8.To run multiple test file(multi module project)

mvn -pl {module-name} -Dtest={TestClassName1},{TestClassName2} test

9.To run test case with patterns(single module project)

mvn -Dtest={TestClassName1}#test* test

10.To run test case with patterns(multi module project)

mvn -pl {module-name} -Dtest={TestClassName1}#test* test

Pattern will try to match the testCase name given in the command where * indicates any name. So test* would result in testcase with name starting with test and rest could be anything.

Note- Please do use package name to reach the TestClass in the commands.

This was all about executing test cases different ways. I believe these commands cover all the ways we can execute test cases and test files using maven.

Tip2 – How to debug build failure using maven command

If you find any issues with maven build and see some errors with the build what would you do, of course you will immediately jump onto the console and see what went wrong but that’s not enough sometimes because you might have to deep dive into the problem, so you can simply debug the build with the following commannd.

mvn some-phase -X or mvn some --debug

Both the commands will produce the same output i.e Produce execution debug output

With the help of this debug output you can easily figure out the problem most of the time.

Tip3 – How to resolve dependency conflicts and transitive dependency issues.
We will not talk about transitive dependecy here for sure but transitive dependency issues are very painful at times as you will not be able to figure out the issue and will always try to resolve it manually i.e. check each dependency in the POM.XML and then make a call. We will make it easy for you, you just have to type in the following command.

mvn dependency:tree

The above command will execute and produce a dependency tree which will help you make out which dependency is coming from where and also, you will understand the relationship between dependencies used in the scope. If you see a dependency coming twice in the scope it will of course create issues but now you know what’s causing the issue so your next step would be to exclude the dependency from the parent dependency which is bringing it along. You can use tag to exclude the dependencies. For more info

Tip4 – How to do Profiling in maven?

Let’s say you have a project which is using several dependencies in the POM.XML and now what you want to do is, you want to create profiles based on environments i.e dev, test and some other. In dev profile you want to use X version of a dependency and in test profile you want to use Y version of dependency. This is very useful when you want to switch between different version without making any changes to the POM.XML and dependecy version. This is not just limited to dependecies you can also use this with plugins as well. If you want to skip the test cases for particular profile that can be done, let’s you are not interested in running test cases for dev profile. Also, this makes things more maintable.

Command – To activate a profile

mvn package -P dev

where dev is the profile.

Command – To activate multiple
profiles at the same time

mvn package -Pdev,test

where dev
and test are two separate profiles being used at the same
time.

Command – To activate profile from a specified
module in a multi module project.

$ mvn -pl {module-name} package -P dev,test

Above command activates dev and test profile in a specifed module .

Tip5 – Skip test cases while you run verify or install phase

Command1.

mvn clean verify -DskipTests=true

Command2.

mvn clean verify -Dmaven.skip.test=true

Both the commands are doing pretty much doing the same thing but there is only difference, command1 will compile the test cases and command2 will skip test cases compilation. So if you do not care about test case compilation or there are compilation error in your test cases, choose the second command.

Tip6  – Run a particular maven phase for a sinlge module(specified module) in a multi module project.

mvn -pl {module-name} clean

Example

mvn -pl {module-name} clean install

Tip7  – Run maven command with bigger heap memory.

MAVEN_OPTS="-Xms512m -Xmx1024m" mvn clean compile

where -Xms512m is the inital heap memory size in mbs for an application

where -Xmx512m is the maximum heap memory size in mbs for an application
You would be using this command when you want to run your application with bigger memory size or when you encounter OutofMemory Error for Java heap space.

Tip8  – Manage dependency versions in a multi-module project

You can simply use tag in parent POM.XML, what you have to do, you have to put all your dependencies inside the tag. Now, in the child modules if you want to use a dependency you just have to add the dependency without the version and it will work just fine and inherit the version from parent POM.XML and this way you can maintain the version of your dependencies. You just to make changes at one place and it will be reflected in all the child modules it is being used, otherwise if you have to make changes to a depency version you will have to make changes at all the child modules because of the version tag.

Tricks

Trick 1. Work offline and not download any dependency from interney, this is will essentially reduce the build time.

Command

mvn clean install -o or mvn clean install –offline

Trick2. Reduce the build time by increasing the number of threads used to make the build.

Command1.

mvn clean install -T 2

Command2.

mvn clean install -T 2C

Where T is the number of thread and C is the CPU so -T2C indicates two threads per CPU and -T 2 indicates two threads. When you will use this command you will certainly see a difference in the build time. Do try this.

If you liked my article, please help me spread this. Keep coding, Keep smiling.


Knoldus-blog-footer-image

Written by 

Deepak is a Software Consultant having experince of more than 5 years . He is very enthusiastic towards his work and is a good team player. He has sound knowledge of different technologies which include Java, C++, C, HTML, CSS, Javascript, C# always keen to learn new technologies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading