Cấu hình Maven chi tiết
Maven là một công cụ quản lý dự án và xây dựng tự động phổ biến trong thế giới Java. Hiểu rõ cách cấu hình Maven là rất quan trọng để tối ưu hóa quy trình phát triển. Bài viết này sẽ đi sâu vào các tệp cấu hình chính của Maven và cách sử dụng chúng hiệu quả.
Tệp cấu hình chính
Maven sử dụng hai tệp cấu hình chính:
- settings.xml: Tệp cấu hình toàn cục, thường nằm trong thư mục conf của thư mục cài đặt Maven
- pom.xml: Tệp cấu hình cho từng dự án cụ thể, nằm trong thư mục gốc của dự án
Có thể có một tệp settings.xml khác trong thư mục .m2 của người dùng, chứa các cấu hình dành riêng cho người dùng đó. Quyền ưu tiên cấu hình là: pom.xml > user settings > global settings.
Cấu hình settings.xml
Kho lưu trữ cục bộ (Local Repository)
<localRepository>D:\maven_repository</localRepository>
Chế độ tương tác (Interactive Mode)
<interactiveMode>true</interactiveMode>
Chế độ ngoại tuyến (Offline Mode)
<offline>false</offline>
Nhóm plugin
Định nghĩa các nhóm plugin mà Maven sẽ sử dụng khi không có groupId được cung cấp trong lệnh.
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
Cấu hình máy chủ (Servers)
Cấu hình thông tin đăng nhập cho các máy chủ lưu trữ riêng tư.
<servers>
<server>
<id>private-repo</id>
<username>dev</username>
<password>secure123</password>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
</server>
</servers>
Cấu hình mirror
Sử dụng mirror để thay thế các kho lưu trữ từ xa.
<mirror>
<id>aliyun-mirror</id>
<name>Aliyun Maven Repository</name>
<url>https://maven.aliyun.com/repository/public</url>
<mirrorOf>*</mirrorOf>
</mirror>
Profiles
Profiles cho phép cấu hình linh hoạt dựa trên các điều kiện khác nhau.
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>dev-repo</id>
<url>https://dev.repo.example.com</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
</profiles>
Active Profiles
Xác định các profile nào sẽ được kích hoạt mặc định.
<activeProfiles>
<activeProfile>development</activeProfile>
</activeProfiles>
Cấu hình pom.xml
Thông tin dự án cơ bản
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
Cấu hình thuộc tính
Định nghĩa các thuộc tính có thể tái sử dụng trong tệp cấu hình.
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Quản lý dependency
Cấu hình dependency cho dự án.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.10</version>
<scope>compile</scope>
</dependency>
</dependencies>
Quản lý dependency (Dependency Management)
Định nghĩa các dependency mà không thực sự thêm vào dự án.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Xây dựng và plugin
Cấu hình các plugin cho quá trình xây dựng.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
Kho lưu trữ từ xa (Repositories)
Cấu hình các kho lưu trữ từ xa cho dự án.
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
Phân phối (Distribution Management)
Cấu hình nơi để triển khai các sản phẩm đã build.
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>https://repo.example.com/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Internal Snapshots</name>
<url>https://repo.example.com/snapshots</url>
</snapshotRepository>
</distributionManagement>
Profiles trong pom.xml
Cấu hình các profile cho dự án.
<profiles>
<profile>
<id>production</id>
<properties>
<environment>prod</environment>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
Thứ tự tải dependency
- Kho lưu trữ cục bộ (local repository)
- Kho lưu trữ từ xa được cấu hình trong settings.xml
- Kho lưu trữ từ xa được cấu hình trong pom.xml
- Kho lưu trữ trung tâm của Maven
Mirror có thể thay thế các kho lưu trữ khác. Nếu một kho lưu trữ có mirror, Maven sẽ tải từ mirror thay vì kho lưu trữ gốc.