Maven - 10 Things Java developer should know

likeThe Apache Maven is an essential tool for Java developers. It makes their life easy by allowing them to create a Java project faster by using a standard directory structure. It also helps them to download project dependency automatically. Not only that, but Maven also downloads transitive dependencies that relieve Java developers from the big headache of keep check of different versions of dependent libraries. For example, if your application is dependent on the Spring framework, but Spring is dependent on Log4j then you also need to download the correct version of Log4j JAR files for theSpring 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 a must for any professional Java developers. In this article, I'll share some practical tips and facts about the 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 compilation of the project, automatic unit testing of builds, deployment, regression testing, and many other tasks.

Btw, if you are just starting with Maven, I suggest you to first go through a comprehensive Maven course like Apache Maven: Beginner to Guru to learn some fundamentals. It will not only help you to build and deploy your Java and Spring Boot projects using Maven but also learn Maven itself in depth.

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) Build Tool

A Maven is a build tool like ANT which is used to create deliverables like JAR, WAR and EAR file in Java environment. Though there are a lot of differences between ANT and Maven like later is very easy to use because of the minimal configuration required.

Maven uses convention over configuration which means lots of sensible default values. See here to learn more differences between Maven and ANT tool.

2)  Dependency Management

The most important benefit of using Maven for building Java applications is dependency management. This means both storing dependent JAR files on a central location as well as downloading them automatically without wasting Developer's precious time.

Since Java application depends upon many internal and external open source libraries with different versions, it makes sense to have an internal maven repository like Nexus repository or may an Artifcatory repository to archive all versioned JAR files 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 a global maven repository with URL likehttps://repo1.maven.org/, which contains JAR files for almost every possible Java libraries. This is also knowns as Maven Public repository.

Btw, if you are first time hearing about Maven repository and didn't know that a public repository existed or you can have a private Maven repository like Nexus and Artificatory, then I suggest you go for a Maven Crash Course to brush up your fundamentals, it will help you immensely. It happened to many of us using Maven that we don't know from where do all those dependent jars are coming and that's where a fundamental course helps.

maven crash course for beginners Java develoeprs

3) Transitive Dependency

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) Configuring Maven

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 thisguide to install Maven in Windows 8 machine.

5) M2Eclipse Plugin

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.

When you use Maven from 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.

Though M2Eclipse offers a convenient interface to build your Maven project, it's also important to know how to use Maven from the command line. You should at least be familiar with essential Maven commands like mvn install, mvn clean, mvn build, etc. If you need a refresher, I suggest you check out Maven Fundamentals Course by Bryan Hansen  on Pluralsight. It's a great course to learn Maven commands.

10 Maven tips Java developer should know

You can also further see 3 Maven Tips for Eclipse Java Developers  if you love using Maven with Eclipse IDE for more tips.

6) Pom.xml

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 stands for the Project object model. This pom.xml contains project details e.g. artifactId, groupId, name, etc which is used to create deliverables like JAR file and can be used to upload JAR in the internal maven repository.

The pom.xml also containsMaven plugins used in a project like 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.

7) Task vs Goals

You can build and run the 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 the command line.

In order to build and run a Maven project from the command line, you need to include maven binaries like 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.

8) Convention over Configuration

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.

You can further check Apache Maven: Beginner to Gurubook to learn more about Maven convention over configuration concept and how it helps you to create and build your project and understand the structure of other open-source Java projects which use Maven.

best course to learn Maven for Java developers

9) Target Directory

When you build the project using Maven it creates class files and copies resources in the target directory. You can find all your class files inside the target/classes directory. When you run the Maven clean command it cleans up this directory to compile your Java files again and create new class files.

On the other hand, mvn install command runs both Maven clean and Maven builds 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 the 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 Learning

Maven Fundamentals by Bryan Hansen

Maven Crash Course

Maven Essentials: The Book

10 Essential Tools Every Java Developer should Learn

Difference between mvn install and mvn release command

Free Maven Books for Java Developers

How to increase the heap size of Maven and ANT

10 Maven Plugins for Java Developers

Top 5 Courses to Learn Apache Maven for Developers

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

P. S. - If you are looking for some free courses to learn Maven, Jenkins and other essential tools for Java developers then I also suggest you check out this list of 10 free Courses to learn Maven, Jenkins, and Docker for Java Developers on Medium.