Software Training and Placement Center
Maven Overview
Maven Introduction
Apache Maven is a build automation tool primarily used for Java projects. It simplifies the build process
by managing project dependencies, building projects, and generating documentation. Maven uses XML
configuration files to define project structure and dependencies, allowing developers to standardize and
automate the build process.
Maven in IDE : Maven can be integrated with various Integrated Development Environments (IDEs) such
as:
• Eclipse: Using the M2Eclipse plugin.
• STS : Integrated support for Maven via the Spring IDE and other plugins.
• IntelliJ IDEA: Integrated support for Maven.
• NetBeans: Built-in support for Maven
What is Dependencies and Its Usage
Dependencies are external libraries or JAR files that a project needs in order to compile and run. Maven
manages these dependencies for you. Dependencies are specified in the [Link] file, and Maven
handles downloading the required JAR files from a repository.
[Link] code:
<dependencies>
<!-- Dependency for JUnit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Dependency for Log4j -->
<dependency>
<groupId>[Link].log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
</dependencies>
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
Software Training and Placement Center
What is POM Model, POM Implementation
POM (Project Object Model) is the fundamental unit of work in Maven. The [Link] file is the core of
a Maven project, and it contains information about the project, configuration details, and dependencies.
Key Elements of POM
• Project Coordinates: Group ID, Artifact ID, and Version.
• Dependencies: Libraries required by the project.
• Build Configuration: Plugins and goals used to build the project.
• Repositories: Locations to search for dependencies.
• Plugins: Tools used to perform tasks such as compilation, packaging, and testing.
Example of [Link]
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>[Link]</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>[Link]</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
Software Training and Placement Center
Maven Archetype
Maven Archetypes are project templates that can be used to generate a new Maven project with a
predefined structure. They help in starting new projects quickly by providing a standardized setup.
Example of Creating a Project from an Archetype
mvn archetype:generate -DgroupId=[Link] -DartifactId=my-app -
DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
In above example, maven-archetype-quickstart is a built-in archetype for creating a basic Java
application.
Maven in STS/Eclipse
Spring Tool Suite (STS) and Eclipse provide Maven integration through plugins:
• STS: Integrated support for Maven via the Spring IDE and other plugins.
• Eclipse: Maven support through the M2Eclipse plugin.
Using Maven in Eclipse
• Importing a Maven Project: Right-click on the Project Explorer, select Import -> Maven ->
Existing Maven Projects.
• Updating Maven Dependencies: Right-click on the project, select Maven -> Update Project.
• Running Maven Goals: Right-click on the project, select Run As -> Maven Build and specify
goals (e.g., clean install).
How Maven Actually Works Inside in Java
Maven works by managing the build lifecycle of a project. The process involves:
• Reading [Link]: Maven reads the [Link] file to understand project configuration,
dependencies, plugins, and goals.
• Downloading Dependencies: Maven fetches required dependencies from remote repositories
(e.g., Maven Central) and stores them in a local repository.
• Executing Goals: Maven executes goals defined in the build lifecycle. Common goals include
compile, test, package, install, and deploy.
• Building the Project: Maven compiles code, runs tests, packages the code into a JAR or WAR file,
and performs other build tasks as specified in the [Link].
Example Code of Maven Build Lifecycle
Here is a brief overview of how Maven commands can be used:
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
Software Training and Placement Center
# Compile the code
mvn compile
# Run tests
mvn test
# Package the application into a JAR/WAR
mvn package
# Install the package into the local repository
mvn install
# Deploy the package to a remote repository
mvn deploy
Summary
• Maven: A build automation tool for Java projects, managing dependencies, builds, and
documentation.
• Dependencies: External libraries specified in the [Link] file.
• POM (Project Object Model): Configuration file defining the project structure, dependencies,
and build configuration.
• Maven Archetypes: Templates for creating new Maven projects with a predefined structure.
• IDE Integration: Maven can be used within IDEs like Eclipse and STS for easier project
management.
• Maven Workflow: Manages the project build lifecycle, including compiling code, running tests,
packaging applications, and more.
Maven simplifies project management and builds automation by providing a standardized approach to
handling dependencies, building, and deploying applications.
Configuring Maven in STS
Configuring Maven in a Java project in Spring Tool Suite (STS) involves several steps. Here’s a detailed
guide to help you set up and configure Maven in STS
1. Install STS (Spring Tool Suite)
Download and Install STS:
• Go to the Spring Tool Suite download page and download the appropriate version for your
operating system.
• Follow the installation instructions to set up STS on your machine.
2. Create a New Maven Project
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
Software Training and Placement Center
Step 1 : Open STS: Launch Spring Tool Suite.
Step 2 : Create a New Maven Project:
• Go to File > New > Other....
• In the wizard, select Maven > Maven Project and click Next.
• Choose a workspace location and click Next.
• Select Maven -> Maven Project
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
Software Training and Placement Center
Step 3 : Select an Archetype:
• In the next step, choose an archetype for your Maven project. Common choices include
maven-archetype-quickstart for a simple Java application.
• Click Next.
• Then Select by default it will select the `maven-archetype-quickstart` 1.1 , if
you want we can change
Step 4 : Enter Project Details:
• Provide the following details:
o Group ID: e.g., [Link]
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
Software Training and Placement Center
o Artifact ID: e.g., my-app
o Version: e.g., 1.0-SNAPSHOT
o Package: e.g., [Link]
• Click Finish.
Import an Existing Maven Project
If you have an existing Maven project, you can import it into STS:
• Import the Project:
• Go to File > Import....
• Select Maven > Existing Maven Projects and click Next.
• Browse to the directory containing the [Link] file of your project and select it.
• Click Finish.
Step 5: Add Dependencies - You can add dependencies to your [Link] file
• Edit [Link]:
• Open the [Link] file in the Project Explorer.
• Add dependencies within the <dependencies> section.
Adding Dependencies Code:
<dependencies>
<!-- Example Dependency for JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Address : ByteSplash Training<scope>test</scope>
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]
</dependency>
</dependencies>
Software Training and Placement Center
Save the File:
• Save the [Link] file. STS will automatically update the project and download the
required dependencies.
Step 6 : Build and Run the Project
Build the Project:
• Right-click on the project and select Run As > Maven build....
• Enter goals such as clean install and click Run.
Run the Application:
• Right-click on the project and select Run As > Java Application if you have a main
method in your project.
Step 7 : Using Maven Goals : You can run Maven goals directly from STS
Run Maven Goals:
• Right-click on the project and select Run As > Maven Build....
• Enter the desired Maven goals (e.g., clean, install, package) and click Run.
Summary
• Create or Import a Maven Project: Use STS’s project creation wizard or import an
existing Maven project.
• Configure Maven: Ensure Maven settings are correctly configured in STS.
• Edit [Link]: Add dependencies and configure project settings.
• Build and Run: Use Maven goals to build your project and run it from STS.
By following these steps, you can efficiently configure and manage Maven projects in Spring
Tool Suite, leveraging Maven's capabilities for dependency management and build automation.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@[Link]