Saturday 26 September 2015

Setting up Maven in windows.


From this post we would be learning how to set up maven on windows and creating a project and running a build. I shall explain in steps and link a video in the end showing the entire process.

1. First let us go and download maven. You can download maven from the following link from apache software foundation. I would prefer downloading zip archive.

Download maven from here


2. I have downloaded maven to my softwares folder and I have extracted it in the same folder.



3. Once the extraction is complete we need to set maven in windows environment variable so that it allows command line tools and other tools to interact with maven.

4. Steps to set up environment variable in windows.

a. Click on search icon and type in environment variables and click on edit the system environment variables.

b. Click on environment variables from the dialog that pops up. Create a new system variable and call it MVN_HOME and point to the folder where you have extracted maven.
         
 MVN_HOME=F:\softwares\apache-maven-3.2.1-bin\apache-maven-3.2.1

c. Click on the path variable and click edit to add MVN_HOME in the following format.
         
 %MVN_HOME%\bin

d. Click OK to exit.

5. To test if we can access
maven from command line open command prompt and type in the following.

mvn --version
6. Let us now create a new maven project. use the following command to createa simple java project in maven.
mvn archetype:create -DgroupId=com.karthik.learning -DartifactId=FirstMavenProject
here group ID is the package for the project and artifact id is the project name.

7. Let us go to the folder where the project is created and maven would have created a folder with the name of the artifact ID and the java package same as the group ID for both main and test folders. Also let us take a look at the file called pom.xml which maven generated for us. It has the information that we used to create the maven project and also it has a dependency to go get Junit jar from maven central repository.
POM.XML

<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.karthik.learning</groupId>
  <artifactId>FirstMavenProject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>FirstMavenProject</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>
8. Let us write some code and try to run a maven build. I am writing a simple java program which adds two numbers and returns the results and I will be writing Junit for the same and I shall run the maven build

Main program:

package com.karthik.learning;

/**
 * <p>
 * Class to add numbers
 * </p>
 * 
 * @author Karthik Arun
 */
public class App {

 /**
  * <p>
  * Method to add two numbers
  * </p>
  * 
  * @param a
  * @param b
  * @return
  */
 public int add(int a, int b) {
  return a + b;
 }
}
Unit test program:

package com.karthik.learning;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * <p>
 * Unit test for simple App.
 * </p>
 * 
 * @author Karthik Arun
 */
public class AppTest extends TestCase {
 /**
  * Create the test case
  * 
  * @param testName
  *            name of the test case
  */
 public AppTest(String testName) {
  super(testName);
 }

 /**
  * @return the suite of tests being tested
  */
 public static Test suite() {
  return new TestSuite(AppTest.class);
 }

 /**
  * <p>
  * Method to test add method
  * </p>
  */
 public void testApp() {
  App app = new App();
  assertEquals(5, app.add(3, 2));
 }
}
9. To run the build open command prompt and go to the folder where maven project is present and run mvn install



Video for this tutorial is available below.



3 comments: