Maven

安装

  • 可以官网安装
  • 或者下载IDEA就会自动安装

Maven是什么

  • 一个跟 NPM 的package.json一样的工具
  • 并且支持本地自定义模块

Maven两种使用模式

  • 正常添加一下网络的模块
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <name>pdt-ssm</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>
  • 自定义父子级模块,模块化项目,常见于springcloud
// 父模块

<?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>

    <!--父级依赖,只有springboot和springcloud需要-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/>
    </parent>

    <!--打包方式,默认是jar-->
    <packaging>pom</packaging>

    <!--自定义为父级,用于整合子模块-->
    <groupId>com.pdt</groupId>
    <artifactId>pdt</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--子模块-->
    <modules>
        <module>pdt-common-redis</module>
        ...
    </modules>

</project>
// 子模块,子模块跟父模块必须同一个 groupId 和 artifactId
<?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">

    <parent>
        <artifactId>pdt</artifactId>
        <groupId>com.pdt</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>pdt-common-redis</artifactId>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>

        <!-- SpringBoot Boot Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

</project>
  • 子模块下还可以有子模块

值得一讲的packaging

  • 这是指打包方式
  • 不管是上面的那种使用方式,只要是用于启动的模块或者工具包 必须填写jar或者war
  • 作为父级的则要改成pom

Maven的标签

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.pdt</groupId>
    <artifactId>pdt</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>consumer-80</module>
        <module>eureka-7001</module>
        <module>eureka-7002</module>
        <module>gateway-80</module>
        <module>module-order-9001</module>
        <module>module-order-9002</module>
        <module>module-user-8001</module>
        <module>pdt-common</module>
    </modules>

</project>

pom.xml下载太慢

  • 打开maven的设置文件settings.xml,把内容改成下面设置(推荐)
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <pluginGroups/>
    <proxies/>
    <servers/>

    <mirrors>
        <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>central</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror>
        <!--配置华为云Maven镜像-->
        <mirror>
            <id>huaweicloud</id>
            <mirrorOf>*</mirrorOf>
            <url>https://mirrors.huaweicloud.com/repository/maven/</url>
        </mirror>
    </mirrors>

    <profiles/>
</settings>
  • 如果不设置maven的settings,可以在每个pom.xml文件加入
<repositories>
    <repository>
        <id>aliyun-maven-repo</id>
        <name>aliyun-maven-repo</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
posted @ 2020-08-18 14:10  一个年轻淫  阅读(85)  评论(0编辑  收藏  举报