Maven Basics

Maven

 

Maven is a build automation tool used primarily for Java projects

 

It can help in reducing development time by organizing the code and also downloading the required dependencies for your project.

 

Maven download and setup

 

To get started with maven, download it from maven installation path.

 

Maven requires JAVA_HOME variable to be set before you can start using it.

 

In windows environment.. assuming java is installed in c:\java\jdk1.7.0_75, you can set it as follows :

set JAVA_HOME=c:\java\jdk1.7.0_75;

 

Also, add the maven installation path in the “path” variable. This would help accessing it from folders other than the maven installation path.

 

If your maven installation is in c:\apache-maven-3.3.9, set it on windows command prompt using :

set PATH=%PATH%;c:\apache-maven-3.3.9\bin;

 

These settings can also be set in Environment variables .

 

Now, on the command prompt, run mvn --version to verify that it is correctly installed.

 

If, it works fine, you can start using Maven.

 

Creating and Building project in Maven

 

You can create a project using below command:

 

mvn archetype:generate DgroupId=com.mycompany.app DartifactId=myapp DarchetypeArtifactId=mavenarchetypequickstart DinteractiveMode=false

 

Here archetype specifies the goal you want to achieve. For example, the goal of above command was to create a simple project structure.

 

This command will create the structure for your project. If you go inside the project, you will find a pom.xml file that contains the dependencies for your project. This file is essential for Maven to be able for managing the phases of your project.

 

For example, you can build your project by going to the project home (where pom.xml is present) and using phase command :

 

mvn package

 

Here are some of the other commonly used phases :

 

  • validate: validate the project is correct and all necessary information is available

 

  • compile: compile the source code of the project

 

  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed

 

  • package: take the compiled code and package it in its distributable format, such as a JAR.

 

  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run

 

  • verify: run any checks to verify the package is valid and meets quality criteria

 

  • install: install the package into the local repository, for use as a dependency in other projects locally

 

  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

 

 

You can use different archetype catalogs to generate the project structure with required dependencies by using the corresponding goals.

 

For example, if you want a struts project, use below :

 

mvn archetype:generate -DarchetypeCatalog=http://struts.apache.org/

This would start the project in interactive mode and provides you options to choose among different archetypes to generate corresponding structure.Similar approach could be used for creating new projects for Spring, Hibernate etc.

If you already have a project setup and you want to add new dependencies, you can achieve by adding them in the pom.xml

i.e. if you want to add hibernate jars to you Struts 2 project you created, add the dependency as follows :

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-core</artifactId>

<version>4.3.10.Final</version>

</dependency>

 

Frequently used Maven archetypes

 

Empty Maven Project

 

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.topjavatutorial.app -DartifactId=App -DinteractiveMode=false

 

Empty Maven Web application

 

mvn archetype:generate -DarchetypeArtifactId=maven‐archetype‐webapp -DgroupId=com.topjavatutorial.webapp -DartifactId=App -DinteractiveMode=false

 

Create Jersey Restful Service

 

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.topjavatutorial -DartifactId=jersey-webapp -Dpackage=com.topjavatutorial -DarchetypeVersion=2.22.1

 

Add this dependency if you need JSON support :

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>

 

Create Jersey Rest Client

 

Create Empty Maven project and add following dependency :

 

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
 

Hibernate project

 

Create Empty Maven project and add following dependency for hibernate-core:

 

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
</dependency>

 

You will also need to provide the dependency for db driver.

 

Here is the dependency for MySQL :

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

 

Spring Application

 

Create an empty Maven project.

 

Add following spring-context dependency in the pom xml

 

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>

 

Spring MVC Web Application

 

Create Empty Maven web app and then add following dependencies :

 

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.5.RELEASE</version>
</dependency>

© 2015 – 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

7 comments

  1. […] the following article if you need help with Maven or importing the project.   Maven Basics […]

  2. […] you need help setting up Maven, refer this article :   Maven basics   Let’s create a standalone java application by using the following archetype in command […]

  3. […] on maven installation or understanding, the following post might be a good starting point:   Maven Basics […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: