Skip to main content

Maven

南大源采用了阿里云(central + jcenter + gradle-plugin),腾讯云(central+jcenter+google)和 maven central 官方的混合形式。

maven 配置指南

全局配置

配置当前登录用户使用南大源,如果未修改过 maven 的默认配置文件位置,请如下操作:

  • linux 用户在终端中输入以下命令:

    mkdir ~/.m2
    vim ~/.m2/settings.xml
    
  • windows 用户在 powershell (或者pwsh中) 输入以下命令:

    mkdir ~/.m2
    cd ~/.m2
    notepad 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">
    <mirrors>
       <mirror>
            <id>nju_mirror</id>
            <mirrorOf>*</mirrorOf>
            <url>https://repo.nju.edu.cn/repository/maven-public/maven/</url>
        </mirror>
    </mirrors>
</settings>

如果已经有内容,则在 mirrors 节点加入子节点 (对于 mirrorOf 值相同的节点,只有首个子节点生效):

<mirror>
    <id>nju_mirror</id>
    <mirrorOf>*</mirrorOf>
    <url>https://repo.nju.edu.cn/repository/maven-public/maven/</url>
</mirror>

保存退出即可使用。

项目配置

如果只在某个项目中使用,则在项目的 pom.xml 中配置:

<project>
    ......[其他配置]
    
    <repositories>
        <repository>
            <id>nju</id>
            <url>https://repo.nju.edu.cn/repository/maven-public/maven/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

maven 私服

南京大学 maven 仓库同时提供 jar 包托管服务,本校师生可以将自己编写的 jar 发布到仓库中让其他人使用。此仓库文件不会同步到其他镜像源

从仓库获取文件

将上面提到的仓库地址改为 https://repo.nju.edu.cn/repository/maven-nju/ 即可。

使用此仓库请注意:

    此仓库会优先从官方库和其他源中获取包,如果你发布了同名包,会被其他来源的同名包覆盖,因此请不要也禁止发布同名包或者名称相近包; 当仓库网络出现问题或者上游源出现问题时,可能会导致你下载到仓库其他人发布的同名包,这可能不是你想要的,甚至有风险(maven-public 没有此问题); 如果发现和上游仓库同名包,请填写统计表,管理员将会定期删除; 如果发现恶意同名包或者类似包,请填写统计表,管理员将核实后采取措施。

    向仓库提交文件

      ~/.m2/settings.xml 中,为 settings 节点添加以下子节点:

      <servers>
          <server>
              <id>nju-server</id>
              <username>学工号</username>
              <password>统一身份认证密码</password>
          </server>
      </servers>
      

      在项目的 pom.xml 中,为 project 节点添加以下子节点:

      <distributionManagement>
          <repository>
              <id>nju-server</id>
              <name>Releases</name>
              <url>http://repo.nju.edu.cn/repository/maven-releases/</url>
          </repository>
      
          <snapshotRepository>
              <id>nju-server</id>
              <name>Snapshot</name>
              <url>http://repo.nju.edu.cn/repository/maven-snapshots</url>
          </snapshotRepository>
      </distributionManagement>
      

      在项目根目录下运行 mvn deploy 即可发布,若版本号中以 -SNAPSHOT 结尾,会发布到 maven-snapshots, 否则会发布到 maven-releases。

      注意

        上面配置中的 id 节点值必须相同。 一经发布,仓库 jar 不允许修改和删除 发布过程中你的用户信息会被记录,因此请勿滥用。 请为你的项目使用合适的项目名称、组织名称和版本号,强烈建议不要使用默认的com.example