Java, J2EE & SOA Certification Training
- 33k Enrolled Learners
- Live Class
Maven is a powerful build tool for Java projects. It is a management and automation tool. It is basically written in Java Language and used to build and manage projects written in C#, Ruby, Scala, and other languages. Before I dive into the world of Maven in Java, let me explain to you the agenda for this article.
I will be discussing the following topics mentioned:
Maven is a really strong project management tool that is used to build and manage any Java-related project. Maven helps in easing the job of a Java developer. It is capable of handling a project’s build, reporting, and documentation.
After understanding the meaning of Maven in Java, let me show you the working through the help of a diagram.
Here, there are specific terms that might be new for you. I will elaborate them one by one.
POM files: Firstly, in order to configure the Maven, you need to use Project Object Model (POM) which is stored in a pom.xml-file. POM includes the configuration setting related to Maven. It also has goals and plugins. In the process of execution, Maven reads the goal, searches for the POM in the directory and gets the required information.
Build Life Cycles, Phases, and Goals: A Build Lifecycle is a well-defined sequence of phases. It defines the order in which the goals are to be executed. Each build phase consists of a sequence of goals. If one life cycle is executed, all build phases in that life cycle are executed. If a build phase is executed, all build phases before it in the pre-defined sequence of build phases are executed.
Build plugins: Maven is basically a plugin execution framework and every task is done through plugins. If there is a need to perform a certain set of actions for your project which are not covered by the standard Maven build phases and goals, you can add a plugin to the POM file. Maven has some standard plugins that can be used in the java project.
A plugin actually gives a set of goals. Plugins are mentioned in pom.xml using plugin elements.
Build profiles: Build profiles are used if you need to build your project in several ways. You can also customize build for multiple environments such as Production v/s Development environments. There are three types of build profiles: Per profile, Per user, Global.
Repository: A repository is simply a directory on your machine. The project jars, plugins or any other project-related materials are stored here. There are three different types of repositories: Local, Central, Remote.
You need to follow these simple steps:
1. Your system should have Java installed in it.
2. Set Java environmental variable (click here)
3. Download Maven (from here)
4. Now you have to unpack your Maven zip at your desired location.
5. Add the bin directory of the directory you created, i.e., apache-maven-3.5.3(whatever version you have installed) to the PATH environment variable and system variable.
6. Now open cmd and run mvn -v command
7. If your screen displays the below-shown screenshot, then the installation process is completed.
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T01:19:05+05:30) Maven home: C:apache-maven-3.5.3bin.. Java version: 1.8.0_151, vendor: Oracle Corporation Java home: C:Program FilesJavajdk1.8.0_151jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
POM stands for Project Object Model. The pom.xml file contains information about the project and configuration information to build the project. It contains dependencies, build directory, source directory, test source directory, plugin, etc.
Maven goes through the pom.xml file, then executes the goal.
For the creation of a simple pom.xml file, you must need the following elements:
project : Root element of a pom.xml file.
groupId: Sub element of the project. It describes the Id for the project.
modelVersion: Sub element of the project. It tells you the model version.
artifactId: Sub element of the project. It specifies the id for the project. An artifact is either produced or used by a project. Examples of artifacts: JARs, source and binary distributions, and WARs.
Version: Sub element of the project. It tells you the version of the artifact under given group.
A sample of a pom.xml file is shown below:
<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.edureka.application1</groupId> <artifactId>my-app</artifactId> <version>1</version> </project>
There are some additional elements in pom.xml file:
Packaging: defines the packaging type such as war, jar etc.
Scope: defines the scope for the maven project.
Url: specifies the URL of the project.
Name: defines the name of the maven project.
Dependency: defines a dependency. It is used inside dependencies.
A sample code of pom.xml file showing the additional elements:
<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.edureka.application1</groupId> <artifactId>my-application1</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>Maven Quick Start Archetype</name> <ur>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> <scope>test</scope> </dependency> </dependencies> </project>
A repository is simply a directory on your machine. The project jars, plugins or any other project-related materials are stored here.
Let me elaborate on the different types of repositories.
1. Local Repository
2. Central Repository
3. Remote Repository
Whenever any dependency has to be searched, it is done in repositories. Maven initializes its search from Local repository, then Central repository and finally in Remote repository.
The local repository of Maven is a folder location on your machine, where all the project related elements are stored. As soon as the Maven build is executed, Maven automatically downloads all the dependency jars into the local repository.
By default, maven local repository is user_home/m2 directory.
If any dependency is not found in the local repository, then Maven traverses the central repository. Maven then downloads these dependencies in your local repository.
When Maven wants to download dependency, it goes to a remote repository. A remote repository is a repository present on a web server and is widely used to host the internal projects of an organization.
Starting with the advantages:
1. Maven gives an easy start to the project in different environments.
2. You can use Maven to easily build your project to a jar, war as per your requirements.
3. You can easily add new dependencies by writing the dependency code in the pom file.
Now, the disadvantages:
1. If the maven code for an existing dependency is not available, then one cannot add that dependency.
2. Maven installation along with plugins is a must.
With this, I conclude my article here. I hope the contents explained above proved to be informative and beneficial to you. We will resume exploring the Java world. Stay tuned!
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Maven in Java” blog and we will get back to you as soon as possible.