Maven 3.x from the beginning

What is maven

This is first and most important question. Because by name we cannot get any idea about what it is. Reason behind it Maven is Yiddish word meaning accumulator of knowledge. Is was originally started as an attempt to simplify the build processes in the Jakarta Turbine project. But when needs of common build tools requirement increase Maven started to grow as to fill the gap.
Maven’s primary goal is to allow a developer to comprehend the complete state of a development effort in the shortest period of time. In order to attain this goal there are several areas of concern that Maven attempts to deal with:

  • Making the build process easy
  • Providing a uniform build system
  • Providing quality project information
  • Providing guidelines for best practices development
  • Allowing transparent migration to new features

With considering all those we can say maven is open source build tool which provide artifacts as well as resolve dependencies. Also it has capability of version projects and produce java docs and etc.

Why we need Maven

Simple answer is it make life easy.

  • It can download dependency hierarchy
  • Can maintain with local repository. (Download once use any time)
  • Nice integration with ID

Maven Vs Ant

ANT

  • Very procedural tool
  • Build on top of xml and java and it make it to be cross platform
  • It is not a comprehensive build tool
  • We need to define every single step

MAVEN

  • Maven is build tool over scripting tool
  • Lot of build in functionality
  • Consistency across the projects
  • Version control build

Install Maven 3.x

  • Download maven from http://maven.apache.org/download.cgi
  • Setup environment variable
    • JAVA_HOME -> Directory that you installed jdk not include bin directory
    • MAVEN_HOME -> directory that you extracted Maven not include bin
    • Update PATH variable, at end of it
      ;%JAVA_HOME%\bin;%MAVEN_HOME%\bin

Verify the installation

Goto command prompt and enter following command


mvn -version 
check the maven installed version
check the maven installed version

 

Lets create a project to demonstrate maven

  • Open spring STS or eclipse
  • Create new project [File -> New -> Project] make sure this is general project. (not Java project)
  • Create new files pom.xml in side project directory [Project Object Model]
  • Create directory structure as src\main\java (this is required by maven – convention over configuration)
  • Add following for pom.xml file

<project>
<groupId>com.ks</groupId>
<artifactId>HelloMavenApp</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
</project>


pom.xml file in eclipse
pom.xml file in eclipse

Note: pom.xml file should be under project folder. NOT under src

  • Group id -> similar to company (com.krishantha or things like that)
  • Artifact id -> application name
  • Version -> what is the version going to build
  • Model version -> xml version which we going to process
  • Packaging -> output format (jar)

Create Java class

follow the instruction in following image

create file in eclipse
create file in eclipse

Create classic file. not java class

provide file name to class

provide file name to class
add following program to class

public class HelloMaven{
public static void main (String [] args){
System.out.println("Hello Maven");
}
}

Now go to command prompt and navigate to project folder.

Enter following command.

mvn clean

Above command will be download few jar files and other plugins it need

mvn compile

It will compile your program and now should have target directory

mvn package

It will package your program as jar file as expected in pom.xml

Structure

  • By default it need src/main/java directories
  • Src/test/java is for hold the testing code
  • Compile source code to target directory
  • Run according to the pom.xml file

Pom.xml

There are major 4 categories for pom.xml

  • Project information
    • Groupid
    • Artifactid
    • Version
    • Packaging
  • Dependencies
    Mention about the project dependencies
  • Build
    Directory structure
  • Repository
    from where we can download dependencies

Dependencies

  • It is a registry of required dependent libraries
  • It s need to have 3 minimum parameters to work
    • Groupid
    • Artifactid
    • Version

What Maven commands means

  • Clean
    Delete target directories and generated resource
  • Compile
    Compile source code and copy required resources
  • Package
    Run compile , run unit test , package app based on pom.xml
  • Install
    Package command + install in local repository
  • Deploy
    Install command + install corporate repository

Local repository

  • Default maven repository is you home directory and .m2
  • For previous configuration it will store in C:\Users\kdinesh\.m2\repository
  • It will help to avoid the duplication of lib files over SCM and file system.

Default override

  • There is a way to override this defaults of the maven
  • Build section can do the job
  • You have evidence that how maven going to build the final artifact name
Lets see how we can override that method

Change the code as follows and run mvn clean package

change pom file as follows to demonstrate the <build> section

<project>
<groupId>com.ks</groupId>
<artifactId>HelloMavenApp</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<build>
<finalName>MavenDemoApp</finalName>
</build>
</project>

again navigate to project folder and enter  mvn package command. after its finished navigate to target folder inside project folder (newly created when compile) and list available files. you can see file name changed.

mvn package

cd target

dir

build

version

  • Version can be anything and no hard rules
  • However SNAPSHOT is a specific version over releases.Changes always downloaded before compile the code
  • It save you by sending development version for production
  • SNAPSHOT must be in capital letter to serve the purpose

Transitive dependencies

  • This is the reason to Maven become a super star
  • If we refer one dependency its automatically pulled all relevant dependencies.
  • This is very useful as creator only know what are compatible of.

scope

There are 6 scopes available for define

  • Compile -> default scope. Artifacts ships with app
  • Provided -> artifact will provided by container
  • Runtime -> no need for compile. Need to execute Dynamic library. (like jdbc)
  • Test -> only need for test execution
  • System -> DO NOT USE 🙁 hard code file path to your file system
  • Import -> dealing with dependency management.

to verify how above theory works change pom.xml file as follows

<project>
<groupId>com.ks</groupId>
<artifactId>HelloMavenApp</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<repositories>
<repository>
<id>jboss-public-repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
</dependency>
</dependencies>
<build>
<finalName>MavenDemoApp</finalName>
</build>
</project>

now right click on project at package explore –> configure –> Convert to Maven

Convert project to maven project
Convert project to maven project

now you can see even though we added one file (hibernate-core it fetch all required dependencies

Maven Transitive dependencies
Maven Transitive dependencies

Repositories

There are two types of repositories

  • Dependency repository
  • Plugin repository
Local Repository

This is the place where maven looked first. If not available it download form maven repo.

Remote repository

Simple storage which has http access

Phases

  • Validate
    Validate the project requirement and information
  • Compile
    Compile the source code
  • Test
    Test the compiled code
  • Package
    Packaging based on pom
  • Integration-test (from Maven 3.x)
    Run integration test
  • Verify
    Run integrity checks
  • Install
    Install package on local repo
  • Deploy
    Copy package for report repo

Compiler plugin

  • Use to compile both test and source code
  • Invoke javac with the class path setup from dependencies
  • Default is 1.5 regardless to the installed jdk
  • You can use configuration section to override this setting
  • you can see above warning on your IDE. you can change POM file as follows to fix above issue. its <build> section change as follows
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<finalName>MavenDemoApp</finalName>
</build>

if you got any error right click on project -> maven -> update project.

Java doc plugin

we can use this pugin to attach java-doc with package

Javadoc plugin condifuration
Javadoc plugin condifuration

change the POM file as above and execute

mvn install

Navigate to target folder and you can see java doc also generated.

now we have discussed almost full coverage of Maven. it is better to try your self and understand how it is work

Share this on your world...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePrint this page

One Response to “Maven 3.x from the beginning”

  1. How to mock ehCache using mockito | [NOK] - Notes of Krish

    […] create src/main/java and src/test/java directories. (if you not understand maven nature refer this article) […]

    Reply

Leave a Reply