Testbytes IN website

All You Need To Know About Apache Maven

September 3rd, 2018
All You Need To Know About Apache Maven

When you start running an application you need somebody to instruct. Most of the people start running their programs without much thought that what is happening behind the scenes.

They might store all the Java libraries in a folder on their hard drive. The problem arises when you try to move the program from one computer to another, the directories that you have created might not match the directories on your new computer.
The problem gets much worse when you try to make sure that everyone using this program is using the same versions of libraries to compile but you don’t need to worry as there is a much better solution i.e. Apache Maven.
It is a tool that helps you to structure your project. If you want to learn more about maven keep reading…
About Apache Maven
Apache Maven is a build automation tool or a project management tool that provides the developer with complete build lifecycle framework.
It is used for building and managing any Java-based project and it is built under the license of Apache 2.0. There are lots of libraries available on the Maven repository.
Most of the times when you work on a project you require 3rd party libraries like MySQL connector, spring, spring mvc, hibernate etc.
You can do it in two ways i.e. you can download the dependencies and place the jar files in lib folder or you can use Maven Repository. Well, the second one is the best option.
Maven is a Yiddish word that means “accumulator of knowledge”. The main motive behind developing the Maven is to get a standard way to build the projects, to get a clear picture of what is a project consists of and to get an easy way to publish the project information.
The day to day work of Java developers could be easier with the use of Maven and it also helps in the comprehension of any Java-based project. Maven is developed by the Apache Software foundation initially on 13 July 2014.
Objectives of Apache Maven

  • The main objective of Apache Maven is to enable the developer to understand the complete state of development effort in shortest time span and to give a comprehension project model that is easy to understand, reusable and maintainable.
  • Making build process easy by providing shielding from the details.
  • Maven provides a uniform build system by using project object models and a set of plugins that are shared by all projects
  • Maven provides the developers with quality project information such as dependency list, mailing list, unit tests, cross-referenced sources, etc.
  • It makes easy to guide a project in one direction and provide best practices guidelines
  • Maven provides its clients with an easy way to update the installations so that they won’t miss taking advantages of changes done in Maven software.

Features of Apache Maven

  • Apache Maven offers a simple project setup following the best practices. Developers can easily get a new project and start it in seconds
  • There is no ramp-up time for new developers coming with a project
  • Maven can easily work with multiple projects at the same time
  • There is no need for extra configuration and provide instant access to new features
  • It is extensible, with the ability to easily write plugins in Java or scripting languages
  • Maven has the capacity to build a number of projects into predefined outputs like metadata, jar, war etc.
  • Maven has better and improved error reporting. It provides the developers with a link to the Maven wiki page where a developer can get the whole description of errors.
  • There is no need to specify parent in the sub-model for the purpose of maintenance
  • Apache Maven is extensible via plugins that keep the Maven core small. For example, if you don’t know how to compile Java source code, the Maven compiler plugin will handle all this
  • Maven has a large repository of libraries. You can load project dependencies from the local file system or from the internet
  • Apache Maven comes with a feature of dependency management. The build system of maven resolve the dependencies and build the dependent projects if required
  • It has another distinguish feature of convention over configuration.
  • They don’t have to mention configuration details

How to Use Apache Maven
Maven is a Java tool, so, if you want to use Maven for your projects you must have installed Java. First of all, you need to download the Maven and follow the installation instructions to install Maven on your system.
Once you have downloaded and installed Maven on your computer you need to type a command prompt i.e. “mvn –version”, then it will print out the installed version of Maven.
Sometimes you may require extra configuration depending upon the network setup. Make sure that you are prepared to use Maven on windows.
Create a Project

  • You need to create a directory and need to start a shell in that directory. Execute the Maven goal on your command line i.e. “mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false”
  • Sometimes you may need to execute the command twice before it succeeds. You will notice a standard project structure under the directory such as:
my-app
|– pom.xml
`– src
|– main
|   `– java
|       `– com
|           `– mycompany
|               `– app
|                   `– App.java
`– test
`– java
`– com
`– mycompany
`– app
`– AppTest.java

The directory contains project source code, test source, and POM.xml files.

  • To create and build a project in Maven you need to start eclipse and create a Maven project. You need to add necessary dependencies and build the project.
  • Then you need to add the dependency to the POM.xml file.
  • After that, you need to add Java class to the project
  • After adding Java class build a project then add Maven plugin and rebuild

What is Maven POM?
POM or Project Object Model is a fundamental unit of work in Maven. It contains all the project information and configuration details that Maven uses to build the project.
Maven checks the POM in the current directory while executing a task. POM was initially named as project.xml in Maven 1then it was renamed in to pom.xml in Maven 2. A POM contains the default values for most projects.

Also Read : 10 Key Factors for Successful Test Automation

It also reads and gets the needed information related to configuration and then executes the goal. Following are some of the configurations that are specified in the POM:

  • Project dependencies
  • Plugins
  • Goals
  • Build profiles
  • Project version
  • Developers
  • Mailing list etc.

Example of POM

<project xmlns = “http://maven.apache.org/POM/4.0.0”
xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
</project>

Super POM
The super POM is the default POM of Apache Maven. The POMs that you create for your projects are extended in configuration specified in the super POM.
Maven uses the POM configuration from super POM and project configuration in order to execute the relevant goal.
A super POM helps the developers to specify minimum configuration details in pom.xml file. You can run the command mvn help:effective-pom to look the default configurations of super POM.
Minimal POM
The minimal POM requires the following things:

  • Project root
  • Model version set to 4.0.0
  • GroupId
  • ArtifactId
  • And the version of the artifact under the specified group

Example of minimal POM

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>

Maven Repository
The Maven repository is a directory to store project jars, library jars, plugins and other project specific artifacts that are later used by Maven. It is categorized into three different groups
app testing
Local Repository: the local Maven repository is created when you run any of the Maven command for the first time. It keeps all your project’s dependencies into a folder.
It automatically downloads the dependency into local repository when you run a Mavel build
Example of Local Repository

<settings xmlns = “http://maven.apache.org/SETTINGS/1.0.0”
xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation = “http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd”>
<localRepository>C:/MyLocalRepository</localRepository>
</settings>

Central Repository: It is a Maven repository containing a large number of commonly used libraries. Central repository is used when Maven could not find the dependency in the local repository.
It is managed by Maven community and is not required to be configured; it only requires an internet access to be searched.
Remote Repository: When Maven fails to search the dependency on both local and central repository then it stops the build process and output an error message to console.
Maven provides the concept of remote repository to prevent such situation. Maven can download dependency from remote repository in the pom.xml file.
Example of Remote Repository

<project xmlns = “http://maven.apache.org/POM/4.0.0”
xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation = “http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>com.companyname.common-lib</groupId>
<artifactId>common-lib</artifactId>
<version>1.0.0</version>
</dependency>
<dependencies>
<repositories>
<repository>
<id>companyname.lib1</id>
<url>http://download.companyname.org/maven2/lib1</url>
</repository>
<repository>
<id>companyname.lib2</id>
<url>http://download.companyname.org/maven2/lib2</url>
</repository>
</repositories>
</project>
Testbytes IN website
Recent Posts
Subscribe
Please enter valid email address

Contact Us
Please type your Name
Please enter valid email address
Please enter phone no.
Please enter message
Testbytes IN website

Search Results for:

Loading...

Contact Us