Sunday, September 25, 2016

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.

In case of multiple development teams environment, Maven can set-up the way to work as per standards in a very short time. As most of the project setups are simple and reusable, Maven makes life of developer easy while creating reports, checks, build and testing automation setups.

Maven supports developers to manage following activities:


  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution
  • mailing list


Convention over Configuration

Maven uses Convention over Configuration which means developers are not required to create build process themselves.
Developers do not have to mention each and every configuration detail. Maven provides sensible default behavior for projects. When a Maven project is created, Maven creates default project structure. Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml.

In order to build the project, Maven provides developers options to mention life-cycle goals and project dependencies (that rely on Maven pluging capabilities and on its default conventions). Much of the project management and build related tasks are maintained by Maven plugins.

POM.XML:

POM stands for Project Object Model. It is fundamental Unit of Work in Maven. It is an XML file. It always resides in the base directory of the project as pom.xml.

POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal. Some of the configuration that can be specified in the POM are following:

  • project dependencies
  • plugins
  • goals
  • build profiles
  • project version
  • developers
  • mailing lists


What is a Maven Repository?

In Maven terminology, a repository is a place i.e. directory where all the project jars, library jar, plugins or any other project specific artifacts are stored and can be used by Maven easily.
Maven repository are of three types
  • local
  • central
  • remote
Maven local repository is a folder location on your machine. It gets created when you run any maven command for the first time.
Maven local repository keeps your project's all dependencies (library jars, plugin jars etc). When you run a Maven build, then Maven automatically downloads all the dependency jars into the local repository.It helps to avoid references to dependencies stored on remote machine every time a project is build.

Maven central repository is repository provided by Maven community. It contains a large number of commonly used libraries.
When Maven does not find any dependency in local repository, it starts searching in central repository using following URL: http://repo1.maven.org/maven2/

Sometime, Maven does not find a mentioned dependency in central repository as well then it stopped build process and output error message to console. To prevent such situation, Maven provides concept of Remote Repository which is developer's own custom repository containing required libraries or other project jars.

What are Maven Plugins?

Maven is actually a plugin execution framework where every task is actually done by plugins. Maven Plugins are generally used to :
  • create jar file
  • create war file
  • compile code files
  • unit testing of code
  • create project documentation
  • create project reports

Following is the list of few common plugins:

clean - Clean up target after the build. Deletes the target directory.
compiler - Compiles Java source files.
surefire - Run the JUnit unit tests. Creates test reports.
jar - Builds a JAR file from the current project.
war - Builds a WAR file from the current project.
javadoc - Generates Javadoc for the project.
antrun - Runs a set of ant tasks from any phase mentioned of the build.

Creating Project

Maven uses archetype plugins to create projects. To create a simple java application, we'll use maven-archetype-quickstart plugin.

Project Templates

Maven provides users,a very large list of different types of project templates (614 in numbers) using concept of Archetype. Maven helps users to quickly start a new java project using following command:
mvn archetype:generate

Archetype is a Maven plugin whose task is to create a project structure as per its template. 

No comments:

Post a Comment