maven-notes

book-body

Brief Notes on Maven Architecture

Contents

local

Overview

Just some brief notes and pointers regarding maven. This is not maven tutorial. This is just intended to help you recollect what you already know and build some more clarity on it.

References

Concepts

  • Following concepts are involved:
    • Project Object Model
    • Life Cycle Stages
    • Packaging Type
    • Goals
    • Project and Modules
    • Build Plugins
    • POM is declarative, not procedural.
    • Local Repository, Central Repository
    • Reuse component projects Locally
    • Build Profile.
    • Implicit Target (Jar) Filenames
    • Dependency deduction and Parallel Execution
    • Dependency versioning

Notes

  • POM - Project Object Model is expressed in pom.xml file. Object oriented representation of projects. Declarative instead of procedural.

  • Build lifecycle has following stages: validate, compile, test, package, verify, install and deploy.

  • Ultimately you want to express the dependency tree and specify actions for specific goals.

  • You can have a single POM file (in simple project) or collection of directories with their own POM files. Each POM file represents a Project. POM files can inherit from another. POM files inheritance form a tree structure (not graph). But implicit dependencies between projects is a graph.

  • A project can aggregate multiple component sub-projects called modules in pom.xml

  • Each POM file associated with packaging type which is any one of following:

    • POM (just plain POM file only)
    • jar is the default. It implicitly immediately defines implementation for goals in various default lifecycles.
    • Other goals are war and ear
    • maven-plugin packaging type is used to bundle implementation for custom goal (like compile, install, etc)
    • You can define your own packaging type, but usually you will never need to do that. See http://stackoverflow.com/questions/1427722/how-do-i-create-a-new-packaging-type-for-maven
  • Defining your own goal involves writing maven plugin (Mojo) which is packaged as maven-plugin.

Note

Note that packaging type just defines the implicit semantics for relevant goals in lifecycles. Depending on the goal, the relevant actions from relevant project POM files get executed. That's why this POM model it is Declarative and not Procedural.

  • You list the plugins (standard or custom plugins) that you use in POM under <build><plugins>. They are by default inherited i.e. custom goals and actions apply to child POMs as well. To break that inheritance, explicitly mark them as: <inherited>false</inherited>

  • Similarly, the parent <dependencies> also inherited to children. (You can't disable it).

  • Use mvn dependency:tree to dump dependencies.

  • For creating destination jar file it uses project artifactId, etc for target file name. You can customize it though using <finalName> property. If you want to generate 2 separate Jar files from single project, you need to use maven-assembly-plugin.

  • Build profile is an important concept. You can trigger various profiles as per different conditions like:

    • You can directly activate profile like below::
      mvn groupId:artifactId:goal -P profile-1,profile-2 mvn compile -P myDebugEnv

    • Define as <activeProfiles> in POM xml file itself or in

    • Define <activation> condition in POM xml e.g. when provided jdk version is 1.6 or os is Windows. or when a specific file is missing or present. etc.

  • Dependency versioning is much simpler by listing all default dependency definitions under <dependencyManagement> section in parent POM. This way, child POMs do not need to "repeat" the version. e.g. Child POM project depends on slf4j lib. But that lib version exists in top level POM. Similar considerations apply for <pluginsManagement>

Diagrams

  • Maven Directory Structure:

Maven Dir Structure

  • Life cycle bindings - Highlevel Architecture (Source) :

Maven Highlevel Architecture

  • Maven Workflow ... :

Maven Lifecycle Flow

  • Maven Repositories ... (Source) :

Maven Repositories

  • POM Components ... (Source) :

POM Components