Dive Into Maven: From Basics to Building Your First Project

Dive Into Maven: From Basics to Building Your First Project

For anyone stepping into the world of Java development or DevOps, Maven is a tool you can’t ignore. It's not just a build tool - it's a complete project management system. Let’s decode Maven, understand its core components, and walk through creating a Maven project from scratch.

What is an Artifact?

In Maven, an Artifact is the final output of your build process - usually a .jar, .war, or .ear file. It's the packaged version of your project that can be deployed or shared.

Key Maven Identifiers:

  • Group ID: Represents the project’s structure or organization.

Example:

Commercial App: com.linkedin

Government App: gov.edudept

Organization App: org.apache

  • Artifact ID: The unique name for your project.

Example: weblinkedinapp

  • Version: Denotes the current version of the project (e.g., 1.0.0).

Together, these form the Maven Coordinate: com.icicibank:webappicicibank:1.0.0

Step-by-Step: Creating a Maven Project

  1. Run the Maven archetype command:

mvn archetype:generate        

2. Select the project type:

(Maven lists template numbers)

  • 1740 – Spring Project
  • 2208 – Sample Maven Project
  • 1244 – MVC 4 Web App
  • 2630 – Multi-platform (Web, Android, iOS)

? Select a number (e.g., 2208 for sample project) and hit enter.

3. Choose the version:

Stick with the default for most cases (e.g., version 7).

4. Define Project Details:

  • Group ID: com.linkedin
  • Artifact ID: weblinkedinapp

5. BUILD SUCCESS — Your project is now created!

Project Structure Breakdown:

  • src/main/java → Main application code
  • src/test/java → Unit test code
  • pom.xml → The heart of the project (dependencies, build config)

Managing Dependencies:

  • Visit Maven Central Repository
  • Search for required APIs (e.g., MySQL or Google Maps)
  • Copy the <dependency> tag and paste it into your pom.xml

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>        

  • Run:

mvn compile        

? Dependencies get downloaded to the local repository.

Compiling & Testing the Code:

  • Compile Java Code:

mvn compile        

? Creates a target folder with .class files.

  • Run Unit Tests:

mvn test        

? Compiled test classes go into target/test-classes.

Creating the Artifact (JAR/WAR):

mvn package        

? The final artifact gets stored in the target folder.

Mastering Maven is a key step for Java developers and DevOps enthusiasts. Its simplicity in managing dependencies, packaging code, and integrating with CI/CD pipelines makes it a powerhouse tool.

?? Are you using Maven in your workflows? Or have you explored alternatives like Gradle? Drop your experiences in the comments!


Pierre DELOINCE

Administrateur Système DevOps (RNCP niv 6). Dipl?me obtenu chez Simplon en février 2025

1 个月

Très informatif

Karan Singh Rajawat ??

DevOps Engineer | Linux | Git | Maven | Terraform | Docker | Jenkins | GitLab | AWS DevOps 3X AWS Certified | 22X AWS Digital Badges | IT Specialist | AI Learner

1 个月

Exploring Maven was a game-changer for me! Whether you're a beginner or scaling projects, Maven simplifies builds, manages dependencies, and boosts productivity.

回复

要查看或添加评论,请登录

Karan Singh Rajawat ??的更多文章

社区洞察

其他会员也浏览了