Benefits of Maven 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 deliverable 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.

As Maven uses convention over configuration which means lots of sensible default values.

2) Dependency Management
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.

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 this guide 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:

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

9) Target Directory
When you build the project using Maven it creates class files and copies resource 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 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 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.

 

Related blog:

Java training institute in chennai