Maven in Java EE project

Embed Size (px)

Citation preview

Maven with Java EE

Lecturer: Ondrej Mihlyi

ContentsMaven objectives

Build lifecycle

Dependencies and Repositories

Plugins, profiles and configuration

Multiple modules

Useful plugins

Jave EE projects with Maven

Maven ObjectiveA comprehensive model for projects which is reusable and maintainable

plugins and tools that interact with this declarative model

Build automation, Testing automation, Deployment, Automatic download of dependencies, Generating of documentation, ...

Convention over ConfigurationDevelopers are not required to define build process themselves

No need to define all configuration details

Many default conventions shared by maven projects

Standard project structure (source folder, configuration files, build folder)

Maven installationImplemented in Java

Executed

Using mvn command

Integrated as plugin to Java based IDE (Eclipse)

POM file (1)Defines information and configuration specific to a maven project

project dependencies

plugins

goals

build profiles

project version

developers

mailing list

POM file (2)Single POM file per project (project module)

Mandatory configuration: groupId, artifactId,version

Super POM contains default configuration defined by maven

All POM files inherit this configuration

Effective POM includes also all default and inherited configurationCreated in memory by Maven before each execution

To display effective POM: > mvn help:effective-pom

Build lifecyclewell defined sequence of phases, in which build steps (goals) are executed

Later phases depend on earlier phases, and are not executed without them

http://www.tutorialspoint.com/maven/maven_build_life_cycle.htm

Build lifecycle phases

PHASEHandles

prepare-resourcesresource copying

compilecompilation

testRun automated tests

packagepackaging

installInstallation to local repository

http://www.tutorialspoint.com/maven/maven_build_life_cycle.htm

Clean lifecycleSeparate lifecycle from default Build lifecycle

Phases in this lifecycle do not depend on phases in the Build lifecycle

Maven build goalSmallest step in maven execution

Defined by a maven plugin

Customizable by plugin configuration (per plugin, per goal)

Maven build phaseBlock of goals to be executed, together with all previous phases

Goals can be attached to a phase, some goals define a default phase

Goals attached to a phase are executed in order defined in pom.xml

Execution of phases and goalsExecution of a phase:

Executes the phase and all previous phases

> mvn install

executes compile, test, package, install phases

Execution of goal:Executes only single goal defined by a plugin

> mvn help:describe

Build propertiesConfiguration values can be defined in properties (variables)

For reuse

Customization of build commands at one place

Built-in properties

http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide

Build properties usage

Definition C:\windows

Usage${my.special.directory}

http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide

Maven ProfilesSet of predefined configuration values

Types of profiles: Per project (in pom.xml), Per user, Global (settings.xml)

Profile activation:

Explicit: command line, in settings.xml

On condition: value of env. variable, OS properties, presence of a file

RepositoriesStorage of downloadable artifacts and plugins

Artifacts and plugins identified by: artifactId, groupId, type, version

Properties of a repository:Name (id), path (IP address), Credentials

Types of repositories:Release, Snapshot, Central

Search in central repository: http://search.maven.org/

Repositories

Multiple modulesStorage of downloadable dependencies (artifacts, plugins)

Artifacts and plugins identified by: artifactId, groupId, type, version

Basic maven plugins

PluginDescription

HelpGoals to aid in writing pom.xml

SurefireRuns unit tests, bound to test phase

JAR, WAR, EAR, ...Plugins for packaging final artifact, bound to package phase of particular project type

ResourcesCopy and modify project resource files, bound to process-resources phase

JavadocGenerate javadoc documentation

AntrunExecutes Ant tasks within Maven

http://maven.apache.org/plugins/

Useful maven plugins

PluginDescription

ArchetypeGenerate a skeleton project structure from a template

DependencyDependency manipulation (copy, unpack) and analysis

ReleaseRelease the current project - updating the POM and tagging in the SCM

EclipseGenerate an Eclipse project file for the current project

Build NumberCreate a build property for SCM revision and timestamp

ExecExecute a new executable or Java program

Other types of pluginsUtility plugins (help in maven builds)

Documentation plugins (to generate specific kind of documentation)

Quality check plugins (pmd, checkstyle)

Test plugins (integration testing)

Deployment to application server

Multiple modules in projectMaven supports hierarchy of modules

Parent projects of type POM can contain other child modules

Parents execute all tasks on all child modules by default

If a module depends on another project module, the module is added as a dependency

Typical EAR project structureParent project (type POM)

Backend module (type POM)

EJB module (type EJB)

Entity module (type JAR)

Frontend module (type POM)WAR module (type WAR)

Assembly EAR module (type EAR)

POM inheritanceAll pom.xml files in child modules inherit from all parents in hierarchy

All pom.xml files inherit from super POM

Effective POMConfiguration of parent POM is merged into POM of a child module

What is not overwritten in child module is copied from parent

In case of collections two options: overwrite all vs. Concatenate

Final configuration is represented by effective POM

Dependency managementConfigurational pattern to specify version numbders of all dependencies in the top parent POM

Do not specify version for dependencies defined in dependency management only if specific version is really required

Multi-module buildThrough parent project

All project with all modules are built by default

Can specify on command line, which modules are built and whether with dependencies

Projects are built so that dependencies are built before modules that depend on them

http://maven.apache.org/guides/mini/guide-multiple-modules.html