How to Install Maven and Setup Maven in Jenkins

What is Apache Maven

A maven is a build tool used for Java projects. Maven can also be used to build and manage projects written in various languages such as C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

The most powerful feature of Maven is download project dependency libraries automatically defined in the pom.xml. Also, configure project build cycle such as invoke junits for test sonarqube for static analysis.

Prerequisites

Java Development Kit (JDK) and EclipseMaven 3.3+ require JDK 1.7 or above to execute
MemoryNo minimum requirement
DiskApproximately 10MB is required for the Maven installation itself. In addition to that, additional disk space will be used for your local Maven repository. The size of your local repository will vary depending on usage but expect at least 500MB.
Operating System

No minimum requirement. Start-up scripts are included as shell scripts and Windows batch files.

Install Apache Maven

  • Next unzip the file that you just downloaded on your machine.
  • Next, set the enviornmental variables for Apache Maven by going in enviormental variable and add variable name as M2_HOME and MAVEN_HOME
  • Next append the Maven bin directory in the PATH variable.
  • Next, check the Maven version
  • Next, configure the local maven repository by going in the conf folder and then inside the setting.xml file and update the path after creating a maven_repo folder as shown below.

Setup Maven Project

  • First step is to open eclipse and navigate to new project and then look for Maven as shown below.
  • Select Create a simple project
  • Provide the group ID, artifact ID as shwon below.
  • It will create the new folder on your eclipse as shown below.
  • Next, create a Java class and name it something as my-calculator
  • Create a new class mycalculator
  • After clicking on Finish you will as below.
  • Next, add the methods in the class.
package mycalculator_package;

public class mycalculator {

// Method to add two numbers
 public int add(int a, int b) {
  return a + b;	
}   	
// Methods to multiple two numbers
 public int multiple(int a, int b) {
	  return a * b;	
	}  	
// Methods to subtract two numbers
 public int subtract(int a, int b) {
	  return a - b;	
	}  	
// Methods to divide two numbers
 public int divide(int a, int b) {
	  return a % b;	
	}  	
}
  • Similarly create a junit by creating a test class
  • Next, we dont want Junit4 to be added.
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
</dependency>
</dependencies>  
  • Now, add the code below in the mycalculatortest.java as shown below.
package mycalculatortest_package;
import static org.junit.Assert.*;
import org.junit.Test;
import mycalculator_package.mycalculator;
public class mycalculatortest {

	@Test
	public void addtest() {
		mycalculator calc = new mycalculator();
		assertEquals(100, calc.add(80, 20));
	}
	@Test
	public void subtracttest() {
		mycalculator calc = new mycalculator();
		assertEquals(60, calc.subtract(80, 20));
	}
	@Test
	public void multipletest() {
		mycalculator calc = new mycalculator();
		assertEquals(100, calc.multiple(10, 10));
	}

}
  • Next add the maven compiler version in the POM file as we are using JDK 1.8 and then go to Project and Maven and update Project as shown below.
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>  
  • Next Run the Maven Project as shown below by navigating to Run as and then Maven Build. All your Java source code remains in the src folder and all the classes are pressent in the target folder.
Execution Process of Java Program in Detail | Working of JUST-IT-TIME  Compiler (JIT) in Detail - Simple Snippets
  • Further run mvn clean and mvn clean test: Cleans the project and removes all files generated by the previous build and the mvn clean test cleans the target folder as well.
  • Next test locally on your machine some of the other commands such as mvn compile that Compiles source code of the project.
  • mvn test-compile: Compiles the test source code.
  • mvn test: Runs tests for the project.
  • mvn package: Creates JAR or WAR file for the project to convert it into a distributable format.
  • mvn install: Deploys the packaged JAR/ WAR file to the local repository.
  • mvn deploy: Copies the packaged JAR/ WAR file to the remote repository after compiling, running tests and building the project.

Setting up Maven in Jenkins WAY 1

  • Create a new Job in Jenkins and call it Maven-JOB

  • Select top level Maven targets in the Build Step
  • Next add clean test package in the Goals and the location of pom.xml file.
  • Next, trigger the Jenkins Job and should see the project compiled succesfully.

Setting up Maven in Jenkins WAY 2

  • Install Maven Plugin using Manage Jenkins
  • Now create another job where you will notice the Maven Project option as shown below. Select Maven Project and enter an item name as maven-job2
  • Inside the Build TAB add the workspace path which is the location where you pom file is located.
  • Make sure to add: Resolve Dependencies during Pom parsing in the Build step
  • Next navigate to Manage Jenkins to Global Tool Configuration and add Maven and JDK as shown below.
  • Next run the Jenkins Job and you should then be able to run the Job.
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s