0% found this document useful (0 votes)
4 views26 pages

Lab Program 2

The document provides a detailed guide on creating and managing a Maven project, including steps for using the command line and IDEs. It explains the structure and purpose of the POM file, dependency management, and the use of plugins within Maven. Additionally, it covers how to specify dependency versions and repositories, along with practical steps for setting up a sample Maven project using Apache Commons Lang library.

Uploaded by

Bhavana Biradar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Lab Program 2

The document provides a detailed guide on creating and managing a Maven project, including steps for using the command line and IDEs. It explains the structure and purpose of the POM file, dependency management, and the use of plugins within Maven. Additionally, it covers how to specify dependency versions and repositories, along with practical steps for setting up a sample Maven project using Apache Commons Lang library.

Uploaded by

Bhavana Biradar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Prof. K.

Navya
Assistant Professor
Department of ISE

LAB PROGRAM -2

Course Name : DEVOPS


Course Code:BCSL657D
2. Working with Maven: Creating a Maven Project,
Understanding the POM File, Dependency Management and
Plugins.
1. Creating a Maven Project
There are a few ways to create a Maven project, such as using the command line,
IDEs like IntelliJ IDEA or Eclipse, or generating it via an archetype.

1.Using Command Line:


1.To create a basic Maven project using the command line, you can use the
following command:

mvn archetype:generate -DgroupId=com.example -DartifactId=myapp


-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
•groupId: A unique identifier for the group (usually the domain name).
•artifactId: A unique name for the project artifact (your project).
•archetypeArtifactId: The template you want to use for the project.
•DinteractiveMode=false: Disables prompts during project generation.

This will create a basic Maven project with the required directory structure
and pom.xml file.
2. Using IDEs
Most modern IDEs (like IntelliJ IDEA or Eclipse) provide wizards to generate
Maven projects. For example, in IntelliJ IDEA:

1.Go to File > New Project.


2.Choose Maven from the list of project types.
3.Provide the groupId and artifactId for your Project.
2. Understanding the POM File

The POM (Project Object Model) file is the heart of a Maven project. It is an
XML file that contains all the configuration details about the project. Below is
an example of a simple POM file:

A basic pom.xml structure looks like this:


Key element in pom.xml:

• <groupId>: The group or organization that the project belongs to.


• <artifactId>: The name of the project or artifact.
• <version>: The version of the project (often follows a format like 1.0-
SNAPSHOT).
• <packaging>: Type of artifact, e.g., jar, war, pom, etc.
• <dependencies>: A list of dependencies the project requires.
• <build>: Specifies the build settings, such as plugins to use.
Dependency Management
Maven uses the <dependencies> tag in the pom.xml to manage external libraries
or dependencies that your project needs. When Maven builds the project, it will
automatically download these dependencies from a repository (like Maven
Central).

Example of adding a dependency:


Transitive Dependencies
• Maven automatically resolves transitive dependencies. For example, if you
add a library that depends on other libraries, Maven will also download those.
Scopes
• Dependencies can have different scopes that determine when they are
available:
• compile (default): Available in all build phases.
• provided: Available during compilation but not at runtime (e.g., a web
server container).
• runtime: Needed only at runtime, not during compilation.
• test: Required only for testing.
4. Using Plugins
Maven plugins are used to perform tasks during the build lifecycle, such as
compiling code, running tests, packaging, and deploying. You can specify
plugins within the <build> section of your pom.xml.
•Adding Plugins
•You can add a plugin to your pom.xml like so:
1.Common Plugins
•maven-compiler-plugin: Compiles Java code.
•maven-surefire-plugin: Runs unit tests.
•maven-jar-plugin: Packages the project as a JAR file.
•maven-clean-plugin: Cleans up the target/ directory.
2.Plugin Goals Each plugin consists of goals, which are specific tasks to be
executed. For example:
•mvn clean install: This will clean the target directory and then install the
package in the local repository.
•mvn compile: This will compile the source code.
•mvn test: This will run unit tests.
Dependency Versions and Repositories
1.Version Ranges
• You can specify a version range for dependencies, allowing
Maven to choose a compatible version automatically. for
example:
2.Repositories
• Maven primarily fetches dependencies from Maven Central, but
you can also specify custom repositories. For example:
Working with Maven Project

• Open command prompt.


• mkdir program2 – this will create program2 folder.
• cd program2 – navigate program2 folder.
• After then follow the below step to working with Maven project.
Step 1: Creating a Maven Project
•You can create a Maven project using the mvn command (or through your IDE,
as mentioned earlier). But here, I’ll give you the essential pom.xml and Java code.
•Let’s use the Apache Commons Lang library as a dependency (which provides
utilities for working with strings, numbers, etc.). We will use this in a simple
Java program to work with strings.

mvn archetype:generate -DgroupId=com.example -DartifactId=myapp


-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Step 2: Open The pom.xml File
•You can manually navigate the project folder named call myapp and open
the file pom.xml and copy the below code and paste it then save it.
•In case if you not getting project folder then type command in your cmd.
•cd myapp – is use to navigate the project folder.
•notepad pom.xml – is use to open pom file in notepad.
Step 3: Open Java Code (App.java) File
•Open a file App.java inside
the src/main/java/com/example/ directory.
•After opening the App.java copy the below code and paste it in that
file then save it.

You might also like