Thursday, 22 August 2013

Maven Basics Tutorial

Maven


Maven is an automation and management tool developed by Apache Software Foundation. It was initially released on 13 July 2004.It is used for projects build, dependency and documentation. It simplifies the build process like ANT. In short terms we can tell maven is a tool that can beused for building and managing any Java-based project.



Objective :

The primary goal of Maven is to provide developer with the following :
  • A comprehensive model for projects, which is reusable, maintainable, and easier to comprehend.
  • Plugins or tools that interact with this declarative model.


There are cases when you want to run some specific test cases as many test cases are failing and breaking your build and you just want to check your test case , So you can do this by a simple maven command.




Maven POM :

POM stands for Project Object Model. It is fundamental unit of work in Maven. It is an XML file that resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM :
<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.home.practice-project</groupId>
   <artifactId>my-project</artifactId>
   <version>1.0</version>
 <!-- dependencies for application -->
       <dependencies> 
               <dependency> 
                       <groupId>org.apache.logging.log4j</groupId> 
                       <artifactId>log4j-api</artifactId> 
                       <version>2.11.0</version> 
                 </dependency> 
       </dependencies> 

</project>


Elements used in pom.xml file and their explanation :

project -

It is the root elment of the pom.xml file.

modelVersion 


modelversion means what version of the POM model you are using. Use version 4.0.0 for maven 2 and maven 3.

groupId-


groupId means the id for the project group. It is unique and Most often you will use a group ID which is similar to the root Java package name of the project like we used the groupId com.home.practice-project

artifactId


artifactId used to give name of the project you are building. in our example name of our project is My-project.

version-


version element contains the version number of the project. If your project has been released in different versions then it is useful to give version of your project.

dependencies

dependencies element is used to defines a list of dependency of project.

dependency


dependency defines a dependency and used inside dependencies tag. Each dependency is described by its groupId, artifactId and version.

name


this element is used to give name to our maven project.

scope


this element used to define scope for this maven project that can be compile, runtime, test, provided system etc.

packaging

packaging element is used to packaging our project to output types like JAR, WAR etc.



Maven commands :



1   .  mvn package

create package



2   . mvn clean dependency:copy-dependencies package

The assembly plugin can then be used to package the whole appassembler directory to a zip.


3   . mvn clean

Clean target folder


4   . mvn install 

When you do a Mvn install, it will roughly
  • Generate whatever it needs,
  • Compile the sources,
  • Copy other resources,
  • Create the artifact for your project,
  • Run unit tests,
  • Copy the artifact to the local Mvn repository (this is usually $HOME/.m2/repository).

So a Mvn clean install will first clean the target and then run the steps above.


5   . mvn clean install

Clean target folder and Installs it to the local Maven repository.

6   . mvn compile

compile your source code for errors

7   . mvn test

run your test cases


8   . mvn -Dtest=TestClass#abcMethod test

Run specific test case of the class 

TestClass is the test class name 

abcMethod is the test method


9   . mvn clean install -DskipTests=true -Dmaven.test.failure.ignore=true

Skip all test cases.




No comments:

Post a Comment

Spring boot with CORS

CORS (Cross-Origin Resource Sharing) errors occur when a web application running in a browser requests a resource from a different domain or...