0% found this document useful (0 votes)
2 views

unit - ii (1)

The document provides an overview of using Maven and Gradle for build and project management in software development, particularly for Java projects. It covers the installation of Maven, its features, build lifecycle, POM files, and the importance of dependency management. Additionally, it discusses Maven profiles, repositories, plugins, and the process of creating and building artifacts.

Uploaded by

nirmala.mca
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit - ii (1)

The document provides an overview of using Maven and Gradle for build and project management in software development, particularly for Java projects. It covers the installation of Maven, its features, build lifecycle, POM files, and the importance of dependency management. Additionally, it discusses Maven profiles, repositories, plugins, and the process of creating and building artifacts.

Uploaded by

nirmala.mca
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

DEVOPS AND FULL STACK DEVELOPMENT

UNIT II - COMPILE AND BUILD USING MAVEN & GRADLE

Introduction, Installation of Maven, POM files, Maven Build lifecycle, Build


phases(compile build, test, package) Maven Profiles, Maven repositories(local,
central, global),Maven plugins, Maven create and build Artificats, Dependency
management, Installation of Gradle, Understand build using Gradle

Introduction

 Maven is a project management tool. Basically it is a comprehension tool.


 It is based on Project Object Model(POM).
 This tool is used for build, dependency and documentation.
 Maven simplifies and standardizes the project build process.
 Apache Maven is a popular build automation and project management tool used
primarily for Java projects.
 The Maven can further be used in building and managing the projects written
using languages like C#, ruby and other programming languages.

Problems without Maven

If we choose not to use Maven for building the Java projects then we may encounter
following problems-

1) Dependency management :Without Maven we need to manage all the project


dependencies manually. It includes the complex tasks such as downloading library
files, ensuring their compatibility with the undergoing project, managing the version
control. It can be time-consuming and error-prone.

2) Build automation: in case of Struts, Hibernates, Spring framework we need to


manually create Jar and War files. The dependencies of these Jar files need to be set
up manually.
3) Standardized project structure: Maven creates a standardized project structure
which can be easily grasped by the developers. Without Maven, it is difficult to set up
and maintain the standard project structure that could be followed by all the seam
members

4) IDE integration: IDE stands for Integrated Development Environment, There are
many popular IDE such as Eclipse, IntelliJ IDEA, NetBeans and so on. Many popular
IDE provide seamless integration with Maven, allowing us to import, build and
manage Maven projects effortlessly, Without Maven, we need to configure the IDE
manually.

5) Lack of plugins and community support: Maven has support for plugins that can
simplify various tasks such as deployment and documentation generation Without
Maven, lack of Plugins forces the developer in perform deployment and
documentation tasks manually Which can be time consuming and error prone.

Features of Maven

Model-based builds − Maven is able to build any number of projects into predefined
output types such as jar, war, metadata.
Coherent site of project information − Using the same metadata as per the build
process, maven is able to generate a website and a PDF including complete
documentation.
Release management and distribution publication − Without additional
configuration, maven will integrate with your source control system such as CVS and
manages the release of a project.
Backward Compatibility − You can easily port the multiple modules of a project
into Maven 3 from older versions of Maven. It can support the older versions also.
Automatic parent versioning − No need to specify the parent in the sub module for
maintenance.
Parallel builds − It analyzes the project dependency graph and enables you to build
schedule modules in parallel. Using this, you can achieve the performance
improvements of 20-50%.
Better Error and Integrity Reporting − Maven improved error reporting, and it
provides you with a link to the Maven wiki page where you will get full description of
the error.

Installation of Maven

You can download and install maven on windows, linux and MAC OS platforms.
Here, we are going to learn how to install maven on windows OS.
To install maven on windows, you need to perform following steps:
1. Download maven and extract it
2. Add JAVA_HOME and MAVEN_HOME in environment variable
3. Add maven path in environment variable
4. Verify Maven

1) Download Maven
To install maven on windows, you need to download apache maven first.
Download Maven latest Maven software from Download latest version of Maven

For example: apache-maven-3.1.1-bin.zip


Extract it. Now it will look like this:

2) Add MAVEN_HOME in environment variable


Right click on MyComputer -> properties -> Advanced System Settings -
> Environment variables -> click new button
Now add MAVEN_HOME in variable name and path of maven in variable value. It
must be the home directory of maven i.e. outer directory of bin. For example: E:\
apache-maven-3.1.1 .It is displayed below:

Now click on OK button.

3) Add Maven Path in environment variable


Click on new tab if path is not set, then set the path of maven. If it is set, edit the path
and append the path of maven.
Here, we have installed JDK and its path is set by default, so we are going to append
the path of maven.
The path of maven should be %maven home%/bin. For example, E:\apache-
maven-3.1.1\bin .
4)Verify maven
To verify whether maven is installed or not, open the command prompt and write:
mvn −version

Now it will display the version of maven and jdk including the maven home and java
home.
Let's see the output:

POM Files
 POM stands for Project Object Model.
 It is XML file which is named as pom.xml.
 It is a fundamental building blocks for Maven project.
 It contains information related to the project and configuration information such as
 dependencies,
 plugins,
 source directory,
 goals and so on.
 Maven reads pom.xml file to accomplish its configuration and operations,

The sample pom.xml

<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.javatpoint.application1</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>

Elements of maven pom.xml file


Element Description

project It is the root element of pom.xml file.


It is the sub element of project. It specifies the modelVersion. It should
modelVersion
be set to 4.0.0.
groupId It is the sub element of project. It specifies the id for the project group.
It is the sub element of project. It specifies the id for the artifact
artifactId (project). An artifact is something that is either produced or used by a
project.
It is the sub element of project. It specifies the version of the artifact
version
under given group.

Additional Elements

packaging defines packaging type such as jar, war etc.


name defines name of the maven project.
url defines url of the project.
dependencies defines dependencies for this project.
dependency defines a dependency. It is used inside dependencies.
defines scope for this maven project. It can be compile, provided,
scope
runtime, test and system.

Maven Build Lifecycle


Each lifecycle consists of a sequence of phases. Each phase represents a stage in the
lifecycle, and executing a phase will trigger the execution of all preceding phases.
Below we provide information about each phase in the maven build build life cycle.
Maven Build Workflow
Below is a typical Maven build workflow for the default lifecycle.
 Validate Phase: Check the project configuration and validate if all information
required is present.
 Compile Phase: Compile the source code of the project.
 Test Phase: Execute unit tests using a testing framework.
 Package Phase: Bundle the compiled code into a JAR/WAR file.
 Verify Phase: Perform checks on the packaged code.
 Install Phase: Install the package to the local repository for use as a dependency
in other projects locally.
 Deploy Phase: Deploy the package to a remote repository for sharing with other
developers.

Built-in Build life cycle phases

Maven comes with 3 built-in build life cycles as shown below:

1) Clean: In the Clean lifecycle phase, it performs the cleaning operation in which it
deletes the build directory name target and its contents for a fresh build and
deployment. perform this operation we use command mvn clean.

2) Default: This phase handles the complete deployment of the project

3) Site: This phase handles the generating the java documentation of the project.
Let us discuss them in detail

1) Clean phase

The purpose of clean phase is to clean up the project and make it ready for fresh
compile and deployment. Clean lifecycle performs 3 types of clean operations -

1. Pre-clean - This phase executes processes needed prior to the actual project
cleaning.

2. Clean - It removes all files generated by the previous build.

3. Post-clean - It executes processes needed to finalize the project cleaning.

2) Default phase

The default lifecycle is the most commonly used one and is responsible for building,
testing, and packaging the project. It includes the following phases:
• Validate: Validates the project's configuration and dependencies.
• Compile: Compiles the project's source code.
• Test: Executes unit tests for the project.
• Package: Packages the compiled code and resources into a distributable format
(e.g., JAR or WAR).
• Verify: Performs additional checks and generates reports to ensure code quality.
• Install: Installs the project's artifact in the local Maven repository.
• Deploy: Deploys the project's artifact to a remote repository (if needed).

3) Site phase

The Maven site plugin handles the project site's documentation, which is used to
create reports, deploy sites, etc.
The phase has four different stages:
• Pre-site: Executes tasks before generating the site documentation.
• Site: Generates project documentation and reports.
• Post-site: Executes tasks after generating the site documentation.
• Site-deploy: Deploys the generated site documentation to a remote server (if needed).

Ultimately, the site that is created contains the project report.


Each phase in these lifecycles corresponds to a specific goal or set of goals. By
running a Maven command, you can specify a phase, and Maven will execute all the
preceding phases as well, ensuring that the build process is consistent and complete.

Maven Profile

 Maven Profile is an important feature of Maven. Using Profile we can customize


the build for different environments.

 Profiles are portable for different build environments. The build environment
means a specific environment for production, testing or development
environment instances. So in order to configure these instances Maven provides
the various features for the build using profile.

 The profiles are specified in a pom.xml file. Hence using a single pom.xml file, it
possible to work in different environments such as production or development
with the help of profiles.

 By defining the profiles it is possible to modify the pom.xml during the build
time. That means we can configure separate environments for development and
production instances. So, based on the parameters passed, the corresponding
profiles are activated accordingly,

Types of Build Profile


Build profiles are majorly of three types.
Type Where it is defined
Per Project Defined in the project POM file, pom.xml
Defined in Maven settings xml file (%USER_HOME
Per User
%/.m2/settings.xml)
Defined in Maven global settings xml file (%M2_HOME
Global
%/conf/settings.xml)

Maven Repository
Maven repository is a directory where all the project jars, library jar, plugins or any
other project specific artifacts are stored and can be used by Maven easily.
There are three types of maven repositories:-

 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 local repository by default get created by Maven in %USER_HOME%


directory.

 To override the default location, mention another path in Maven settings.xml file
available at %M2_HOME%\conf directory.

Example:
<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:
 This is the repository provided by maven community.
 This repository contains large set of commonly used/required
 libraries for any java project. Basically, internet connection is required if
developers want to make use of this central repository.
 But, no configuration is required for accessing this central repository.
Key concepts of Central repository are as follows −
 This repository is managed by Maven community.
 It is not required to be configured.
 It requires internet access to be searched.

Example:
<repositories>
<repository>
<id>central.repository</id>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories>

Remote Repository
 Maven does not find a mentioned dependency in central repository as well.
 It then stops the 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.

Example:
<repositories>
<repository>
<id>remote.repository</id>
<url>http://download.ogrname.com/maven2/</url>
</repository>
</repositories>

Maven Plugins:

Maven. Plugin is a central part of Maven framework which is used to perform specific
goal.

There are two types of Maven plugins -

1. Build plugin: These plugins are declared inside <build> element. These plugins
are executed at the time of build.

2. Reporting plugin: These plugins are declared inside the element <reporting. These
plugins are executed at the time of site generation.

Following is a list of some core plugins

 clean: Clean up after the build.

 compiler: Compiles Java sources.

 deploy: Deploy the built artifact to the remote repository.

 failsafe: Run the JUnit integration tests in an isolated classloader.

 install: Install the built artifact into the local repository

 resources: Copy the resources to the output directory for including in the JAR.

 site: Generate a site for the current project.

 surefire: Run the JUnit unit tests in an isolated classloader.

 verifier: Useful for integration tests it verifies the existence of certain conditions.

Example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source> <!-- Java source version -->
<target>1.8</target> <!-- Java target version -->
</configuration>
</plugin>
</plugins>
</build>
Benefits of Maven Plugins: • More detailed and well architecture. • A managed life
cycle • Implementation of multiple languages • Reusability of the common build
logic. • Ability to write maven plugins completely in java

Maven create and build Artificats:


Maven is a powerful build tool and project management tool used primarily for Java
projects. It is used to manage dependencies, compile code, and create artifacts such as
JAR (Java Archive) files. Here's a general outline of how Maven creates and builds
artifacts: 1. Project Setup: First, you need to set up your Maven project. This typically
involves creating a project directory structure with a pom.xml file, which defines the
project's configuration and dependencies. 2. POM.xml Configuration: In the pom.xml
file, you specify the project's metadata, dependencies, and build settings. You can
define the project's packaging type, which could be JAR for a Java library, WAR for a
web application, or other formats.3. Dependency Resolution: Maven will
automatically download and manage the project's dependencies based on the
specifications in the pom.xml file. It retrieves the required JAR files from a central
repository or other specified repositories. 4. Compilation: When you run the mvn
compile command, Maven compiles your Java source code, producing class files.
These class files are stored in the target directory by default. 5. Testing: You can run
tests on your code using the mvn test command. Maven executes test cases and
reports the results. 6. Packaging: To create an artifact (such as a JAR file), you use the
mvn package command. This step takes the compiled classes and resources and
packages them according to the project's packaging type (e.g., JAR). 7. Install: If you
want to install the artifact in your local Maven repository, you can use the mvn install
command. This makes the artifact available for other projects on your local machine
to use.UNIT - II 8. Deployment: For distributing your artifact to a remote repository,
you would configure Maven to deploy it using the mvn deploy command. This is
often used when sharing your library with others or deploying a web application to a
server. 9. Customization: You can customize the build process by adding plugins to
your pom.xml file. Plugins can perform various tasks, such as code generation, code
analysis, or even custom packaging.

Maven Dependencies Management:


In Maven, a dependency is just another archive-JAR, ZIP, and so on-which our
current project needs in order to compile, build, test, and/or run.

These project dependencies are collectively specified in the pom.xml file, inside of a
<dependencies> tag.

When we run a maven build or execute a maven goal, these dependencies are resolved
and then loaded from the local repository.

If these dependencies are not present in the local repository, then Maven will
download them from a remote repository and cache them in the local repository.

Dependency management is an important activity because to build a product we need


several library files, plugins or some external dependencies.

We have to be sure that all these components are compatible for building our product.
This task can be taken care by dependency management.
Example of dependency

Here is a dependency section of pom.xml file.

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.3.5 RELEASE</version>

</dependency>

<dependencies>

Dependency Scope:

Dependency scope in maven is classified into 5 different types. Each of these scope
controls the dependencies that are available in the class path and dependencies that
are included in the application.
Sr.No
Scope & Description
.

compile
1 This scope indicates that dependency is available in classpath of project.
It is default scope.

provided
2 This scope indicates that dependency is to be provided by JDK or web-
Server/Container at runtime.

3 runtime
This scope indicates that dependency is not required for compilation, but
is required during execution.

test
4 This scope indicates that the dependency is only available for the test
compilation and execution phases.

system
5
This scope indicates that you have to provide the system path.

import
This scope is only used when dependency is of type pom. This scope
6
indicates that the specified POM should be replaced with the
dependencies in that POM's <dependencyManagement> section.

You might also like