Welcome to our hands-on guide to using Maven in DevOps! If you're new to the world of DevOps or looking to integrate Maven into your workflow, this blog will take you through the basics and help you get started with a practical project.
What is Maven?
Maven is a powerful build automation tool used primarily for Java projects. It simplifies the process of building, testing, and deploying applications by managing project dependencies and configuration. Maven uses an XML file called pom.xml
(Project Object Model) to manage project settings and dependencies.
Why Use Maven in DevOps?
In a DevOps environment, automation and integration are key. Maven fits well into this ecosystem by:
Automating Builds: Compile code, run tests, and package applications automatically.
Managing Dependencies: Easily manage and update project libraries.
Consistent Builds: Ensure builds are consistent across different environments.
Setting Up Maven
Installing Maven with Apt
You can install Maven on Ubuntu in a quick and easy way through the apt command. For this, you must follow these steps.
Step-1: Update the system repositories
Open the terminal on Ubuntu and type the command that follows:
sudo apt update -y && sudo apt upgrade -y
Step- 2: Install Maven
Install Maven on Ubuntu from the official repository using the command that follows:
sudo apt install maven -y
Step- 3: Verifying installation
Verify the installation by checking Maven's current version with the command that follows:
mvn -version
If the installation was a success, the output which is shown in the image below will be displayed:
And, you're done.
Hands-On Project: Building a Simple Java Application
Let's create a simple Java application using Maven. This project will cover creating a Maven project, managing dependencies, building the project, and running tests.
Step 1: Create a Maven Project
Open a Terminal and navigate to your desired project directory.
Run the following command to create a new Maven project:
git clone https://github.com/jaiswaladi246/springboot-java-poject.git
This command creates a new Maven project with a standard directory structure.
Step 2: Understand the Ideal Project Structure
Navigate into the newly created application
directory. You'll see a high level structure like this:
pom.xml
: The configuration file for your Maven project.src/main/java
: Contains your application's source code.src/test/java
: Contains your test code.
Step 3: Build the Project
Run the following command in your terminal to compile the project:
mvn compile
This command compiles your source code and stores the compiled files in the target/classes
directory.
Step 4: Run Tests
To run your unit tests, execute:
mvn test
Maven will compile and run the tests defined in src/test/java
.
Step 5: Package the Application
To package your application into a JAR file, run:
mvn package
This will create a JAR file in the target
directory.
Step 6: Maven Clean
The mvn clean
command is used to remove all files generated by the previous build. This includes the target
directory where Maven stores compiled classes, JAR files, and other build artifacts. Essentially, mvn clean
helps to start your build from a fresh state, ensuring no remnants from previous builds interfere with the current one.
Open your terminal, navigate to your Maven project directory, and run:
mvn clean
This command deletes the target
directory and all its contents.
Step 7: Maven Clean Package
The mvn clean package
command is a combination of two phases: clean
and package
. It first executes the clean
phase to remove any previous build artifacts and then proceeds to the package
phase to compile your code, run tests, and package the application into its distributable format (e.g., a JAR or WAR file).
Open your terminal, navigate to your Maven project directory, and run:
mvn clean package
This command performs the following steps:
Clean: Deletes the
target
directory.Validate: Checks if the project is correct and all necessary information is available.
Compile: Compiles the source code of the project.
Test: Runs the tests using a suitable unit testing framework.
Package: Packages the compiled code into a distributable format like JAR or WAR.
Step 8: Deploy
Run the application by running the jar file as below
java -jar target/spring-boot-web.jar
Step 9: Stop the Application
You can simply stop it by clicking ctrl+c on the same window (zoom to see the details)
The website is unreachable now!
Now you can host any website of your choice using Maven!
Conclusion
Maven is an essential tool for Java development in a DevOps environment, providing automation and consistency throughout the build and deployment process. By following this hands-on project, you've learned how to create a Maven project, manage dependencies, build your application, and integrate it into a CI/CD pipeline. Keep exploring and integrating more DevOps practices to streamline your development workflow!
Thanks for spending your valuable time in learning to enhance your career!ππ
Follow me on
Hashnode:kshitijaa.hashnode.dev
LinkedIn:https://www.linkedin.com/in/kshitija-bartakke-malwade-39678b141/