Tuesday, 7 January 2014

Configure maven using command prompt



Maven is project management and comprehension tool. It provides to developer to complete project building life cycle management. We can setup the maven project in very short time.

There is no need to mention each and every detail in configuration file; Maven has default behavior for projects. When we creates a maven project so it creates default project structure where we have to placed files according and there is no need to define in maven configuration file pom.xml.

Default Directory Structure

Source Code:  ${baseDir}/src/main/java
Resources: ${baseDir}/src/main/ resources
Tests: ${baseDir}/target/classes
Compiled Byte Code: ${baseDir}/src/test
Distributable JAR: ${baseDir}/target

Install Maven
Download latest apache-maven and unzip the file and maven is java based tool, it should be installed on your environment and JDK should be 1.5 or above.

Set the Environment Variable:

MAVEN_HOME: YOUR_PATH\apache-maven-3.1.0
PATH: %MAVEN_HOME%\bin;

Run the below command from command Prompt to check maven installation
mvn –version 

It should show the maven version along with java home, java version etc. as mentioned below



Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 07:45:32+0530)

Maven home: C:\personal\software\apache-maven-3.1.0\bin\..

Java version: 1.7.0_60-ea, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_60\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"
  



Create Project Using Maven

Run the below script from command prompt

mvn archetype:generate -DgroupId=com.jigarnagar.app -DartifactId=mysample-calculate -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
 

It will take time while running first maven project on your system because maven is going to downloading most recent artifact into your local repository.


After getting a build successful message, you will notice that generate goal created a directory with the same name given as the artifactId.



Import a project using Eclipse:

Right click on project explore – import – existing maven project – select – Next
Browse the project from ROOT directory and Next, Finish


 

 


It creates a maven project and project structure will look like that


 
Maven Pom.xml

Project Object Modal – it is core file of maven project configuration which contains majority of information require building the project in just a way as we want.

It is XML file and always resides in base directory of the project.

<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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.jigarnagar.app</groupId>
  <artifactId>mysample-calculate</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mysample-calculate</name>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

</project>

 
POM also contains goals and plugin. All pom files require the project element and three mandatory fields: group id, artifact id and version. Maven local repository project notation will be group id:artifact id:version.




Group id
Project’s group id
It is unique
example: com.jigarnagar.actions
Artifact id
Project id
Generally Name of the project
Version
Project version
It is used to separate version with repository

 Maven Build Life Cycle

It defines the order in which goals are to be executed. Maven build life cycle consists of following sequence of phases (phase represent a stage in life cycle).


Prepare- resource
Copy resource
Compile
Compile source code
Package
bundle compile source code into JAR/WAR
As defined in pom.xml file
Install
Install the package into local or remote repository


Default (Build) life cycle




Life cycle phase
Description
Validate
Validate project, all information available to complete the build process
Initialize
Initialize the build stage
Generate-sources
Generate any source code to be included in compilation stage
Process- sources
Process source code, example filter any value
Generate-resources
Generate resource to be included into package
Process-resources
Copy & process resource in destination directory
ready for package phase
Compile
Compile source code
Process-classes
Post process the generated files from compilation
Package
bundle compile source code into JAR/WAR
As defined in pom.xml file
Install
Install the package into local or remote repository

No comments:

Post a Comment