10 Maven
10 Maven
Maven
Apache Maven is a java based software project management and comprehension tool. Based on the
concept of a project object model (POM), Maven can manage a project's build, reporting and
documentation from a central piece of information. Maven provides d a complete build lifecycle
framework for project management. Development team can automate the project's build infrastructure in
almost no time as Maven uses a standard directory layout and a default build lifecycle.
In abstract, Maven simplifies and standardizes the project build process. It handles compilation,
distribution, documentation, team collaboration and other tasks seamlessly. Maven increases reusability
and takes care of most of build related tasks.
1. Build process
The "Build" is a process that covers all the steps required to create a deliverable product of your
software into preproduction and production. In the Java world, this typically includes:
Generating source.
Compiling sources.
Executing tests (unit tests, integration tests, etc).
Packaging (into jar, war, ejb-jar, ear).
Running health checks (static analyzers like Checkstyle, Findbugs, PMD, test coverage, etc).
Generating reports.
A defined build process is an essential part of any development cycle because it helps close the gap
between the development, integration, test, and production environments. A build process alone will
speed the migration of software from one environment to another. It also removes many issues
related to compilation, classpath, or properties that cost many projects time and money.
Ant
Apache Ant is a Java library and command-line tool whose mission is to drive processes described
in build files as targets and extension points dependent upon each other. The main known usage of
Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile,
assemble, test and run Java applications. Ant can also be used effectively to build non Java
applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type
of process which can be described in terms of targets and tasks.
It has very low learning curve thus allowing anyone to start using it without any special preparation.
It is based on procedural programming idea. After its initial release, it was improved with the ability
to accept plug-ins.
Major drawback was XML as the format to write build scripts. XML, being hierarchical in nature,
is not a good fit for procedural programming approach Ant uses. Another problem with Ant is that
its XML tends to become unmanageably big when used with all but very small projects.
Maven
Apache Maven is a software project management and comprehension tool. Based on the concept of
a project object model (POM), Maven can manage a project's build, reporting and documentation
from a central piece of information. Its goal was to improve upon some of the problems developers
were faced while using Ant. Maven continues using XML as the format to write build specification.
However, structure is diametrically different.
While Ant requires programmer to write all the commands that lead to the successful execution of
some task whereas Maven relies on conventions and provides the available targets (goals) that can
be invoked. As the additional, and probably most important addition, Maven introduced the ability
to download dependencies over the network (later on adopted by Ant through Ivy). Main benefit
from Maven is its life-cycle. As long as the project is based on certain standards, with Maven one
can pass through the whole life cycle with relative ease. This comes at a cost of flexibility.
Now the interest for DSLs (Domain Specific Languages) continued increasing. The idea is to have
languages designed to solve problems belonging to a specific domain. In case of builds, one of the
results of applying DSL is Gradle and for e.g. gradle is used in Android for build and packaging.
Gradle
Gradle aims to help organizations ship better software, faster. Faster builds is one of the most direct
ways of achieving this;Gradle combines good parts of both tools and builds on top of them with
DSL and other improvements. It has Ant’s power and flexibility with Maven’s life-cycle and ease
of use. For example, Google adopted Gradle as the default build tool for the Android OS.
Gradle does not use XML. Instead, it had its own DSL based on Groovy (one of JVM languages).
As a result, Gradle build scripts tend to be much shorter and clearer than those written for Ant or
Maven. The amount of boilerplate code is much smaller with Gradle since its DSL is designed to
solve a specific problem: move software through its life cycle, from compilation through static
analysis and testing until packaging and deployment.
4. What it does?
Maven simplifies and give solution to the above-mentioned problems. It performs mainly following
tasks.
o It makes a project easy to build
o It provides uniform build process (maven project can be shared by all the maven projects)
o It provides project information (log document, cross referenced sources, mailing list,
dependency list, unit test reports etc.)
o It is easy to migrate for new features of Maven
You can download and install maven on windows, Linux and MAC OS platforms.
Note: Maven is Java based tool, so the very first requirement is to have JDK installed on your
machine.
Download Maven archive
https://maven.apache.org/download.cgi
OS Archive name
Windows apache-maven-3.5.0-bin.zip
Mac apache-maven-3.5.0-bin.tar.gz
Linux /usr/local/apache-maven
Mac /usr/local/apache-maven
Windows Append the string ;%M2% to the end of the system variable, Path.
Maven help in project structure ,dependencies management,the reason behind is that maven
repositories,maven talk to repositories and form the project structure.
Note: Maven get all its knowledge from maven repositories,how will be the project
structure,what is the project type etc.
Maven uses the concept of the repository to hold the jar files. There are two types of repository
namely local and remote repository.
Every maven project has a pom.xml file. If u run Your application with maven then, Now it checks
your local repository for the jar files. If the jar files are not found it goes to the remote repository
and download the jar. After downloading jars it will Execute life cycle, build phases and/or goals.
<groupId>java.org.wahid</groupId>
<artifactId>MavenTestApp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenTestApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
modelVersion It is the sub element of project. It specifies the modelVersion. Which is set
to 1.0.0.
groupId It is the sub element of project. It specifies the id for the project group.
artifactId It is the sub element of project. It specifies the id for the artifact (project).
An artifact is something that is either produced or used by a project.
Examples of artifacts produced by Maven for a project include: JARs,
source and binary distributions, and WARs.
version It is the sub element of project. It specifies the version of the artifact under
given group.
scope defines scope for this maven project. It can be compile, provided, runtime, test and system.
Hello World!
-------------------------------------------------------
TESTS
-------------------------------------------------------
Running com.eqv.wahid.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.059 sec
Results :
Each of these build lifecycles is defined by a different list of build phases, where in a build phase
represents a stage in the lifecycle.
Given the lifecycle phases above, this means that when the default lifecycle is used, Maven will
first validate the project, then will try to compile the sources, run those against the tests, package the
binaries (e.g. jar,wa), run integration tests against that package, verify the integration tests, install
the verified package to the local repository, then deploy the installed package to a remote
repository.
Summary:
✓ Maven is a software build management and comprehension tool.
✓ Maven is based on the concept of a project object model, Maven helps in code build,
dependency management, documentation creation, site publication, and distribution
publication are all controlled from the declarative file.
✓ Maven can be extended by plugins to utilise a number of other development tools for
reporting or the build process.
✓ Maven Archetype is a set of tools to deal with archetypes, i.e. an abstract representation of a
kind of project that can be instantiated into a concrete customized Maven project. An
archetype knows which files will be part of the instantiated project and which properties to
fill to properly customize the project.