# Maven

采用官方 maven central + gradle-plugin 混合形式。gradle 和 maven 使用的是同一个仓库，不过配置方法略有区别。

## maven 配置指南

### 全局配置

配置当前登录用户使用南大镜像站，如果未修改过 maven 的默认配置文件位置，请如下操作：

- linux 用户在终端中输入以下命令:

    ```shell
    mkdir ~/.m2
    vim ~/.m2/settings.xml
    ```

- windows 用户在 powershell (或者pwsh中) 输入以下命令：

    ```shell
    mkdir ~/.m2
    cd ~/.m2
    notepad settings.xml
    ```

如果打开的文件为空，粘贴以下内容：

```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">
    <mirrors>
       <mirror>
            <id>nju_mirror</id>
            <mirrorOf>*</mirrorOf>
            <url>https://repo.nju.edu.cn/maven/</url>
        </mirror>
    </mirrors>
</settings>
```

如果已经有内容，则在 `mirrors` 节点加入子节点 (对于 mirrorOf 值相同的节点，只有首个子节点生效)：

```xml
<mirror>
    <id>nju_mirror</id>
    <mirrorOf>*</mirrorOf>
    <url>https://repo.nju.edu.cn/maven/</url>
</mirror>
```

保存退出即可使用。

### 项目配置

如果只在某个项目中使用，则在项目的 `pom.xml` 中配置：

```xml
<project>
    ......[其他配置]
    
    <repositories>
        <repository>
            <id>nju</id>
            <url>https://repo.nju.edu.cn/maven/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
```

## gradle-plugin 配置指南

注意，开源镜像同时提供 gradle 二进制文件下载，将项目文件下的 `gradle\wapper\gradle-wrapper.properties` 的 `distributionUrl` 修改为 `http://mirror.nju.edu.cn/gradle/gradle-[version].zip` 即可，详细版本请[参考链接](https://mirror.nju.edu.cn/gradle)。

### 全局配置

配置当前登录用户使用南大镜像站，如果未修改过 gradle 的默认配置文件位置，请如下操作:


- linux 用户在终端中输入以下命令:

    ```shell
    mkdir ~/.gradle
    vim ~/.gradle/init.gradle
    ```

- windows 用户在 powershell (或者pwsh中) 输入以下命令:

    ```shell
    mkdir ~/.gradle
    cd ~/.gradle
    notepad init.gradle
    ```

覆盖写入以下内容:

```gradle
allprojects {
    buildscript {
        repositories {
            def NJU_REPOSITORY_URL = 'https://repo.nju.edu.cn/maven/'
            all { ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $NJU_REPOSITORY_URL."
                        remove repo
                    }
                    if (url.startsWith('https://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} deleted."
                        remove repo
                    }
                    if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                        project.logger.lifecycle "Repository ${repo.url} deleted."
                        remove repo
                    }
                    if (url.contains('plugins.gradle.org/m2')) {
                        project.logger.lifecycle "Repository ${repo.url} deleted."
                        remove repo
                    }
                }
            }
            maven { url NJU_REPOSITORY_URL }
            mavenLocal()
        }
    }

    repositories {
        def NJU_REPOSITORY_URL = 'https://repo.nju.edu.cn/maven/'
        all { ArtifactRepository repo ->
            if (repo instanceof MavenArtifactRepository) {
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $NJU_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} deleted."
                    remove repo
                }
                if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} deleted."
                    remove repo
                }
                if (url.contains('plugins.gradle.org/m2')) {
                    project.logger.lifecycle "Repository ${repo.url} deleted."
                    remove repo
                }
            }
        }
        maven { url NJU_REPOSITORY_URL }
        mavenLocal()
    }
}
```

如果已经运行过 gradle ，请先执行一次 `gradle --stop` 命令关闭所有 gradle 的 daemon， 然后重新运行即可。

### 项目配置

如果只在某个项目中使用，则在项目的 `build.gradle` 中配置:

```gradle
buildscript {
    repositories {
        maven { url 'https://repo.nju.edu.cn/maven/' }
    }
}


plugins {
    ...[你需要用的 gradle 插件]
}

allprojects {
    repositories {
        maven { url 'https://repo.nju.edu.cn/maven/' }
    }
}
```

注意这样可能有时候无法通过镜像下载部分 gradle 插件。