Skip to main content

Maven Basics Tutorial

Maven


Maven is an automation and management tool developed by Apache Software Foundation. It was initially released on 13 July 2004.It is used for projects build, dependency and documentation. It simplifies the build process like ANT. In short terms we can tell maven is a tool that can beused for building and managing any Java-based project.



Objective :

The primary goal of Maven is to provide developer with the following :
  • A comprehensive model for projects, which is reusable, maintainable, and easier to comprehend.
  • Plugins or tools that interact with this declarative model.


There are cases when you want to run some specific test cases as many test cases are failing and breaking your build and you just want to check your test case , So you can do this by a simple maven command.




Maven POM :

POM stands for Project Object Model. It is fundamental unit of work in Maven. It is an XML file that resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM :
<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.home.practice-project</groupId>
   <artifactId>my-project</artifactId>
   <version>1.0</version>
 <!-- dependencies for application -->
       <dependencies> 
               <dependency> 
                       <groupId>org.apache.logging.log4j</groupId> 
                       <artifactId>log4j-api</artifactId> 
                       <version>2.11.0</version> 
                 </dependency> 
       </dependencies> 

</project>


Elements used in pom.xml file and their explanation :

project -

It is the root elment of the pom.xml file.

modelVersion 


modelversion means what version of the POM model you are using. Use version 4.0.0 for maven 2 and maven 3.

groupId-


groupId means the id for the project group. It is unique and Most often you will use a group ID which is similar to the root Java package name of the project like we used the groupId com.home.practice-project

artifactId


artifactId used to give name of the project you are building. in our example name of our project is My-project.

version-


version element contains the version number of the project. If your project has been released in different versions then it is useful to give version of your project.

dependencies

dependencies element is used to defines a list of dependency of project.

dependency


dependency defines a dependency and used inside dependencies tag. Each dependency is described by its groupId, artifactId and version.

name


this element is used to give name to our maven project.

scope


this element used to define scope for this maven project that can be compile, runtime, test, provided system etc.

packaging

packaging element is used to packaging our project to output types like JAR, WAR etc.



Maven commands :



1   .  mvn package

create package



2   . mvn clean dependency:copy-dependencies package

The assembly plugin can then be used to package the whole appassembler directory to a zip.


3   . mvn clean

Clean target folder


4   . mvn install 

When you do a Mvn install, it will roughly
  • Generate whatever it needs,
  • Compile the sources,
  • Copy other resources,
  • Create the artifact for your project,
  • Run unit tests,
  • Copy the artifact to the local Mvn repository (this is usually $HOME/.m2/repository).

So a Mvn clean install will first clean the target and then run the steps above.


5   . mvn clean install

Clean target folder and Installs it to the local Maven repository.

6   . mvn compile

compile your source code for errors

7   . mvn test

run your test cases


8   . mvn -Dtest=TestClass#abcMethod test

Run specific test case of the class 

TestClass is the test class name 

abcMethod is the test method


9   . mvn clean install -DskipTests=true -Dmaven.test.failure.ignore=true

Skip all test cases.




Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...