使用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 坐标

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

常用命令

UTOOLS1563873521089.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Maven使用入门 1. Maven概述 Maven是什么?Maven项目对象模型(POM),可以通过一小段描述信...
    小小韩_小小红阅读 2,596评论 2 28
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,081评论 0 23
  • 首先说明,本人并不是第一次学习maven,也不是第二次学习maven,为什么还要来学习写下这些东西?是因为之前一直...
    渭小粉阅读 432评论 0 1
  • 最近用到了Maven,就开始看看文档,网上找找资料,看了些视频。现在整理一下,仅供参考。 0x00 关于Mave...
    明月的味道阅读 914评论 4 8
  • maven是一款优秀的服务构建工具,基于约定优于配置原则,提供标准的服务构建流程。maven的优点不仅限于服务构建...
    zhipingChen阅读 3,990评论 0 4