Background Circle Background Circle

Maven_Part_1

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

what is maven ?

Maven is a build automation tool developed using the Java programming language. It is primarily used for Java-based projects to manage the build process, including source code compilation, testing, packaging, and more. Maven utilizes the Project Object Model (POM), where the pom.xml file describes the project’s configuration and dependency management.

Features of Maven:

The Maven Build Automation tool provides a lot of features to make the development easy. Below we listed them

  • Dependency Management: Automatically downloads and manages external libraries.
  • Standard Project Structure: Follows a fixed folder layout for source, test, and other files.
  • Build Lifecycle: Defines standard build phases like compile, test, and deploy.
  • Plugins: Supports plugins for compiling, testing, packaging, and more.
  • POM File: Uses pom.xml to manage configuration and dependencies.
  • Central Repository: Fetches dependencies from a shared online repository.
  • Build Profiles: Supports different settings for dev, QA, and production.
  • Reporting: Can generate Javadoc, test reports, and project documentation.
  • IDE Support: Integrates with Eclipse, IntelliJ, NetBeans, etc.

Maven Project Structure:

project-root/

├── pom.xml

├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/company/app/
│ │ │ └── Application.java
│ │ │
│ │ ├── resources/
│ │ │ └── application.properties
│ │ │
│ │ └── webapp/ (only for WAR projects)
│ │ └── WEB-INF/
│ │ └── web.xml
│ │
│ └── test/
│ ├── java/
│ │ └── com/company/app/
│ │ └── ApplicationTest.java
│ │
│ └── resources/
│ └── test-data.json

├── target/
│ ├── classes/
│ ├── test-classes/
│ └── app-1.0.0.jar

└── README.md

What is build ?

A build is the automated process of converting source code into a runnable or deployable artifact.
This process may include compiling code, running tests, resolving dependencies, packaging binaries, and preparing the application for deployment.

Real-World Example

In a Java project:

  • Developers write .java files

  • The build process:

    1. Compiles .java.class

    2. Runs unit tests

    3. Packages the result into a .jar or .war

    4. Makes it ready to deploy on a server

Why Builds Are Important

Without a build process:

  • Developers must manually compile and package code

  • Inconsistencies occur across environments

  • CI/CD pipelines cannot function

Source Code → Compile → Test → Package → Deploy

Why Companies Use Maven (Interview Insight)

Benefits

  • Convention over configuration

  • Strong dependency management

  • Widely adopted in enterprise projects

  • Excellent CI/CD support

Comparison (Common Interview Question)

ToolPurpose
MavenBuild + Dependency Management
GradleFaster, more flexible build tool
AntScript-based, no dependency mgmt
 
What Is an Artifact in Maven?
In Maven, an artifact is the output of a build process or any packaged file that Maven manages.
It is typically a JAR, WAR, or EAR file that is versioned, uniquely identified, stored, and reused.
How Maven Identifies an Artifact–>groupId : artifactId : version
ElementMeaning
groupIdOrganization or project group
artifactIdName of the project/module
versionArtifact version

What is the Difference Between JAR WAR and EAR?

JAR Files

To understand the differences between Ear and War files, first, we must take a look at JAR files. JAR stands for Java ARchive. JAR file is the basic artifact for Java applications. It’s a .zip file with the .jar extension. It includes contents such as classes, resources, and meta information files. We can run these files with java -jar command with just JRE.

WAR Files

WAR stands for Web Archive. WAR files are intended to contain complete Web applications. They are also .zip files and are an extension of the JAR file format. This means they also contain classes and resources, but they can also contain other JARS in the form of libraries, JSP files, Servlets, HTML, CSS, JS and XML like static web files.

A WAR file must contain a web.xml file in the WEB-INF folder. In this file, we declare the configurations of the web application.

JAR files can run as standalone applications but WAR files can not, we need a host for them to run, like a servlet/web container or an application server.

EAR Files

EAR stands for Enterprise Archive. EAR files are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web applications (WARs). Ear file contains all the web application-related technologies and also Java enterprise components like EJB, JMS etc.

Similar to WAR, the EAR is a JAR extension and must contain a special XML file, named application.xml, in the META-INF folder. In this file, we describe the enterprise application and list its modules. Additionally, we can add security roles for the whole app.

Like WAR, an EAR file also cannot be run as a standalone application. We must deploy it on an application server.

Article content

maven installation
===================
System Requirements
——————–
–> Maven 3.9+ requires JDK 8 or above to execute.
–> No memory minimum requirement
–> Disk [Approximately 10MB is required for the Maven installation itself.]–> Operating System [No minimum requirement. Start up scripts are included as shell scripts (tested on many Unix flavors) and Windows batch files.]

NOTE: All the external software are stored in /opt directory


step 0: Launch an EC2 machine & connect to that machine

step 1: sudo su – [switch to root user]——

step 2: Install java
——
sudo su –

yum update -y

javac -version
#Login as a root user

NOTE: If you want to search in Linux server use below command

sudo yum search java | grep -i OpenJDK

java -version

java 21
——–

sudo yum install java-21-openjdk-devel -y

step 3: Install maven
——

Pre Requisite Software
—————————–
Java (JDK) is the Pre – Requisite Software for Maven.

javac -version

1) Login as a root user and change the directory to /opt/

sudo su –

cd /opt/

yum install wget unzip -y

2) Download the Maven Software

wget https://dlcdn.apache.org/maven/maven-3/3.9.10/binaries/apache-maven-3.9.10-bin.zip
wget https://dlcdn.apache.org/maven/maven-3/3.9.11/binaries/apache-maven-3.9.11-bin.zip

unzip apache-maven-3.9.11-bin.zip

3) Set the class path/Environmental Variable

# User specific environment and startup programs

——————————–

vi ~/.bash_profile —> add below two statements

export M2_HOME=/opt/apache-maven-3.9.11 –> this line tells where maven is intalled some tools and scripts use this variable to locate maven automatically
export PATH=$PATH:$M2_HOME/bin —> This line update the system path so that you can execute MVN command from anywhere in the terminal. without typing the full path /opt/apache-maven-3.9.11/bin/mvn

mvn -version or mvn -v —-> Not installed , you need to load. 

You can use below command for loading :

source ~/.bash_profile

Check maven version now:

mvn -version or mvn -v

Demystifying Maven’s Directory Structure: What’s Really in Those Folders?

If you’ve ever downloaded Apache Maven, you’ve seen those four mysterious folders: binconflib, and boot. They sit there quietly, holding the keys to one of Java’s most powerful build tools. But what’s actually inside them? Let’s pop the hood and explore each directory like digital archaeologists.

🎯 The bin/ Directory: Where the Magic Starts

The Launch Pad

Open the bin/ folder and you’ll find the entry points to the Maven universe:

maven-home/
├── bin/
│ ├── mvn ← Unix/Linux/Mac launcher
│ ├── mvn.cmd ← Windows launcher
│ ├── mvnDebug ← Debug mode for Unix
│ └── mvnDebug.cmd ← Debug mode for Windows

Pro Tip: Ever wondered why you type mvn clean install and things happen? The mvn script here is a sophisticated shell script that:

  1. Sets up Java environment variables

  2. Configures memory settings

  3. Bootstraps the entire Maven runtime

The Debug Scripts are your best friends when things go wrong. They enable remote debugging on port 8000, letting you connect your IDE and step through Maven’s execution like a boss.

⚙️ conf/: The Control Room

settings.xml – The Heartbeat of Maven

If Maven were a spaceship, conf/settings.xml would be its control panel. This single file governs:

<!– The Power Settings –>
<settings>
<!– Where to store downloaded jars? –>
<localRepository>/path/to/your/repo</localRepository>

<!– Need to work behind corporate firewall? –>
<proxies>
<proxy>
<id>corporate</id>
<active>true</active>
<protocol>http</protocol>
<host>proxy.company.com</host>
<port>8080</port>
</proxy>
</proxies>

<!– Mirrors for faster downloads –>
<mirrors>
<mirror>
<id>aliyun-maven</id>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
</settings>

User vs Global: Here’s a crucial distinction:

  • Global: MAVEN_HOME/conf/settings.xml (affects all users)

  • User: ~/.m2/settings.xml (your personal settings, overrides global)

Secret Weapon: The toolchains.xml file lets Maven use specific JDK versions for specific projects—perfect for maintaining legacy Java 8 projects while building new ones with Java 17.

📚 lib/: The Engine Room

Where Maven Lives

The lib/ directory contains everything Maven needs to run—except your project dependencies. Think of it as Maven’s private library:

lib/
├── maven-core-3.9.6.jar
├── maven-model-3.9.6.jar
├── maven-settings-3.9.6.jar
├── slf4j-api-2.0.9.jar
├── guava-31.1-jre.jar
└── … (50+ more JARs)

Critical Insight: Notice what’s NOT here? Your project dependencies! Those live in ~/.m2/repository. The lib/ only contains what Maven itself needs to function.

Common Misconception: Developers often think Maven plugins live here. Nope! Plugins get downloaded to your local repository just like regular dependencies.

🚀 boot/: The Secret Sauce

The Classloading Conductor

The boot/ directory contains Maven’s most ingenious component:

boot/
└── plexus-classworlds-2.7.0.jar

This dir helps to start maven correctly.

 

 

Prev Post

Next Post

Leave a Reply

Your email address will not be published. Required fields are marked *