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 :
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 :
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=my–app –DarchetypeArtifactId=maven–archetype–quickstart –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 :
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 :
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 :
Frequently used Maven archetypes
Empty Maven Project
Empty Maven Web application
Create Jersey Restful Service
Add this dependency if you need JSON support :
Create Jersey Rest Client
Create Empty Maven project and add following dependency :
Create Empty Maven project and add following dependency for hibernate-core:
You will also need to provide the dependency for db driver.
Here is the dependency for MySQL :
Spring MVC Web Application
© 2015 – 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post