Tuesday, September 19, 2023

9 Maven Concepts and Tools Every Java Developers Should Know

The 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 the 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 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 first go through a comprehensive Maven course like the Apache Maven: Beginner to Guru course 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 files in the 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 the Maven and the ANT tool.

9 Maven concepts for Java developers



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 like https://repo1.maven.org/, which contains JAR files for almost every possible Java library. This is also knowns as Maven Public repository.

Btw, if you are first time hearing about the 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 the 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 the version, this is a seriously good feature to save a lot of time which goes to set up a 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 the local repository and connection detail to connect to the global maven repository. Please follow the steps given in this guide to install Maven on a Windows machine.



5. M2Eclipse Plugin

If you are using Eclipse IDE for Java development then you can also use the 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 the M2Elcipse plugin, just right click and choose the Maven Install or Maven Build option as shown 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 the 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 the JAR file and can be used to upload JAR in the internal maven repository.

The pom.xml also contains Maven plugins used in a project like the JAR plug-in which is responsible for creating a JAR file from class files and resources. pom.xml also contains all dependencies 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 the 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 Guru book 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, the mvn install command runs both Maven clean and Maven builds together. The Maven clean will clean old build artifacts like 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 a 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.


Other Maven and Java articles you may like
  • 21 Tech Skills Java Developer can learn (skills)
  • 10 Essential Tools Every Java Developer should Learn (tools)
  • 10 Best Spring Framework Courses for Beginners (courses)
  • How to install Maven in Windows 10? (steps)
  • How to fix the Maven Eclipse Dependency search not working issue? (solution)
  • How to increase the heap size of Maven? (steps)
  • Top 10 Maven Plugin Every Java developer should know (list)
  • 5 Best Apache Kafka Courses for Beginners (online courses)
  • How to create or modify build.xml in ANT? (tutorial)
  • 3 tips to use Maven in Eclipse for Java development (tips)
  • How to build a Java project using ANT? (article)
  • What is the difference between Maven, ANT, and Jenkins? (answer)
  • Top 5 Apache Maven Free Books for Java developers (books)
  • Top 6 Online Courses to Learn Apache Maven (courses)
  • 10 Things Every Java developers should learn (skills)
  • Maven vs Gradle for Beginner Java Developers (article)

Thanks for reading this article so far, if you like these Essential Maven concepts for Java developers then please share it with your friends and colleagues. If you have any questions or suggestions 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.

And lastly one question for you? What is your favorite build tool in Java? Maven or Gradle? 

7 comments:

  1. These aren't tips.

    ReplyDelete
  2. where are the tips ?

    ReplyDelete
  3. @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.

    ReplyDelete
  4. @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.

    ReplyDelete
  5. 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.

    ReplyDelete
  6. How early would you recommend a Java noobie pick up Maven? I just started learning the language and have started work on my first (very basic) app, but since I'm just starting out with basic Java principles, I've been keeping it simple. I have already added Git to my beginner's toolbox since the cloud storage is handy, but I'm not sure if I'm ready to dive into Maven just yet.

    ReplyDelete
  7. for everyone's benefit A couple of things which I noticed after spending a couple of hours fixing maven issues
    - maven by default uses settings.xml from users home directory ~/.m2/settings.xml
    - then there is a global settings.xml which is in MAVEN_HOME/conf/settings.xml

    you should always check those two files if you are facing issue with downloading or not finding artifacts etc

    Another thing which I learned is that when you run mvn install, it puts the artifacts into local repository, I mean ~/.m2/repository and next time if any other module or projects needs it uses from there. Only if it doesn't find here then it goes to remote repository like nexus or artifactory.

    one last thing, if you are using multi module maven projects then make sure that every child pom.xml have parents reference and parent pom.xml have entry of all the module in the order you want to build.

    ReplyDelete