Introduction to Maven
Maven is a project management tool that abstracts development processes into a Project Object Model (POM). It addresses common issues like jar version conflicts and complex project upgrades by providing standardized dependency management and build automation.
Key Functions
- Build Automation: Standardized cross-platform project building
- Dependency Management: Resolves version conflicts and manages resources
- Standard Structure: Defines consistent project directories:
src/main/java- Application source codesrc/main/resources- Configuration filessrc/test/java- Test source code
Environment Setup
Installation
- Download from official site
- Extract archive to installation directory (e.g.,
D:\apache-maven)
Configuration
- Set
MAVEN_HOMEenvironment varible to installation path - Add
%MAVEN_HOME%\binto systemPATH - Verify with terminal:
mvn -v
Core Concepts
Repositories
- Local: Developer machine cache (
~/.m2/repository) - Central: Public repository hosted by Apache
- Private: Organization-specific repository
Coordinates
Uniquely identify resources in repositories:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.8</version>
</dependency>
Repository Configuration
Modify conf/settings.xml:
<localRepository>D:/custom-repo</localRepository>
<mirror>
<id>aliyun-mirror</id>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
Project Creation
Manual Setup
- Create standard directory structure
- Add sample class: ```
package com.example;
public class Sample {
public String greet(String name) {
return "Hello " + name;
}
}
- Create test class: ```
package com.example;
import org.junit.Test;
public class SampleTest {
@Test
public void testGreet() {
Sample s = new Sample();
Assert.assertEquals("Hello world", s.greet("world"));
}
}
- Configure
pom.xml: ``` <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo-app</artifactId> 1.0.0 <groupId>junit</groupId> <artifactId>junit</artifactId> 4.13.2
IntelliJ Setup
- Create new project: File → New → Project → Maven
- Configure cooordinates in POM
- Add dependencies in
<dependencies>section - For web projects: ```
war
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
2.2
Dependency Management
Transitivity Rules
- Path Priority: Closer dependencies override distant ones
- Declaration Order: First declarasion takes precedence
Scopes
| Scope | Main | Test | Package |
|---|---|---|---|
| compile | ✓ | ✓ | ✓ |
| test | ✗ | ✓ | ✗ |
| provided | ✓ | ✓ | ✗ |
Lifecycle and Plugins
Build Lifecycles
- clean: Removes build artifacts
- default: Core build process (compile, test, package)
- site: Generates project documentation
Plugin Configuration
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>