Java-项目管理:Maven基础

Maven是一个基于项目对象模型(POM)的自动化构建工具,使项目构建标准化,可以解决依赖管理等问题。

  • 构建:把项目进行编译、测试、打包、部署进行运行的过程。
  • 项目对象模型(POM):定义项目的基本信息,描述项目如何构建、声明依赖等等
  • Archetype、原型模板:用于快速创建Maven项目
    • maven-archetype-quickstart:普通java项目
    • maven-archetype-webapp:java web项目

标准目录结构

image

根目录:pom.xml所在目录 

src/main/java:项目源代码

src/main/resources:项目资源、配置文件

src/test/java:测试源代码

src/test/resources:测试资源、配置文件

 

target/lib:项目运行时依赖的包

target/classes:项目源代码编译编译结果

target/test-classes目录:测试源代码编译结果

target/surefire-reports目录:测试报告

1、Maven的项目配置文件:pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- 1. 项目坐标:唯一标识 -->
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.test</groupId>     <!-- 组织/公司域名倒写 -->
    <artifactId>my-test-project</artifactId> <!-- 项目名称 -->
    <version>1.0-SNAPSHOT</version>         <!-- 版本号 -->

    <!-- 2. 属性配置(可选,用于统一版本) -->
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- 3. 核心:依赖管理 -->
    <dependencies>
        <!-- 添加JUnit 5依赖,用于测试 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.2</version>
            <scope>test</scope> <!-- 只在测试时生效 -->
        </dependency>
        <!-- 添加日志依赖 -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.4.5</version>
        </dependency>
    </dependencies>
</project>
项目坐标:唯一标识
--用于确定唯一的项目
<groupId>:项目组/公司标识,通常是域名反写、比如com.dmall
<artifactId>:项目标识,比如dmall-coupon
<version>:项目版本,比如0.0.1-SNAPSHOT
<packaging>jar</packaging>:项目的打包方式,默认为jar
--jar包:压缩包,对项目的class文件和资源文件进行打包,方便其它人使用
META-INF\MANIFEST.MF:详情单,对jar包的内容进行说明

Manifest-Version: 版本
Created-By: 创建人
Class-Path:jar包依赖类的路径
Main-Class:包含main方法的类,执行时从这个类的main()开始运行

 

 依赖管理、<dependency> 依赖重复:引入路径短者优先,先声明者优先
<scope>:定义依赖的作用范围

1、compile:编译、测试、运行都有效,默认值
2、provided:编译、测试有效,运行时不打包
3、runtime:测试、运行有效
4、test:只在测试有效
配置文件、<profile>

作用:使不同的环境可以使用不同的配置文件来进行构建

        <!-- Profile 1: 开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
                <env.active>dev</env.active> <!--定义属性:env.active = dev -->
                <db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
                <db.username>dev_user</db.username>
                <db.password>dev_pass</db.password>
                <logging.level>DEBUG</logging.level>
            </properties>
            <!-- 可以在此添加特定依赖,如开发工具 -->
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                </dependency>
            </dependencies>
            <!-- 默认激活此 profile -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
插件、<plugin>

作用:Maven 本身只提供了基本的构建生命周期,实际工作(编译、测试、打包)都通过插件完成。

<plugin>示例见第四部分。

插件作用典型场景
maven-compiler-plugin 配置 Java 编译版本 确保项目使用正确的 JDK 版本编译
maven-surefire-plugin 执行单元测试(JUnit/TestNG) 运行 mvn test 时自动执行测试用例
maven-failsafe-plugin 执行集成测试 区分单元测试和集成测试
maven-assembly-plugin 打包成可执行 Jar(含依赖) 生成一个可直接运行的 .jar 文件
maven-surefire-report-plugin 生成测试报告 生成 HTML 格式的测试执行报告

2、 常用Maven命令(在项目根目录执行)

  • -D:指定Java全局属性,比如-Dmaven.test.skip=true,跳过test阶段
  • -P:指定Profile,比如-Ptest、表示使用id为test的Profile
命令作用
mvn clean 删除target目录(编译产物)
mvn compile 编译src/main/java下的代码
mvn test

执行src/test/java下的测试用例(包含test-compile、test)

测试失败时,默认会停止构建并输出失败信息

mvn package 打包项目成jar或war
mvn install 打包 + 安装到本地仓库(供其他项目依赖)
mvn deploy
打包并将打包文件上传到远程仓库

image

3、Maven的配置文件:settings.xml

作用:设置本地仓库、私人远程仓库、镜像等、

  • 用户级:~/.m2/settings.xml
  • 全局级:$MAVEN_HOME/conf/settings.xml
  • 查找过程:本地仓库-->Maven官方的中央仓库-->私人远程仓库,找到后下载到本地仓库进行使用。
定义本地仓库

<localRepository>xx</localRepository>

默认:C:\${user.home}\.m2\repository

定义远程私人仓库
<repositories>
        <repository>
            <id>nexus</id> <!--不能与镜像ID重复-->
            <name>Team Nexus Repository</name>
            <url>http://xiaoyaziyun.com:8081/nexus/content/groups/public</url>
        </repository>
 </repositories>
定义镜像
<mirror>
    <id>aliyun</id> <!--镜像的唯一标识-->
    <mirrorOf>central</mirrorOf> <!--中央仓库的镜像,中央仓库的请求都会转到该镜像-->
    <name>aliyun</name>
    <url>https://maven.aliyun.com/repository/public</url> <!--镜像的地址-->
</mirror>

1、作用:拦截对于指定仓库的请求,并重定向到对应镜像地址

4、Maven的插件配置:<plugins>

<build>
    <plugins>
        <!-- 1. 编译器插件:指定 Java 编译版本 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>11</source>   <!-- 源代码使用的 Java 版本 -->
                <target>11</target>   <!-- 生成字节码的 Java 版本 -->
                <encoding>UTF-8</encoding> <!-- 编码格式 -->
            </configuration>
        </plugin>

        <!-- 2. Surefire 插件:执行测试用例(JUnit/TestNG) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <configuration>
                <!-- 指定测试类命名规则,根据命名规则查找测试类 -->
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                </includes>
                <!-- 是否跳过测试(不推荐,但有时本地调试会用) -->
                <skipTests>false</skipTests>
                <!-- 测试失败时是否继续构建 -->
                <testFailureIgnore>false</testFailureIgnore>
            </configuration>
        </plugin>

        <!-- 3. 打包插件:将项目打成可执行 Jar(包含所有依赖) -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.5.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <!-- 指定主类入口 -->
                        <mainClass>com.example.MainApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> <!-- 绑定到 package 阶段 -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 5、项目继承和集中管理:父POM

父POM的作用:父POM是所有子模块的“模板”,可以统一管理整个项目组的公共配置

父POM通过两种方式发挥作用:

  1. 继承(Inheritance):子模块的POM通过 <parent> 继承父POM的配置。这避免了在每个子模块中重复声明相同的依赖版本、插件配置等。

  2. 聚合(Aggregation):父POM通过 <modules> 列出所有子模块。

    1. 在父POM目录下执行 mvn clean install,Maven会按顺序构建所有子模块,实现“一键构建整个项目”。

父POM示例

<!-- 父POM:my-parent-project/pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- 父项目自身的坐标 -->
    <groupId>com.example</groupId>
    <artifactId>my-parent-project</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging> <!-- 父项目的打包方式必须是 pom -->

    <!-- 1. 统一管理依赖版本(子项目继承) -->
    <properties>
        <java.version>11</java.version>
        <junit.version>5.9.2</junit.version>
        <logback.version>1.4.5</logback.version>
    </properties>

    <!-- 2. 公共依赖(所有子模块默认继承) -->
    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
    </dependencies>

    <!-- 3. 依赖版本管理(子模块可选择使用,但版本由父统一控制) -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 4. 聚合:声明所有子模块 -->
    <modules>
        <module>service-a</module>
        <module>service-b</module>
    </modules>

    <!-- 5. 公共插件配置(子模块继承) -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

继承示例:

子模块的POM通过 <parent> 标签来引用父POM

<!-- 子模块:my-parent-project/service-a/pom.xml -->
<project ...>
    <modelVersion>4.0.0</modelVersion>

    <!-- 1. 继承父POM -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>my-parent-project</artifactId>
        <version>1.0.0</version>
        <relativePath>../pom.xml</relativePath> <!-- 父POM的相对路径 -->
    </parent>

    <!-- 2. 子模块自己的坐标(groupId和version可以继承,只需指定artifactId)-->
    <artifactId>service-a</artifactId>
    <!-- packaging默认为jar,无需特别声明 -->

    <!-- 3. 子模块特有依赖 -->
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
    </dependencies>
</project>

1、在父POM<dependencyManagement>声明的依赖,需要子模块单独声明后才会引入

--可省略version和scope,使用<dependencyManagement>中指定的version和scope

2、在父POM<dependencies>声明的依赖,所有子模块会自动引入

posted @ 2026-08-17 11:30  Fēngwèi  阅读(5)  评论(0)    收藏  举报