Maven - 10 Things Java developer should know

The Apache Maven is an essential tool for Java developers. It makes their life easy by allowing them to create Java project faster by using a standard directory structure. It also helps them to download project dependency automatically. Not only that, Maven also download transitive dependencies which relieve Java developers from the big headache of keep check of different versions of dependent libraries. For example, if your application is dependent on Spring framework but Spring is dependent on Log4j then you also need to download the correct version of Log4j JAR files for Spring MVC framework, Maven does this automatically for you. Maven also allows you to build projects, upload artifacts to the central or local nexus repository and deploy the release in your various test environments automatically.

In short,   good knowledge of Maven is must for any professional Java developers. In this article, I'll share some practical tips and facts about Maven tool which every Java developer should know.

These tips and knowledge will help you to make most of the Maven and simplify your day by day development task e.g compilation of project, automatic unit testing of builds, deployment, regression testing, and many other tasks.

10 Essential Maven Tips for Java Developers

Here is my collection of some of the useful Maven tips for Java and JEE developers. Since Maven is not only used to build core Java projects but also web and enterprise applications, these tips will help both core Java and web developers alike.

1) A Maven is a build tool like ANT which is used to create deliverable like JAR, WAR and EAR file in Java environment. Though there are a lot of differences between ANT and Maven e.g. later is very easy to use because of minimal configuration required. As Maven uses convention over configuration which means lots of sensible default values. See here to learn more differences between Maven and ANT tool.

2) Main Benefit of using Maven for building Java application is dependency management. Since Java application depends upon many internal and external open source libraries with different versions, it makes sense to have an internal maven repository to archive all versioned JAR file of both internal and external libraries and let every application developer import them automatically using Maven while building an application. Maven itself maintains libraries as known as global maven repository with URL e.g.

3) Maven also manages transitive dependencies for you, what this means is when you include Spring framework as a dependency in your project, you don't need to download all the libraries on which Spring is dependent.

Maven will download those JARs for you and it will also take care of version, this is a seriously good feature to save a lot of time which goes to setup build environment and then deal with nasty errors like UnSupportedClassVersionError and NoClassDefFoundError which generates due to incorrect versions of dependent libraries.

4) In order to use Maven, you need to install Apache Maven in your machine configure settings.xml, which contains the name of local repository and connection detail to connect to the global maven repository. Please follow the steps given in this guide to install Maven in Windows 8 machine.

5) If you are using Eclipse IDE for Java development then you can also use M2Eclipse Maven plug-in which gives you the ability to create, modify and run maven projects from Eclipse. M2E Eclipse plug-in can be downloaded and installed from Eclipse Marketplace. See  3 Maven Tips for Eclipse Java Developers if you are using Maven with Eclipse IDE for more tips.

6) Once Maven is installed and configured on the local machine, you can create Maven projects. Maven project enforces a standard directory structure for organizing source, resource, and test classes. They follow convention over configuration which means all Java classes will go to the src/main/java folder and all test classes will go to the src/main/test folder. See Maven Essentials to learn more about Maven convention over configuration.

7) Another important thing in Maven is pom.xml, this is similar to build.xml of ANT build tool and for every project, you have their pom.xml which stand for Project object model. This pom.xml contains project details e.g. artifactId, groupId, name etc which is used to create deliverable like JAR file and can be used to upload JAR in the internal maven repository.

The pom.xml also contains Maven plugins used in project e.g. JAR plug-in which is responsible for creating a JAR file from class files and resources. pom.xml also contains all dependency e.g. JAR files and their version required by your Java project.

8) You can build and run Maven project either from the command line or Eclipse itself. Similar to task Maven has goals and you can run those goals by invoking them using the mvn command in command line.

In order to build and run maven project from command line, you need to include maven binaries e.g. MAVEN_HOME/bin in your PATH. If you have M2Eclipse plugin installed then you can directly perform maven clean, maven installs from Eclipse itself. clean and install are two frequently used maven goals.

9) When you build the project using Maven it creates class files and copies resource in the target directory. If you are using Eclipse then you can also build your Maven project by using M2Elcipse plugin, just right click and choose Maven Install or Maven Build option as shown in below image:

10 Maven tips Java developer should know

The Maven install command run both maven clean and maven build together. The maven clean will clean old build artifacts e.g. class files from the target directory.

That's all about some of the useful Maven tips for Java developers. Even though now Gradle, another build tool written in groovy, is capturing some ground of Maven, it is still the most popular build tool for Java projects. Gradle will take its time because it has a little bit of learning curve in terms of groovy and domain specific language you use to write the build script but given Maven uses XML, anybody can understand the build process and build dependencies.

Further Reading
Maven Fundamentals
Maven The Definitive Guide
Difference between mvn install and mvn release command
Free Maven Books for Java Developers
How to increase heap size of Maven and ANT

Thanks for reading this article so far, if you like this article then please share with your friends and colleagues. If you have any question or suggestion then please drop a comment and I'll try to find an answer for you. 


Anonymous said...

These aren't tips.

Anonymous said...

where are the tips ?

AnuragUpadhyay said...

@Javin Sir,
Please put some light on built plugin and its config, related to automatically deploying the war file into tomcat wabapp folder.and same config for multi maven project as well.

Javin Paul said...

@Anonymous, well I agree these are not tips but points which Maven user should know. You can find the real tips in my earlier post, 3 Eclipse Maven tips for Java developers.

Javin Paul said...

Hello @Anurag, sure, we'll share more about build plugin, automatically deploying the war file into tomcat using release plugin and some config for multi maven projects as well. Thanks for your suggestions, nice ones.

Post a Comment