gradle的一些实践

gradle的一些实践,nexus的配置参考:nexus配置的一些实践

1. 认证

~/.gradle/gradle.properties 添加账号密码的变量

1
2
3
vim ~/.gradle/gradle.properties
nexus_user=你的账号
nexus_pwd=你的密码

2. 依赖

注意两个文件:

2.1 文件一:项目根目录的build.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
buildscript {
ext {
springCloudFrameVersion="2020.0.3"
springCloudVersion="3.0.3"
springBootVersion= "2.5.4"
}
//注意,在buildscript里添加仓库
repositories {
maven {
credentials {
username = "${nexus_user}"
password = "${nexus_pwd}"
}
url = "https://nexus.local.com/repository/maven-public/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
allprojects {
apply plugin: 'java'
group = 'test.lib'
sourceCompatibility = '11'
targetCompatibility = '11'
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
//注意,在allprojects里添加仓库
repositories {
maven {
credentials {
username = "${nexus_user}"
password = "${nexus_pwd}"
}
url = "https://nexus.local.com/repository/maven-public/"
}
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudFrameVersion}"
}
}
}
subprojects {
apply from: '../global_config.gradle'
}

2.2 文件二:setting.gradle

1
2
3
4
5
6
7
8
9
10
11
12
// 注意,必须放在文件头
pluginManagement {
repositories {
maven {
credentials {
username = "${nexus_user}"
password = "${nexus_pwd}"
}
url = "https://nexus.local.com/repository/maven-public/"
}
}
}

注意:不要再依赖 https://nexus.local.com/repository/javaproject/ 或者 google(),maven_centre(),jencenter(),只依赖maven-public.
maven-public作为group,已经将其他仓库全部作为成员进行了合集,包括:maven-center和阿里云的仓库。
如果你额外需要jcenter或者其他仓库,应该在nexus上创建proxy,并依然由maven-public统一收束。

3. 发布

目标:

1
2
3
4
//通过upSnap直接发布一个snapshots的包到nexus的snapshots仓库
./gradlew clean assemble upSnap
//通过upRelease直接发布一个release的包到nexus的release仓库
./gradlew clean assemble upRelease

配置方式

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
plugins {
// java项目添加java-library,android项目添加'com.android.library'
id 'java-library'
// 原'maven' plugin已更名为'maven-publish'
id 'maven-publish'
}
def testVersion = '6-SNAPSHOT'
def releaseVersion = '1.0.6.RELEASE'
def publishGroupId = "test.lib"
def publishArtifactId = "test-lib"
publishing {
publications {
maven(MavenPublication) {
from components.java
groupId = publishGroupId
artifactId = publishArtifactId
}
}
repositories {
maven {
name = 'release'
url = "https://nexus.local.com/repository/maven-releases/"
credentials {
username = "${nexus_user}"
password = "${nexus_pwd}"
}
}
maven {
name = 'snap'
url = "https://nexus.local.com/repository/maven-snapshots/"
credentials {
username = "${nexus_user}"
password = "${nexus_pwd}"
}
}
}
}
tasks.register('upSnap') {
group = 'publishing'
version = testVersion
dependsOn tasks.withType(PublishToMavenRepository).matching {
it.repository == publishing.repositories.snap
}
}
tasks.register('upRelease') {
group = 'publishing'
version = releaseVersion
dependsOn tasks.withType(PublishToMavenRepository).matching {
it.repository == publishing.repositories.release
}
}