Maven 多模块与私服
多模块项目
大型项目拆分为多个子模块,方便管理和复用。
父 POM
<!-- parent/pom.xml -->
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging> <!-- 父模块必须是 pom 类型 -->
<modules>
<module>my-common</module>
<module>my-service</module>
<module>my-web</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.5</version>
<type>pom</type>
<scope>import</scope> <!-- BOM 导入 -->
</dependency>
</dependencies>
</dependencyManagement>
子模块
<!-- my-web/pom.xml -->
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>my-web</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-service</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
dependencyManagement vs dependencies
| 特性 | dependencyManagement | dependencies |
|---|---|---|
| 作用 | 声明版本号 | 实际引入依赖 |
| 子模块是否需要显式依赖 | 需要 | 不需要 |
| 典型场景 | 父 POM 统一版本 | 真正需要的模块 |
Nexus 私服
部署
docker run -d -p 8081:8081 --name nexus sonatype/nexus3
# 初始密码在 /nexus-data/admin.password
配置 settings.xml
<settings>
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>${NEXUS_PASSWORD}</password>
</server>
</servers>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
</settings>
发布到私服
<!-- pom.xml -->
<distributionManagement>
<repository>
<id>nexus-releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
mvn deploy # 发布到私服