前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用maven快速入门

使用maven快速入门

作者头像
Isaac Zhang
发布2019-09-10 18:03:20
4280
发布2019-09-10 18:03:20
举报
文章被收录于专栏:奔跑的人生奔跑的人生

Maven 基础知识

官网: 传送门

Maven 项目结构

代码语言:javascript
复制
$ MavenProject
|-- pom.xml
|-- src
|   |-- main
|   |   `-- java
|   |   `-- resources
|   `-- test
|   |   `-- java
|   |   `-- resources
`-- README.md

POM文件

  • POM文件代表 工程对象模型(Project Object Model)它是使用Maven工作的基本组件,位于工程根目录。
  • POM文件支持继承
代码语言:javascript
复制
<?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 坐标

UTOOLS1563873610326.png
UTOOLS1563873610326.png

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 (相当于设置不允许将该依赖传递下去)
UTOOLS1563869243423.png
UTOOLS1563869243423.png

常用命令

UTOOLS1563873521089.png
UTOOLS1563873521089.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Maven 基础知识
    • POM文件
      • Maven 坐标
        • Maven Dependency
          • Scope类型
          • 依赖传递特性
        • 常用命令
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档