What is Maven?
Apache Maven is project management tool which is following the concept of a project object model (POM). Mavan can manage project’s build and documentation from a central place. Maven 1 actually was started as a sub project of Apache Turbine in 2002 (by Sonatype’s Jason van Zyl). It was released in July 2004 as v1.0. Maven 2 was released in Oct 2005. It was a complete rewrite of the previous project. It was not backward compatible.
Maven 3 was released in October 2010. It is same as Maven 2 but more stable. This article explores the very basic concepts needed for the beginners who want to learn and get started with the Maven. Before the popularity of using Maven, most of the projects used Ant Script for the projects build which is used only for the build purpose.
If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on Java topics, please subscribe here.
Definition of Maven from the Maven site(http://maven.apache.org/):
“Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
Maven is an open source software engineering platform. The intent of Maven is to make intra-project development highly manageable in the hopes of providing more time for cross-project development. You may say it as sharing of project development knowledge. The core functionality of Maven is automated project building, distribution and website creation.
- Dependency System: Superior dependency management including automatic updating, dependency closures (also known as transitive dependencies). Maven encourages the use of a central repository of JARs and other dependencies. Maven has a mechanism which helps your project’s clients to download any required JARs for building your project from a central JAR repository.This allows reuse of JARs across projects for Maven users.This also helps in dealing with backward compatibility issues.
- Multi-module builds: Able to easily work with multiple projects at the same time
- Consistent project structure: Consistent structure across all projects means no ramp up time for new developers coming onto a project
- Consistent build model: Maven is able to build any number of projects into predefined output types such as a JAR, WAR, or distribution based on metadata about the project, without the need to do any scripting in most cases.
- Plugin oriented: The Maven functionality is implemented in terms of plugins.
- Project generated sites: Maven generates a website or a PDF file using the same metadata as for the build process.You can include any documentation to this site and the standard report about the state of development of the project.Examples of this information can be seen at the bottom of the left-hand navigation of this site under the “Project Information” and “Project Reports” sub menus.
Comparison of Maven with Apache ANT:
Major differences between Maven and ANT are:
- Maven is description of a project whereas ANT is development of a build script per project.
- Maven follows conventions like a common project directory structure whereas ANT needs to be told about the exactly where to find the source and where to put the output.
- Maven has invocation of defined goals (targets) whereas in ANT script you have invocation of project specific targets.
- Maven has build life cycle, standard project layout where as ANT doesn’t and it has too complex scripts(You have to define goals and goal dependencies). You have to attach a sequence of tasks to each goal manually.
- Maven has reusable plugins, repositories whereas ANT scripts are not reusable.
- Maven is declarative.All you have to do is create a pom.xml file and put your source in the default directory. Maven takes care of the rest.ANT is procdeural which means you need to tell it to compile,copy and then compress.
POM stands for Project Object Model.It describes following data about a project:
- Metadata: Location of Directories, Developers/Contributors, Dependencies, Repositories
- Dependencies (Transitive Dependencies), Inheritance, and Aggregation
- Key Elements
- Model Version
- Group ID
- Artifact ID
- Profiles (Alternate build configurations)
(Note:Transitive Dependencies definition- A dependency that should be included when declaring project itself is a dependency.Example: ProjectA depends on ProjectB. If ProjectC depends on ProjectA then ProjectB is automatically included. Only compile and run time scopes are transitive)
POM has four categories
- General project information-this includes project information like name,URL for project,sponsor organization,list of developers,license for the project.
- Build settings-This include customization of default Maven build behaviour.
- Build environment-This inlcudes profiles that can be used and activated for working in different environments(example development server,production server).
- POM relationships-This includes inheritance of POM settings of parent projects.
- Download the latest Maven from http://maven.apache.org/download.cgi.
- The installed file is:apache-maven-3.0-bin.zip
- Extract the archive to the directory you wish to install Maven.We have extracted to the following folder:
- Test Maven
In order to test Maven setup,you need Java installed/configured on your machine.Assuming you have JDK installed at C:\Program Files\Java\jdk1.7.0_11\bin, execute the following commands which sets your System Environment with Java and Maven paths.
set MAVEN_HOME=C:\tools\apache-maven-3.0 set MAVEN_OPTS=-Xms256m -Xmx512m set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11 set path=%path%,%JAVA_HOME%\bin;%MAVEN_HOME%\bin
- Verify Maven installation
Open the command prompt and execute the following command:
c:\> mvn --version
This should give the current installed version of Maven.
Apache Maven 3.0 (r1004208; 2010-10-04 17:20:56+0530) Java version: 1.7.0_11 Java home: C:\Program Files\Java\jdk1.7.0_11\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
Basic User specific Maven Configuration
Now that you have installed Maven, it is a good idea to setup the .m2 directory, where Maven creates its local repository. To create .m2 directory, assuming “Manisha” is logged in user, open the command console and execute the following command, to create the .m2 directory.
mkdir C:\Documents and Settings\Manisha\.m2
The settings.xml file(under the .m2 directory) contains user-specific configuration for authentication, repositories,and other information to customize the behavior of Maven. Sample settings file is:
<settings> <servers> <server> <id>server1</id> <username>user1</username> <password>password1</password> </server> <server> <id>server2</id> <username>user2</username> <password>password2</password> </server> <!-- Server settings for repositories Ends --> </servers> <!-- Mirror settings for repositories Starts --> <mirrors> <mirror> <id>mirror1</id> <name>Internal network access to any repository</name> <mirrorOf>repo1</mirrorOf> <url>https://www.javabeat.net/repo1/</url> </mirror> </mirrors> <!-- Mirror settings for repositories Ends --> <!-- Profiles settings for repositories Starts --> <profiles> <profile> <id>profile1</id> <activation> <activeByDefault>true</activeByDefault> </activation> <repositories> <repository> <id>repo1</id> <url>https://www.javabeat.net/repo1/</url> </repository> <pluginRepositories> <pluginRepository> <id>pluginrepo1</id> <name>Repos (public/proxied)</name> <url>https://www.javabeat.net/pluginrepo1/</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </pluginRepository> </profile> </profiles> <!-- Profiles settings for repositories Ends --> </settings>
In this article we saw basic overview about Apache Maven, its installation and basic configuration.The next article would deal with creating a simple Java project using Maven. I would write the series of tutorials on Apache Maven and how to use Apache Maven in your projects. If you have any questions, please post it in the comments section. If you are interested in receiving the future articles on Java topics, please subscribe here.