1. 程式人生 > 實用技巧 >北京師範大學第十六屆程式設計競賽決賽--H 吾好夢中做題

北京師範大學第十六屆程式設計競賽決賽--H 吾好夢中做題

安裝

  • 可以官網安裝
  • 或者下載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>