1. 模塊化開發
模塊化開發是將大型應用程序拆分成多個小模塊的過程,每個模塊負責不同的功能。這有助于降低系統復雜性,提高代碼的可維護性和可擴展性。
2. 聚合模塊
聚合模塊(父模塊)用于組織和管理多個子模塊。它定義了項目的全局配置,如依賴、插件等,使得子模塊可以繼承這些配置。
示例代碼
父模塊 pom.xml
:
<project><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>parent-module</artifactId><packaging>pom</packaging><modules><module>module-a</module><module>module-b</module></modules><properties><java.version>1.8</java.version><spring.version>5.3.10</spring.version></properties><dependencyManagement><dependencies><!-- 定義版本號,子模塊繼承后無需再定義版本號 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${spring.version}</version></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
子模塊 module-a/pom.xml
:
<project><parent><groupId>com.example</groupId><artifactId>parent-module</artifactId></parent><modelVersion>4.0.0</modelVersion><artifactId>module-a</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>
3. Maven 插件管理
在父模塊中定義 <pluginManagement>
,子模塊可以引用這些插件,而不需要重復定義插件版本。
父模塊 pom.xml
:
<pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.4.5</version></plugin></plugins>
</pluginManagement>
子模塊將自動繼承這些插件配置。
4. 自定義屬性
在父模塊中定義 <properties>
標簽,可以在所有子模塊中使用這些屬性。
父模塊 pom.xml
:
<properties><project.basedir>${project.basedir}</project.basedir>
<properties>
子模塊 module-a/pom.xml
:
<build><resources><resource><directory>${project.basedir}/src/main/resources</directory><filtering>false</filtering></resource></resources>
</build>
5. 版本管理
在父模塊中統一管理依賴的版本號。
父模塊 pom.xml
:
<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${spring.version}</version></dependency></dependencies>
</dependencyManagement>
6. 多環境配置
通過定義不同的配置文件,可以適應不同的運行環境。
父模塊 pom.xml
:
<profiles><profile><id>dev</id><properties><active.profile>dev</active.profile></properties></profile>
</profiles>
子模塊 application-dev.yml
:
server:port: 8081
spring:datasource:url: jdbc:mysql://localhost:3306/dev_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false
7. 跳過測試
在 Maven 中,可以通過配置 maven-surefire-plugin
來跳過測試。
父模塊 pom.xml
:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-sure-plugin</artifactId><configuration><skipTests>true</skipTests></configuration></plugin></plugins>
</build>
8. 私服配置
在 Maven 中配置私服(如 Nexus)可以方便管理依賴和插件。
settings.xml:
<servers><server><id>nexus-releases</id><username>admin</username><password>admin123</password></server>
</servers>
pom.xml:
<repositories><repository><id>nexus-releases</id><url><http://localhost:8081/nexus/content/repositories/releases/></url></repository>
<repositories>
9. 資源上傳
將構建的 JAR 文件上傳到私服,可以在 pom.xml
中配置 maven-deploy-plugin
插件。
pom.xml:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-deploy-plugin</artifactId><configuration><repositoryId>nexus-releases</repositoryId><url><http://localhost:8081/nexus/content/repositories/releases/></url></configuration></plugin></plugins>
</build>
總結
模塊化開發和 Maven 的使用可以提高項目的可維護性和可擴展性。通過聚合和繼承,可以簡化項目的構建和配置管理。同時,Maven 提供的多環境配置、跳過測試、私服配置等功能,使得項目可以更好地適應不同的開發和運行環境。