Skip to content

01-Maven 高级

对应原始资料:maven高级

一、依赖范围(scope)

scope主代码测试代码打包典型
compile(默认)spring-core
provided×servlet-api(Tomcat 提供)
runtime×mysql-connector(运行时反射)
test××junit
system×本地 jar(不推荐)

二、依赖传递与排除

1. 传递性

A 依赖 B(compile),B 依赖 C(compile),则 A 自动拥有 C。

  • compile 传递 compile。
  • provided/test 不传递。

2. 依赖冲突

路径最短者优先;路径相同则先声明者优先。

3. 排除依赖

xml
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <exclusions>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

三、版本统一管理

xml
<properties>
    <spring.version>5.3.20</spring.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

四、分模块开发

把大型项目拆成多个 Maven 模块(聚合):

mall-parent
├── mall-common      通用工具
├── mall-domain      实体类
├── mall-dao         数据访问
├── mall-service     业务
└── mall-web         Web 启动

1. 聚合(父 pom)

xml
<packaging>pom</packaging>
<modules>
    <module>mall-common</module>
    <module>mall-web</module>
</modules>

2. 继承(子 pom)

xml
<parent>
    <groupId>com.itheima</groupId>
    <artifactId>mall-parent</artifactId>
    <version>1.0</version>
</parent>

3. dependencyManagement

父 pom 中统一管理版本,子模块只声明 groupId/artifactId,不写 version:

xml
<!-- 父 -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<!-- 子 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <!-- 不写版本 -->
</dependency>

五、私服 Nexus

1. 仓库类型

  • 宿主仓库 hosted:存放团队自己开发的 jar(releases / snapshots)。
  • 代理仓库 proxy:代理中央仓库,缓存下载。
  • 仓库组 group:组合多个仓库的访问入口。

2. 配置 settings.xml

xml
<servers>
    <server>
        <id>nexus-releases</id>
        <username>admin</username>
        <password>123456</password>
    </server>
</servers>

<mirrors>
    <mirror>
        <id>aliyunmaven</id>
        <mirrorOf>central</mirrorOf>
        <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
</mirrors>

3. 发布 jar 到私服

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

六、Maven 生命周期与命令

命令阶段说明
cleanclean删 target
compilecompile编译主代码
testtest跑单元测试
packagepackage打 jar/war
installinstall装到本地仓库
deploydeploy发到私服

完整生命周期:clean → validate → compile → test → package → verify → install → deploy → site

七、打包插件

打可执行 jar(含依赖):

xml
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

练习建议

  1. 配置阿里云镜像加速依赖下载。
  2. 把一个练习项目拆成多模块(common + main)。
  3. 在 pom 中用 properties 统一管理版本。
  4. dependency:tree 分析项目依赖结构。