使用maven快速入门
Maven 基础知识
官网: 传送门
Maven 项目结构
$ MavenProject
|-- pom.xml
|-- src
| |-- main
| | `-- java
| | `-- resources
| `-- test
| | `-- java
| | `-- resources
`-- README.md
POM文件
- POM文件代表 工程对象模型(Project Object Model)它是使用Maven工作的基本组件,位于工程根目录。
- POM文件支持继承
<?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>
<packaging>pom</packaging>
<modules>
<module>mscx-ad-discovery</module>
<module>mscx-ad-zuul</module>
<module>mscx-ad-gateway</module>
<module>mscx-ad-discovery-nacos</module>
<module>mscx-ad-common</module>
<module>mscx-ad-db</module>
<module>mscx-ad-sponsor</module>
<module>mscx-ad-search</module>
<module>mscx-ad-feign-sdk</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxzhongf</groupId>
<artifactId>mscx-ad</artifactId>
<version>1.0-SNAPSHOT</version>
<name>分布式广告系统</name>
<description>基于Spring Cloud Alibaba 实现的分布式广告系统</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--定义远程maven仓库-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>alibaba</id>
<name>ali Milestones</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Maven 坐标
Maven Dependency
Maven思想 是 约定大于配置,默认依赖中,scope 是compile.
Scope类型
-
compile(会被打包到当前project)
表示被依赖的package参与当前project的编译,包含后续的测试,运行周期都会参与,是一个强依赖。
-
test
表示被依赖的jar 仅参与测试相关的处理,包裹测试代码的编译,执行。(如junit)
-
runtime
表示被依赖的jar不需要参与项目的编译,但是后期的测试和运行周期需要参与。
-
provided
打包的时候不需要包含进去,其他的Container会提供该依赖支持,理论上该依赖可以参与编译、测试运行等周期 ,相当于compile,但是在打包阶段做了exclude命令。
-
system
从参与环境来看,和provided相同,但是被依赖项不会从maven仓库获取,而是从本地文件系统获取,一定需要配合
systemPath
属性使用 -
import
This scope is only supported on a dependency of type
pom
in the<dependencyManagement>
section.
依赖传递特性
官方解释:传送门
-
Dependency mediation (最近依赖原则)
"nearest definition" means that the version used will be the closest one to your project in the tree of dependencies. For example, if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0.
根据依赖深度,选择依赖路径最近的package version, 如果依赖深度相同,那么选择前一个。
Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.
-
Dependency management
依赖管理(项目作者可直接指定依赖版本)
-
Dependency scope 如上一节所述
-
Excluded dependencies 排除依赖包中依赖项
-
Optional dependencies (相当于设置不允许将该依赖传递下去)
常用命令
[奔跑的人生] | [segmentfault] | [spring4all] | [csdn] | [掘金] | [OSChina] | [简书] | [知乎] | [51CTO]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?