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

devops programs2

The document provides a comprehensive guide on working with Maven, including creating a Maven project, understanding the POM file, and managing dependencies and plugins. It outlines the steps to create a Maven project using command line commands, details the structure of a sample POM.xml file, and explains the purpose of various Maven plugins. Additionally, it includes example Java code and instructions for building and running a Maven project.

Uploaded by

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

devops programs2

The document provides a comprehensive guide on working with Maven, including creating a Maven project, understanding the POM file, and managing dependencies and plugins. It outlines the steps to create a Maven project using command line commands, details the structure of a sample POM.xml file, and explains the purpose of various Maven plugins. Additionally, it includes example Java code and instructions for building and running a Maven project.

Uploaded by

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

2.

Working with Maven: Creating a Maven Project, Understanding the POM


File, Dependency Management and Plugins.

Working with Maven Project


Note: Always create separate folder to do any program.
 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

1. Using Command Line:


 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

Explanation of each part:


 mvn archetype:generate: This instructs Maven to generate a new
project based on a template (archetype).
NOTE: An archetype in Maven is a template or blueprint used to create
a new project with a predefined structure. It contains a set of files,
directories, and configuration settings that provide a starting point for
specific types of projects.
 -DgroupId=com.example: This sets the groupId for your project, which
is a unique identifier for your project group (usually your domain, e.g.,
com.example).
 -DartifactId=myapp: This sets the artifactId of your project, which is the
name of the resulting project (in this case, myapp).
 -DarchetypeArtifactId=maven-archetype-quickstart: This specifies the
archetype to use for generating the project. The maven-archetype-
quickstart is a basic Java project template for quickly starting a new
Java application.
[Common Archetypes:
Some common archetypes available in Maven (and their corresponding
archetypeArtifactId) include:
1. maven-archetype-quickstart:
o A simple Java application template, useful for quick Java
application development.
2. maven-archetype-webapp:
o A template for creating web applications, typically used for
creating Java web apps that will run in a servlet container (like
Tomcat).
3. maven-archetype-simple:
o A basic Maven project template, often used as a starting point
for many kinds of projects.
4. maven-archetype-site:
o A template for creating a Maven site project, useful for
generating documentation.
5. maven-archetype-j2ee-simple:
o A simple J2EE project template used for building enterprise
applications.
]
 -DinteractiveMode=false: This disables the interactive mode, meaning
Maven will use the default settings and won't ask for any additional
user input.

POM.xml
<?xml version="1.0" encoding="UTF-8"?>

<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.example</groupId>

<artifactId>myapp</artifactId>

<version>1.0-SNAPSHOT</version>

<dependencies>

<!-- JUnit Dependency for Testing -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13.2</version>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<!-- Maven Surefire Plugin for running tests -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.2</version>

<configuration>

<redirectTestOutputToFile>false</redirectTestOutputToFile>

<useSystemOut>true</useSystemOut>

</configuration>

</plugin>

</plugins>

</build>

</project>

 What are Dependencies?

 A dependency is an external JAR file, library, or project that your project relies on.

 These libraries or components might provide functionality that your application needs but
doesn’t implement itself (e.g., a logging framework, testing library, or database connector).

 Why Use Dependencies in Maven?

 Maven helps you manage and automate the process of including and updating these
external dependencies.

 What Are Plugins?

 Plugins in Maven are used to execute specific goals during the build lifecycle of a project.

 Each plugin typically consists of several goals, where a goal is a single task that the plugin
performs.

 Plugins are defined in the pom.xml file, and their goals can be bound to specific phases in the
Maven build lifecycle.

 How Are Plugins Used?

 You can define plugins in the build section of your pom.xml file.

 Each plugin can have multiple goals that perform different tasks.

 You can configure the plugin's behavior by specifying its configuration inside the pom.xml
file.
Common Plugins and Their Use Cases:
1. Maven Compiler Plugin:
o Goal: compile, testCompile
o Purpose: Compiles the source code (Java files) in the
project.
o Example Use: Compiling Java source code from
src/main/java.
2. Maven Surefire Plugin:
o Goal: test
o Purpose: Runs unit tests for the project (typically using
JUnit or TestNG).
o Example Use: Running the tests defined in src/test/java.
3. Maven JAR Plugin:
o Goal: jar
o Purpose: Packages the project into a JAR (Java ARchive)
file.
o Example Use: Packaging the compiled classes and
resources into a JAR file for distribution.
4. Maven WAR Plugin:
o Goal: war
o Purpose: Packages a web application into a WAR (Web
Application ARchive) file.
o Example Use: Packaging a web app into a .war file for
deployment on a servlet container.
5. Maven Install Plugin:
o Goal: install
o Purpose: Installs the project's artifact (e.g., JAR or WAR
file) into the local Maven repository.
o Example Use: Installing the packaged artifact so other
projects can reference it.
6. Maven Clean Plugin:
o Goal: clean
o Purpose: Cleans the project by deleting the target
directory, which contains compiled files, JARs, and other
build artifacts.
o Example Use: Removing old build artifacts before
performing a fresh build.
7. Maven Deploy Plugin:
o Goal: deploy
o Purpose: Deploys the artifact to a remote repository,
making it available for other projects or teams.
o Example Use: Deploying a JAR file to a Maven repository
(e.g., Nexus or Artifactory).
8. Maven Site Plugin:
o Goal: site
o Purpose: Generates a site for the project, which can
include reports, documentation, and other project-
related information.
o Example Use: Generating a site with HTML pages,
JavaDoc, and other project info.

App.java
package com.example;

public class App {

public int add(int a, int b) {

return a + b;

public static void main(String[] args) {

App app = new App();

int result = app.add(2, 3);

System.out.println("2 + 3 = " + result);

System.out.println("Application executed successfully!");

AppTest.java
package com.example;

import org.junit.Assert;

import org.junit.Test;

public class AppTest {

@Test

public void testAdd() {

App app = new App();

int result = app.add(2, 3);


System.out.println("Running test: 2 + 3 = " + result);

Assert.assertEquals(5, result);

To build and run maven project, follow these steps


1. mvn compile
2. mvn test
3. mvn package
4. java -cp target/myapp-1.0-SNAPSHOT.jar com.example.App

The above command is used to run a Java application from the command line. Here’s a breakdown
of each part:

 java: This is the Java runtime command used to run Java applications.

 -cp: This stands for classpath, and it specifies the location of the classes and resources that
the JVM needs to run the application. In this case, it’s pointing to the JAR file where your
compiled classes are stored.

 target/myapp-1.0-SNAPSHOT.jar: This is the JAR file (Java ARchive) that contains the
compiled Java classes and resources. It’s located in the target directory, which Maven creates
after you run mvn package.

 com.example.App: This is the main class that contains the main() method. When you run
this command, Java looks for the main() method inside the App class located in
the com.example package and executes it.

 A JAR JAR (Java ARchive) and a WAR WAR (Web Application ARchive) are both file types
commonly used in Java-based applications, and they are packaging formats used to
distribute Java applications or components.

You might also like