Maven is… • A project management framework from ASF. • It’s a Uniform Build System. • Making the build as easy as possible. • Comprehensive model for software projects.
What Maven provides • Convention over Configuration (konfigurasyon yerine kural ar) • Common Interfaces The time of build engineers is over Stop building the build and focus on development ! • Dependency Management Public repositories • Plugin Architecture • Documentation Generate documentation, reports . .
Instal ing Maven • You need JDK (not the JRE). • Just download the binary from http://maven.apache.org/download.cgi. • Current latest version is 3.2.5. • 3.2.x branch correlates with JDK6 while 3.1.x sticks with JDK5 • Open it up to a folder and add it to the path of the OS / Environment Variable.
To understand Maven one first need to understand the Project Object Model (POM)
POM • It is what makes your project a Maven Project. • It’s an XML file. • Not just for building the project; but also, project’s relationship, list of team members, license, SCM and etc. • Not only for Java projects • you can build FLEX code with appropriate plugins • you can build Microsoft binaries from C# code
Simplest POM <project> <modelVersion>4.0.0</modelVersion> <groupId>tr.org.bimo</groupId> <artifactId>MavenApp</artifactId> <version>1.0.0</version> </project> compile code run tests $ mvn instal } package as jar deploy to local repo
Simplest POM • GAV (groupid - artifactid - version) is the unique identifier for the project. • They are also cal ed as the coordinates. • groupid: is the identifier for a col ection of related modules. It’s more like a hierarchy that starts with the organization and then move on with the specific project group. (tr.org.bimo) • artifactid: is the unique identifier or the simple name of the module within a group (MavenApp) • version: identifier for the build number of the project.
Question here is How is that possible with 6 lines of XML? Where do I define the source folders, test folders and al other stuff ?
Super POM • Same analogy with java.lang.Object • usr/share/Java/maven/lib/maven-model- builder-3.2.5.jar:org/apache/maven/model/ pom-4.0.0.xml • Standard directory layout • Repo def. for http://repo1.maven.org/maven2 • To see the merged POM: mvn help:effective-pom
Let’s create a Maven Project • We’l be using Eclipse Luna • Version: IDE for JavaEE Developers is shipping with Maven Plugins. (Beware of the embedded maven bundled w/ Eclipse.) • We’l also create a project with the archetype soon.
Build LifeCycle • Process for building and distributing an artefact is clearly defined. • 3 built-in Life Cycles default: handles project deployment clean: clean project files generated by a build site: generate project’s site doc. • Each lifecycle consists of phases in specific order. • Zero or more goals attached to each phase.
Build LifeCycle validate default compile test integration-test verify install deploy
Build LifeCycle • validate - validate the project is correct and al necessary information is available • compile - compile the source code of the project • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed • package - take the compiled code and package it in its distributable format, such as a JAR.
Build LifeCycle • integration-test - process and deploy the package if necessary into an environment where integration tests can be run • verify - run any checks to verify the package is valid and meets quality criteria • instal - instal the package into the local repository, for use as a dependency in other projects local y • deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
default - full bundle (21 steps) Validates whether project is correct and all necessary information validate is available to complete the build process. initialize Initialises build state, for example set properties. generate-sources Generate any source code to be included in compilation phase. process-sources Process the source code, for example, filter any value. generate-resources Generate resources to be included in the package. Copy and process the resources into the destination directory, process-resources ready for packaging phase. compile Compile the source code of the project. Post-process the generated files from compilation, for example to process-classes do bytecode enhancement/optimization on Java classes. generate-test- Generate any test source code to be included in compilation sources phase. process-test- Process the test source code, for example, filter any values. sources test-compile Compile the test source code into the test destination directory. process-test- Process the generated files from test code file compilation. classes
default - cont’d test Run tests using a suitable unit testing framework. Perform any operations necessary to prepare a package before prepare-package the actual packaging. Take the compiled code and package it in its distributable format, package such as a JAR, WAR, or EAR file. Perform actions required before integration tests are executed. pre-integration-test For example, setting up the required environment. Process and deploy the package if necessary into an integration-test environment where integration tests can be run. post-integration- Perform actions required after integration tests have been test executed. For example, cleaning up the environment. Run any check-ups to verify the package is valid and meets verify quality criterias. Install the package into the local repository, which can be used install as a dependency in other projects locally. Copies the final package to the remote repository for sharing deploy with other developers and projects.
Dependency Mechanism • It’s where Maven can ease the development when you have hundreds of modules. • You may also depend on external frameworks and maven wil fetch them from repositories. • It happens with the Coordinates ! (GAV factor)
Transitive Dependencies • Transitive Dependencies are introduced with Maven version 2.0. • It al ows you get the libraries that your own dependencies require and include them automatical y. You don’t need to take care of those explicitly. • There is no limit to the number of levels on dependencies.
Dependency Exclusion X • If projectX depends on projectY and projectY depends on projectZ,
Y the owner of projectX can define an exclusion on projectZ for not to fetch it while fetching Z projectY.
Optional Dependencies • Let’s say projectY releases itself and mark its dependency on X projectZ as optional. When projectX depends on the projectY, project X wil only Y depend on the Y but not the Z. But it may also add the Z as a dependency if needed. (optional) Z
Usage of <optional> • ProjectY defines ProjectZ as below with optional dep. <dependency> <groupId>mycompany</groupId> <artifactId>projectZ</artifactId> <version>1.0</version> <optional>true</optional> </dependency>
Scope for Dependencies compile provided runtime test system import C Classpaths Compile TTest RRuntime
th Scope for Dependencies asspa Cl CTR compile default scope, compile scoped dependencies wil be in classpath CT provided similar to the compile, artefact should be provided by JDK / container @ runtime TR runtime not needed for compilation but need @ runtime T test dependency only needed for test compilation & execution CT system same as provided, but artefact should be provided explicitly with <systemPath /> with maven 2.0.9.. dependency to be replaced with --- import the dependencies in that POM's <dependencyManagement> section.
Versioning and Ranges <major>.<minor>.<incremental>-<buildnumber>.<qualifier> MajorVersion: 1.2 MinorVersion: 1.3 IncrementalVersion: 1.3.1 BuildNumber: 1.3.2-12 Qualifier: 1.2-beta-2 All versions with a qualifier are older than the same version without a qualifier. 1.2-beta-2 < 1.2 Identical versions with different qualifier fields are compared by using basic string comparison. 1.2-beta-2 is >1.2-alpha-6
Versioning and Ranges • Ranges can be defined as exclusive and inclusive. (, ) - Exclusive <version>[3.8 , 4.12)</version> [, ] - Inclusive <version>[3.8]</version> Dependency <version>[ , 3.8]</version> Mediation • Maven 2.x was supporting meta-versions like LATEST and RELEASE to specify special versions. • But for the sake of reproducible builds, Maven 3.x no longer supports usage of these meta-versions in POM.
Release and Snapshot Versioning • Snapshot versioning: Used by the projects during development as it implies that the project is stil under development and it may change.
<version>0.0.2-SNAPSHOT</version> • Release versioning: It’s the versioning that is assumed never to change. Only to be used for a single state of the project when it is released then updated to a next snapshot.
Archetypes • Templates for Maven projects • Descriptor XML files + Velocity templates • To create a Maven project: mvn archetype:generate -DgroupId=tr.org.bimo -DartifactId=sampleApp - Dversion=1.0-SNAPSHOT -DarchetypeGroupId=org.apache.maven.archetypes - DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.0
B:2.0 the highest one? A : A : 1.0 D : D : 1.0 C : C : 1.0 B : B : 1.0 commons-logging commons-logging B : B : 2.0 commons-logging commons-logging commons-logging commons-logging 1.0.1 1.0.4 1.1.1 commons-logging commons-logging 1.1.1 Which B wil Maven choose? G:A: V
A : A : 1.0 D : D : 1.0 C : C : 1.0 B : B : 1.0 commons-logging commons-logging B : B : 2.0 commons-logging commons-logging commons-logging commons-logging 1.0.1 1.0.4 1.1.1 commons-logging commons-logging 1.1.1 Maven wil choose the closest one.
A : A : 1.0 D : D : 1.0 C : C : 1.0 B : B : 1.0 commons-logging commons-logging B : B : 2.0 commons-logging commons-logging commons-logging commons-logging 1.0.1 ? ? ? 1.0.4 1.1.1 commons-logging commons-logging ? 1.1.1 Which commons-logging?
The Sequential first at the same distance… A : A : 1.0 D : D : 1.0 C : C : 1.0 B : B : 1.0 commons-logging commons-logging B : B : 2.0 commons-logging commons-logging commons-logging commons-logging 1.0.1 1.0.4 1.1.1 commons-logging commons-logging 1.1.1
The Final Graph… A : A : 1.0 D : D : 1.0 C : C : 1.0 B : B : 1.0 commons-logging commons-logging 1.0.4
Question: Project A wil work in the end OR not? A : A : 1.0 D : D : 1.0 C : C : 1.0 B : B : 1.0 commons-logging commons-logging 1.0.4
Question here is: With mvn compile which module wil compile first? <modules> <module>projectA</module> <module>projectB</module> <module>projectC</module> </modules>
Repositories • To manage build artefacts and dependencies • local or remote repositories • Can store jar, war, ear, ejb, rar . . • Maven looks to at least 2 repository if none specified - local one (under user home) - http://repo.maven.apache.org/maven2 (defined in uber-pom) • Release & Snapshot repositories
Plugins • Plugin-oriented Architecture plugin for even compiling the code. • A maven artefact w/ descriptor (plugin.xml) and one or more MOJOs • A MOJO is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related MOJOs. • In short, a MOJO is a maven goal, to extend functionality not already found in Maven. • Plexus as its IoC. (Guice also introduced with version 3.x) Why not Spring? • List of plugins supported by Maven Project. https://maven.apache.org/plugins
settings.xml • Maven provides 2 settings file, • Local settings.xml at %USER_HOME%/.m2/settings.xml • Global settings.xml at %M2_HOME%/conf/settings.xml • These files are not bundled with Maven project and don’t get distributed. • If both files exist, their contents get merged. • Local settings.xml overrides the global one.