本文最后更新于 146 天前 ,文中信息可能已经过时。如有问题请在评论区留言。

Gradle 使用 Nexus3 作为私有仓库

官方文档参考:declaring-repositories

1. 项目级配置

步骤1:新建 gradle.properties 文件,并添加如下配置:

properties
1
2
3
nexusUrl=nexus url
nexusUsername=nexus username
nexusPassword=nexus password

步骤2:在 build.gradle 中添加如下配置

gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 从 Nexus 下载依赖
repositories {
  // 优先使用本地仓库
  mavenLocal()

  // 使用 Nexus 私有仓库
  maven {
    url "${nexusUrl}"
    credentials {
      username "${nexusUsername}"
      password "${nexusPassword}"
    }
  }
}

2. 全局配置

官方文档参考: init_scripts

在 gradle 安装目录下的 init.d 文件夹内,创建 init.gradle 文件,并添加如下配置:

gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
allprojects {

    // 从 Nexus 下载依赖
    repositories {
        // 优先使用本地仓库
        mavenLocal()

        // 使用 Nexus 私有仓库
        maven {
            url "nexus url"
            credentials {
                username "nexus username"
                password "nexus password"
            }
        }
    }
}

Gradle 使用 Nexus3 作为插件库

官方文档参考:plugins

1. 项目级配置

步骤1:新建 gradle.properties 文件,并添加如下配置:

properties
1
2
3
nexusUrl=nexus url
nexusUsername=nexus username
nexusPassword=nexus password

步骤2:在 settings.gradle首行 添加如下配置:

gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
pluginManagement {
    repositories {
        maven {
            url "${nexusUrl}"
            credentials {
                username "${nexusUsername}"
                password "${nexusPassword}"
            }
        }
        gradlePluginPortal()
    }
}

2. 全局配置

在 gradle 安装目录下新建 init.gradle 文件,并添加如下配置:

gradle
init.gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
settingsEvaluated { settings ->
    settings.pluginManagement {
        repositories {
			// 使用 Nexus 私有仓库
			maven {
				url "nexus url"
				credentials {
					username "nexus username"
					password "nexus password"
				}
			}
			gradlePluginPortal()
        }
    }
}

Gradle 发布到 Nexus3

官方文档参考:publishing_maven:repositoriessample_publishing_credentials

步骤1:新建 gradle.properties 文件,并添加如下配置:

properties
1
2
3
4
nexusReleasesUrl=nexus release url
nexusSnapshotsUrl=nexus snapshot url
nexusUsername=nexus username
nexusPassword=nexus password

步骤2:在 plugins 节点下添加 maven-publish 插件

gradle
1
2
3
plugins {
	id 'maven-publish'
}

步骤3:在 build.gradle 文件中,添加如下配置:

gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 发布到 私有库
publishing {
  publications {
    binary(MavenPublication) {
      from components.java
    }
    binaryAndSources(MavenPublication) {
      from components.java
      artifact sourcesJar
    }
  }

  repositories {
    maven {
      name 'Nexus'
      // 用户口令
      credentials {
        username "${nexusUsername}"
        password "${nexusPassword}"
      }

      // RELEASE 仓库
      def releasesRepoUrl = "${nexusReleasesUrl}"

      // SNAPSHOT 仓库
      def snapshotsRepoUrl = "${nexusSnapshotsUrl}"

      // 根据版本信息动态判断发布到 SNAPSHOT 还是 RELEASE 仓库
      url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
    }
  }
}