Apache Maven Tutorials, Articles and Dependencies release
Apache Maven is a very popular tool for managing the Java projects and building the distributable packages of your project. Maven is a complete project management tool for Java developers which allows developers to compile, test, build and release the Java projects in much easier way. In this section we are going to learn Apache Maven with the help of many tutorials and examples. We will also provide you sample projects to get started with the Maven build tool.
Java developers use Maven for managing their Java projects, but Maven is not limited just to Java it can be used with projects written in C#, Ruby, Scala and others. In this section we will mostly discuss the use of Maven in Java projects.
Maven handles the two important aspects of software development: software building process and managing the dependencies in the software project. Maven uses an XML file called pom.xml for defining the project being built and this file is sufficient to describe all the steps of the software project building. In the pom.xml file developers can define all the dependencies, modules, components, software build order and the plug-ins to use during project building. So, it covers all the aspects of modern software building process.
Maven is powerful and feature rich tool, which comes with the comes with the pre-defined configuration for build-order, directories, many plugins and well established process for building complex projects. Maven build tool automatically downloads the libraries and the plug-ins for your project from the remote Maven 2 Central Repository or from other public repositories. The downloaded libraries are stored on the local repository on the developer's machine. Next time it checks the local repository and if libraries are present in the local repository it uses from there in the project. If the libraries are not present it downloads from Maven 2 Central Repository or other specified repository. This process simplifies the dependency management task and developers can concentrate on the application development.
Maven is building the application on the plugin-based architecture and it can be extended to build the C/C++ projects as well. Maven allows you to build and use the plug-ins to simplify the build process, so it can be extended to make custom build solution for your project.
There are other alternative project build including Gradle and sbt, that do not use the XML. The other project build tools including Gradle, sbt, Apache Ivy and others also uses the Maven dependency from remote Maven repositories. Maven is a very popular project management tool which uses the remote repository for downloading project dependencies and eases the project development lifecycle.
History of Maven
Maven tool was created by Jason van Zyl during the development of Apache Turbine project in 2002. Initially, it was a subproject of Apache Turbine project in 2002. Later on, this Maven was voted and accepted as the top level project at Apache Foundation. The first version of Maven v1.0 was released in July 2004, followed by v2.0 release in October 2005. The version v3.0 of the Apache Maven project was released in October 2010. Now Apache Maven is a top level project at Apache foundation and it is being used by millions of developers around the world for building their projects.
Maven Hello World Project
Now we will see how you can create a simple Hello World Project in maven. It is very easy to compile, build and test your Java project using the Maven build tool. Apache maven uses an XML file for managing the project and its dependencies. The configuration file used in Maven is pom.xml file and we will explain you the details of this file.
Creating first Hello World Project is very easy with maven and you can use the maven-archetype-quickstart archetype for creating the example project quickly. First all install the Maven on your computer and go to the terminal and run following command:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
Above command will ask you some questions and then create a sample project for you.
Here is the sample of pom.xml file created with the maven archetype generator:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>hello-world</artifactId>
<version>1.0</version>
<name>hello-world</name>
<url>https://www.roseindia.net</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Maven also generates App.java which is Hello World application in Java. Here is code of App.java:
package com.test;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
Maven follows the pre-defined directory structure and here is the screen shot of the Maven Hello World application:
Maven command to compile and package the application
Compiling and packaging the project is very easy with the maven and during this process maven downloads the dependency required in your project.
To compile the project you can use following command:
mvn compile
Above command will compile your project as shown below:
To package your project you can use following command:
mvn package
Here is the output of the command:
The mvn package command compiles all the files inside your project and then creates jar file which you can use to run your project.
You can download the Maven Hello World project from here:
Download Maven Hello World example project.
In this tutorial we introduced you with the Apache Maven tool and explained you the steps to use it for compiling your first project.