Understanding of Mavne Structure


Table of Contents
  1. Introduction to Maven
  2. What Maven Provides
  3. Maven Project Structure
  4. POM Types
  5. Maven Build Lifecycle (Phases with Spring Boot Examples)
  6. Key Points to Remember

Introduction to Maven

What Maven Provides

Maven Project Structure
my-springboot-app/
 ├── pom.xml                 (Project Object Model)
 └── src
     ├── main
     │   ├── java            (Spring Boot source code)
     │   │    └── com/example/DemoApplication.java
     │   └── resources       (application.properties, static/, templates/)
     └── test
         ├── java            (Unit & Integration tests)
         └── resources

POM Types
<!-- Example Spring Boot Parent POM -->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.1.0</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
  <!-- Spring Boot Starter Web -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <!-- Testing -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

Maven Build Lifecycle (Phases with Spring Boot Examples)
✅ Phase 1: validate
# Validate project
mvn validate
<plugin>
  <groupId>com.diffplug.spotless</groupId>
  <artifactId>spotless-maven-plugin</artifactId>
  <version>2.38.0</version>
  <executions>
    <execution>
      <phase>validate</phase>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

✅ Phase 2: compile
# Compile sources
mvn compile
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.11.0</version>
  <configuration>
    <source>17</source>
    <target>17</target>
  </configuration>
</plugin>

✅ Phase 3: test
# Run unit tests
mvn test
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.1.2</version>
</plugin>

✅ Phase 4: package
# Create executable jar
mvn package
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

✅ Phase 5: verify
# Run static analysis
mvn verify
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-pmd-plugin</artifactId>
  <version>3.21.2</version>
  <executions>
    <execution>
      <phase>verify</phase>
      <goals>
        <goal>check</goal>
      </goals>
    </execution>
  </executions>
</plugin>

✅ Phase 6: install
# Install in local repo
mvn install
<dependency>
  <groupId>com.example</groupId>
  <artifactId>my-springboot-app</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

✅ Phase 7: deploy
# Deploy to remote repo
mvn deploy
<distributionManagement>
  <repository>
    <id>company-releases</id>
    <url>https://nexus.company.com/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>company-snapshots</id>
    <url>https://nexus.company.com/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

Key Points to Remember