Understanding of Mavne Structure
Introduction to Maven
- Maven is a build automation and project management tool for Java & Spring Boot applications.
- It uses POM (Project Object Model) to define project configuration.
- Maven → defines what to do.
- Ant → defines what to do and how to do.
- When you run a Maven command, it looks for pom.xml in the current directory.
What Maven Provides
- Build generation → Compile, test, package Spring Boot JAR/WAR.
- Dependency resolution → Downloads Spring Boot starter dependencies.
- Documentation → Generates Javadocs, project reports.
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
- src/main/java → Application code (Spring Boot controllers, services, repos).
- src/main/resources → Config (application.yml, logback.xml, static files).
- src/test/java → Unit tests using JUnit/Mockito.
POM Types
- Parent POM: Common configs for multi-module Spring Boot projects.
- Super POM: Default Maven POM (all projects inherit from it implicitly).
- Repositories:
- Local repo → ~/.m2/repository
- Remote repo → Central, Nexus, Artifactory
- Dependencies: Libraries required by the app.
<!-- 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)
- Maven lifecycle = sequence of phases.
- Running a phase executes all previous phases.
- Each phase has goals (tasks) controlled by plugins.
✅ Phase 1: validate
- Ensures project structure is valid.
- In Spring Boot → can run Checkstyle/Spotless plugin for code style validation.
# 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
- Compiles Spring Boot application source code (src/main/java).
- Output → target/classes.
# 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
- Runs JUnit/Mockito/Spring Boot tests in src/test/java.
- Uses Surefire Plugin.
# 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
- Creates a JAR/WAR of the Spring Boot app.
- Spring Boot uses spring-boot-maven-plugin for executable JARs.
# Create executable jar
mvn package
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
✅ Phase 5: verify
- Runs integration tests, code quality checks (PMD/SpotBugs).
# 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
- Installs Spring Boot JAR into local repo (~/.m2/repository).
- Other local projects can depend on it.
# 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
- Deploys Spring Boot artifact to remote repo (Nexus, Artifactory).
- Used for CI/CD pipelines in enterprises.
# 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
- Run mvn test → validate → compile → test (all previous phases included).
- Goal = task (e.g., compiler:compile, spring-boot:run).
- settings.xml → Used to define:
- Local repo path
- Remote server credentials
- Proxies