What is Maven ?
Maven is a project management tool, based on the concept of a project object model (POM).
Maven includes a project lifecycle, a dependency management system and execution of plugin goals at defined phases in a lifecycle.
Convention over configuration
Maven uses convention over configuration. Convention over configuration means that the system assume resonable defaults and the user is not required to do the configuration of the system.
Maven's conventions apply to build phases:
- a maven project is assumed to produce a JAR file.
- maven defines a default lifecycle and a set of common plugins that know how to build and integrate software.
- maven core plugins apply conventions for many common processes.
Maven provides default location of directories:
basedir/
|__pom.xml
|
|__src/
| |___main/
| | |___java/ (java source code)
| | |___resources/ (java configuration)
| | |___webapp/ (web content root)
| |
| |___test
| |___java/ (test code)
| |___resources/ (test configuration)
|
|__target/ (generated JAR)
|___classes/ (compiled code)
The directory src/main is the most important directory,
all of its content will become part of the maven artifact
- the java source code is assumed to be in
src/main/java - the java configuration are assumed to be in
src/main/resources - the document root of the web module is
src/main/webapp
The directory src/test is the place where tests reside,
its content will not become part of the maven artifact:
- the test sources are assumed to be in
src/test/java - the test configuration are assumed to be in
src/test/resources
The directory target contains the JAR artifact
- the compiled code is in
target/classes
When you use maven, you write your source code and if you follow the conventions, you only have to put the code in the predefined directory, maven will take care of the rest.
A common interface
Maven provides a common interface for building software.
- Before maven, each project had its approach to build software and developers moving to a new project had to take time away from writing software to learn how to build the project.
- These days, you check the project and run mvn install.
Universal reuse of capabilities through Maven plugins
Maven retrieves dependencies and plugins from the remote repository.
Plugins are a way to add behavior when building projects:- for example, the maven Surefire plugin is responsible for running unit tests.
- suppose that maven add support for TestNG testing framework in addition to JUnit
- if you decide to use this new feature, you don't have to install new software, you only have to update the version number for a plugin in the POM: you will be able to write new unit tests and your old test will continue to execute without fail.
A conceptual model of a project
Maven keeps a model of a project in the POM file:- the model is a description of the software project
- the model assigns a unique set of coordinates to a project
- the model contains attributes of the project such as
- a set of coordinates: (a group id, an artifact id, version)
- the other projects this project depends upon.
- dependency management made possible by the presence of project coordinates
- remote repository of maven artifacts ordered by coordinates
- reuse of build logic contained in plugins which work with the configurations defined in the POM file
- searching and indexing maven artifacts stored in repository through the Nexus repository manager which work with information in the POM
- project portability through different IDE/tools which now find project information in the POM file
Comparing Maven with Ant
Below, two files, an ant build.xml and a maven pom.xml, which achieve the same result.
| Ant | Maven |
|---|---|
Ant is a build tool with targets and dependencies:
|
All you need to do in Maven is create a simple maven pom.xml file, place the source file in the default directory and run "mvn install". |
| sample build.xml | sample pom.xml |
<project name="my-project" default="dist" basedir=".">
<description>simple example build file</description>
<!-- set global properties for this build -->
<property name="src" location="src/main/java"/>
<property name="build" location="target/classes"/>
<property name="dist" location="target"/>
<target name="init">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile" description="generate the distribution" >
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Ouput into ${build} into a MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>
<target name="clean" description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}"/>
</target>
</project>
|
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany.app</groupId> <artifactId>my-project</artifactId> <version>1.0</version> </project> |
| Ant vs Maven | |
|
|
No comments:
Post a Comment