Monday, July 27, 2015

Maven

What is Maven?
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.

Why Maven?
  • Its easy and fast
     
    No need to worry about packaging all the libraries used in the system, but just mention those in the pom file (pom.xml, will be discussed later) under dependencies.

  • Well structured

    All the libraries used in the project can be understood by looking at the pom file.
  • Provide guidelines for best practices
    i)   Keeping your test source code in a separate, but parallel source tree
      
    ii)  Using test case naming conventions to locate and execute tests 

    iii) Have test cases setup their environment and don’t rely on customizing   the build for test preparation.
The POM

The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of information required to build a project in just the way you want.


Basic pom.xml file is shown below :


<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.hsenid.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<name>my-app</name>
<url>http://maven.apache.org</url>

<dependencies>

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

</dependencies>
</project>

Installation


1. Download the maven latest version from the link below

https://maven.apache.org/download.cgi

2. Then unpack the compressed directory and place it any where as you desired.

3. Then edit the profile to change environmental variables as you did when installing jdk


JAVA_HOME=/home/shashika/install/Java7/jdk1.7.0_45
#JAVA_HOME=/home/shashika/install/Java8/jdk1.8.0_31
M2_HOME=/home/shashika/install/apache-maven-3.3.3
PATH=/sbin:$HOME/bin:$JAVA_HOME/bin:$M2_HOME/bin:${PATH}
export JAVA_HOME
export M2_HOME
export PATH

4. Now type “mvn -version” on terminal to check whether maven is properly installed


Creating a Project

Step 1

 >>mvn archetype:generate
-DgroupID=com.hsenid.app
-DartifactID=my-app
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

archetype:generate

Generates a new project from an archetype, or updates the actual project if using a partial archetype. If the project is fully generated, it is generated in a directory corresponding to its artifactId. If the project is updated with a partial archetype, it is done in the current directory.

groupId

This is generally unique amongst an organization or a project. For example, all core Maven artifacts do (well, should) live under the groupId org.apache.maven. Group ID's do not necessarily use the dot notation, for example, the junit project. Note that the dot-notated groupId does not have to correspond to the package structure that the project contains. It is, however, a good practice to follow. When stored within a repository, the group acts much like the Java packaging structure does in an operating system. The dots are replaced by OS specific directory separators (such as '/' in Unix) which becomes a relative directory structure from the base repository. In the example given, the org.codehaus.mojo group lives within the directory $M2_REPO/org/codehaus/mojo.

artifactId

The artifactId is generally the name that the project is known by. Although the groupId is important, people within the group will rarely mention the groupId in discussion (they are often all be the same ID, such as the Codehaus Mojo project groupId: org.codehaus.mojo). It, along with the groupId, create a key that separates this project from every other project in the world (at least, it should :) ). Along with the groupId, the artifactId fully defines the artifact's living quarters within the repository. In the case of the above project, my-project lives in $M2_REPO/org/codehaus/mojo/my-project.

version

This is the last piece of the naming puzzle. groupId:artifactId denote a single project but they cannot delineate which incarnation of that project we are talking about. Do we want the junit:junit of today (version 4), or of four years ago (version 2)? In short: code changes, those changes should be versioned, and this element keeps those versions in line. It is also used within an artifact's repository to separate versions from each other. my-project version 1.0 files live in the directory structure $M2_REPO/org/codehaus/mojo/my-project/1.0.

Step 2
generate goal will create  a directory with the same name given as the artifactId. Change the path into that directory.

shell>> cd my-app

You can check the standard project structure under this directory.

Step 3

Build the project

shell>>mvn package 

Step 4

Run the project
shell>> java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

  
Create Web Project from Maven Template
 
>>mvn archetype:create
  -DgroupId=<your project's group id>
  -DartifactId=<your project's artifact id>
  -DarchetypeArtifactId=maven-archetype-webapp




No comments:

Post a Comment