Thursday, 29 September 2016

Apache Maven - A java build tool

Maven
  • Build automation tool 
    • Build
      • Compiling 
      • running unit tests 
      • running integration tests
      • Package jar/war
      • Deploy to server
  • Uses POM.xml to describe Project, dependencies, build order & plugins.
  • Convention over configuration
Commands
  • mvn install - install to repository
Objectives:
  • Make build process EASY
  • Uniform build system
  • Describes how software is built
    • It is described by POM(Project Object model)
  • Describes the dependencies the software has
  • Build life cycle- made of up phases
  •  It is collection of plugins. Plugins perform action in maven.
  • It suggest default 'Project Structure' & 'build life cycles'
Generate new project from archetype- Command line
POM
  1. General information
  2. Build settings(Customize the maven lifecycles, add new 'Plugins' and 'Goals')
  3. Build environment(Profiles used to configure different deployment environments, Dev, Test, Prod)
  4. POM Relationships(Modules and SubModules)
  
  • Compile - Dependency available in all build phases & packaged with application
  • Test - Test complication & execution
  • runtime - required only at runtime and not at compile time
  • provided - When container provides dependency
  • system  - Same as provided but you will tell system where exactly is the provided jar
LifeCycles(3 life cycles)
  1. clean - Cleaning up previous build
  2. default or build - building and deployment
  3. site - creating project site documentation
Each lifecycle has Phases;
  • Clean(3 phases) - Few phases have builtin goals/plugins
  • Build(23 Phases)
  • Site(
Plugins
  • They have goals associated with it
    • help:describe -Dcmd =compile ->
Installation: Env variable setup

export M2_HOME=/Users/vdesu/maven/apache-maven-3.6.2
export PATH=$M2_HOME/bin:$PATH

mvn -version
---

Apache Maven- Most used build management tool/Project management tool.
Building a project means
  • Compile
  • Run tests
  • Package into jar files
  • Bundle jar into war files
  • Deploy to server
Maven automates all the above. It follows convention over configuration
  • myproject
    • src/main/java
    • src/main/resources
    • src/test/java
    • src/test/resources
    •  
Archetypes are similar to templates.  Different archtypes are Standalone, webapp, ear etc. Maven creates folder structure based on Archetypes.

Why use Maven:
  • Common interface (standard structure)
  • Dependencies
  • Repositories

Maven Plugin & Goals: Maven Plugin is group of one or more goals. Each plugin exposes atleast one goal.  mvn <plugin name>:<goal name> followed by parameters

  • archetype:generate
  • install:install
  • eg: mvn archetype:generate -DgroupId=com.venkat -DartifactId=hellomaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Maven coordinates
  • group id
  • artifact id
  • packaging
  • version
POM: Project Object Model

  • Helps to manage all dependency in web application

Maven Build Life cycle:
  • Validate -> validate pom.xml 
  • Compile -> generate .classfiles
  • Test ->  run the unit tests
  • Package -> create jar file
  • Integration Test-> 
  • Verify -> 
  • Install -> install to local repository
  • Deploy

Core concepts:
  • POM
  • Plugins & goals
  • Coordinates
  • Repositories
Maven Plugins & Goals
  • Plugin is collection of one or more goals
    • syntax: pluginId:goalId
    • eg: 
      • mvn archetype:generate -DgroupId=com.desu -DartifactId=hellomaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
      • cd hellomaven
      • mvn install   -> this will compile, run test & package
      • java -cp target\hellomaven-1.0-SNAPSHOT.jar com.desu.App
Lifecycle phases - Phases are associated with one or more goals.
  1. process-resources > resources:resources
  2. compile > compiler:compiler
  3. test > surefire:test
  4. package > jar:jar
When a phase is executed, system runs all its prior phases before executing it. 
Coordinates
  • groupId similar to package name in Java
  • artifactId similar to class name in java
  • packaging (extension)
  • version
Repositories
  • It describes 'how' the software is built.
  • It describes 'Dependencies' the software has. 
  • It is described by 'POM' - Project Object Model - stored in xml format
  • Sheilds from build complexities
Maven Build life cycle
  • Validate -> Compile -> Test -> Package -> Integration Test -> Verify -> Install -> Deploy
Multi Module Project
  • Create/copu POM.xml in parent folder of the 2 projects(childs)
    • Change the artifact id to 'parent', name,  
    • set Packaging to POM
  • Define child projects as Modules
  • Make following changes to Child project POM
    • Add <parent> element with maven coordinates of Parent
  • Build multi module project
    • mvn clean install  // builds all 3 projects
    • Add <dependency> element incase one project is dependent on other
Scopes
  • In dependency tag
    • Compile
      • default scope
    • runtime
      •  not required for compilation
    • provided 
      • required during test and run locally. Hence not exported to war/jar
    • test
      • compile & run Tests only
    • system 
      • similar to provided .. but not provided either by maven or system
    • import
      • used for pom based projects.

No comments:

Post a Comment