当前位置: 首页 > ops >正文

Maven的使用

Maven 是 Java 项目中广泛使用的构建工具,它可以帮助你管理项目依赖、编译代码、运行测试、打包和部署应用。下面我将从简单到复杂介绍 Maven 的使用。

1. 简单使用:基本项目结构和 POM 文件

首先,让我们看一个最简单的 Maven 项目结构和 pom.xml 文件:

my-project/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/
│   │           └── example/
│   │               └── App.java
│   └── test/
│       └── java/
│           └── com/
│               └── example/
│                   └── AppTest.java
└── pom.xml

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"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-project</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency></dependencies>
</project>

2. 添加依赖管理

Maven 的核心优势之一是依赖管理。你可以在 dependencies 部分添加项目需要的库:

<dependencies><!-- JUnit 测试框架 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- Apache HttpClient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version></dependency><!-- Jackson JSON 处理 --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.0</version></dependency>
</dependencies>

3. 使用插件

Maven 插件可以扩展项目的功能,比如编译代码、运行测试、打包应用等:

<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><!-- 运行应用插件 --><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.6.0</version><executions><execution><goals><goal>java</goal></goals></execution></executions><configuration><mainClass>com.example.App</mainClass></configuration></plugin><!-- JAR 打包插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><archive><manifest><mainClass>com.example.App</mainClass></manifest></archive></configuration></plugin></plugins>
</build>

4. 多模块项目

对于大型项目,你可以将其拆分为多个模块:

my-project/
├── pom.xml (父项目)
├── module1/
│   ├── pom.xml
│   └── src/
└── module2/├── pom.xml└── src/

父项目的 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"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>my-project</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>module1</module><module>module2</module></modules><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target></properties><dependencyManagement><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- 其他依赖 --></dependencies></dependencyManagement>
</project>

子模块的 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"><modelVersion>4.0.0</modelVersion><parent><groupId>com.example</groupId><artifactId>my-project</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>module1</artifactId><dependencies><!-- 继承父项目的依赖管理 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><!-- 模块特定的依赖 --><dependency><groupId>com.example</groupId><artifactId>module2</artifactId><version>${project.version}</version></dependency></dependencies>
</project>

5. 配置文件和资源

你可以在 src/main/resources 和 src/test/resources 目录下放置配置文件:

<build><resources><resource><directory>src/main/resources</directory><filtering>true</filtering><includes><include>**/*.properties</include><include>**/*.xml</include></includes></resource></resources><testResources><testResource><directory>src/test/resources</directory><filtering>true</filtering></testResource></testResources>
</build>

6. 生命周期和命令

Maven 项目有一个标准的生命周期,常用命令包括:

# 清理项目
mvn clean# 编译源代码
mvn compile# 编译测试代码
mvn test-compile# 运行测试
mvn test# 打包项目
mvn package# 安装到本地仓库
mvn install# 部署到远程仓库
mvn deploy

7. 高级配置: profiles 和 properties

你可以使用 profiles 来针对不同环境配置项目:

<profiles><profile><id>development</id><properties><db.url>jdbc:mysql://localhost:3306/devdb</db.url><db.username>devuser</db.username><db.password>devpass</db.password></properties></profile><profile><id>production</id><properties><db.url>jdbc:mysql://prod-server:3306/proddb</db.url><db.username>produser</db.username><db.password>prodpass</db.password></properties></profile>
</profiles>

使用特定 profile 运行命令:

mvn package -P development

以上是 Maven 的基本到高级使用方法,你可以根据项目需求选择合适的配置方式。

http://www.xdnf.cn/news/12708.html

相关文章:

  • Mac M芯片 RAG 极简流程 安装 ragflow + LM studio
  • Java 高级泛型实战:8 个场景化编程技巧
  • 0x-4-Oracle 23 ai-sqlcl 25.1.1 独立安装-配置和优化
  • OD 算法题 B卷【正整数到Excel编号之间的转换】
  • Web后端开发(请求、响应)
  • SpringCloud2025+SpringBoot3.5.0+gateway+webflux子服务路由报503
  • Pinocchio 库详解及其在足式机器人上的应用
  • 板凳-------Mysql cookbook学习 (十--2)
  • Linux权限探秘:驾驭权限模型,筑牢系统安全
  • 【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
  • 2025新高考二卷选择题第一题题解
  • 嵌入式全栈面试指南:TCP/IP、C 语言基础、STM32 外设与 RT‑Thread
  • MATLAB遍历生成20到1000个节点的无线通信网络拓扑推理数据
  • 大实验:基于赛灵思csg324100T,pmodMAXsonar的危险距离警报
  • [论文阅读] 人工智能+软件工程 | 结对编程中的知识转移新图景
  • 基于贝叶斯网络构建结构方程_TomatoSCI分析日记
  • Qwen系列之Qwen3解读:最强开源模型的细节拆解
  • 计数排序_桶排序
  • 从 Vue 2.0 进阶到 Vue 3.0 的核心技术解析指南
  • **解锁 C++ std::map 的力量**
  • android 布局小知识点 随记
  • OpenEuler服务器警告邮件自动化发送:原理、配置与安全实践
  • 数据的输出、输入
  • 20242817李臻-安全文件传输系统-项目验收
  • springboot2.x升级springboot3.x
  • 端午编程小游戏--艾草驱邪
  • 【SpringBoot自动化部署方法】
  • UDP与TCP通信协议技术解析
  • XXL-JOB——源码分析解读(1)
  • 英语词汇表格 form, table, sheet, grid, tabulation 的区别