Friday, August 8, 2014

Maven Introduction

Maven is a build management tool. Maven works with a declarative paradigm. Maven works with the notion of build lifecycle and a build lifecycle is made up of multiple phases. Before we delve further, let's see how to install Maven. Please follow the steps to do that:

Installing Maven

  • Make sure you have java installed. Maven is build using Java.
  • Download Maven . Please download the binary zip.
  • Extract the zip at a suitable location. Let's say you have extracted it at "D:\programs\apache-maven"
  • Add the path to PATH variable of windows. In this case, append to PATH variable "D:\programs\apache-mave\bin"
  • Open a Command prompt and if Maven is successfully installed, you should be able to run mvn command. 
           mvn -version
     
     Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 
          2014-06-17T19:21:42+05:30)
     Maven home: D:\programs\apache-maven-3.2.2\bin\..
     Java version: 1.8.0_05, vendor: Oracle Corporation
     Java home: C:\Program Files\Java\jdk1.8.0_05\jre
     Default locale: en_US, platform encoding: Cp1252
     OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
  • Another thing to take care of is repository location. Maven downloads project dependencies and keeps them at repository location. For that go to apache-maven\conf directory. Open settings.xml and search for localRepository. By default Maven keeps the dependencies in <user.home>/.m2/repository but you can change it to some other location. Maven might land up downloading a huge amount of dependencies based on the kind of projects you are working so it's a good idea to plan for atleast couple of GB space.
Making First Project

Maven comes with many archetypes. Archetypes can be though of a template to make a certain kind of project. For example the default archetype will create a java based project. To make a default project run the following command on and empty directory

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.lalit.app -DartifactId=maven-app

If the command is successful, you should see a project created inside maven-app directory. The structure of the project will be as follows:

Note that it has got a java directory and a test directory. Inside Java you will find the package structure as com.lalit.app and in it a Java file calle App.java

App.java will be our age old famous Hello World program

package com.lalit.app;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

The other important file inside maven-app directory is pom.xml, which looks as follows:
<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.lalit.app</groupId>
  <artifactId>maven-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

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

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

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

Note the groupId, artifactId as we have mentioned in maven command while building the project. The project has just unit case dependency as for the program to run it does not needs any third party jar. 

Run the program

To run the program run the following command

mvn compile  exec:java -Dexec.mainClass="com.lalit.app.App"

compile will compile the code and exec:java will execute the main class. If you have to pass some arguments than pass them as -Dexec.args="args0 args1"

Also you will notice that the output of the maven is not a simple Hello World but a bunch of things are printed on the console. 

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-app
---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Lalit\temp\maven-app\src\mai
n\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ maven-app ---

[INFO] Compiling 1 source file to C:\Users\Lalit\temp\maven-app\target\classes
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ maven-app ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment
on MEXEC-6.
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.597 s
[INFO] Finished at: 2014-08-08T17:18:31+05:30
[INFO] Final Memory: 14M/186M

I have highlighted the "Hello World" string in that. Maven executes the commands in the context of a build life-cycle and their are multiple phases to it. You can add more instructions in each phase.

Importing the Project in eclipse

For importing project in eclipse issue the following command

mvn eclipse:eclipse

This will create .project and .classpath file and the project can be imported in eclipse.


No comments:

Post a Comment