gradle publish && company use

gradle插件编写 && 上传 && 使用

插件编写

官网教程

打包上传(普通jar包)

账号密码与项目分离

在环境变量 GRADLE_USER_HOME(一般为 ${user.home}/.gradle)下新建 gradle.properties, 里面的配置可被build.gradle引用

插件引用

1
2
apply plugin: 'maven'
apply plugin: 'maven-publish'

kotlin下发生的奇怪问题

1
2
3
4
5
bootJar.enabled = false

jar{
enabled = true
}

打包源码

1
2
3
4
5
6
7
8
9
task sourcetype(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}

artifacts{
archives jar
archives sourcetype
}

上传到maven仓库

1
2
3
4
5
6
7
8
9
10
11
12
    uploadArchives {
repositories.mavenDeployer {
repository(url: 'http://192.168.103.133:8080/repository/scooper-trial/') {// 仓库地址
// 这里配置上传账户的用户名和密码
authentication(userName: scooperTrialUser, password: ******)
}

pom.version = project.version
pom.groupId = project.group
pom.artifactId = project.name
}
}

项目全部配置

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin library project to get you started.
*/

plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id 'org.jetbrains.kotlin.jvm' version '1.3.11'
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'maven'
id 'maven-publish' //该插件可以将打包的jar发送到maven库
}

apply plugin: 'io.spring.dependency-management'

version = "1.0.0.0-beta11"
group = "cn.com.scooper"


repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
mavenCentral()
jcenter()
maven {
url = 'http://192.168.103.133:8080/repository/internal'
}
}

dependencies {
// Use the Kotlin JDK 8 standard library.
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

// Use the Kotlin test library.
testImplementation 'org.jetbrains.kotlin:kotlin-test'

// Use the Kotlin JUnit integration.
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}

dependencies {
compile "org.springframework.boot:spring-boot-starter"
}

dependencies {
compile project(":kiswich-spring-boot-starter")
compile project(":kiswich-spring-boot-base")
compile project(":kiswich-spring-boot-autoconfigure")
}

allprojects {
configurations {
modules
compile.extendsFrom(modules)
}
}

subprojects {
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories {
maven {
url = 'http://192.168.103.133:8080/repository/internal'
}
mavenCentral()
}

group = rootProject.group
version = rootProject.version

bootJar.enabled = false

jar{
enabled = true
// archiveName = "${baseName}-${appendix}-${version}.${extension}"
}

task sourcetype(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}

artifacts{
archives jar
archives sourcetype
}

dependencies {
// Use the Kotlin JDK 8 standard library.
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'

// Use the Kotlin test library.
testImplementation 'org.jetbrains.kotlin:kotlin-test'

// Use the Kotlin JUnit integration.
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
}

dependencies {
compile "org.springframework.boot:spring-boot-starter"
compile 'org.springframework.boot:spring-boot-starter-web'
}

dependencies {
compile 'cn.com.scooper.common:scooper-common:1.0.24'
}

uploadArchives {
repositories.mavenDeployer {
repository(url: 'http://192.168.103.133:8080/repository/scooper-trial/') {// 仓库地址
// 这里配置上传账户的用户名和密码
authentication(userName: scooperTrialUser, password: ******)
}

pom.version = project.version
pom.groupId = project.group
pom.artifactId = project.name
}
}
}

bootJar.enabled = false
jar.enabled = true

打包上传插件

与前者不用的是,这里将用gradle自带的publish上传,因为maven只是把jar包上传了,并没有上传插件的信息

配置插件信息

1
2
3
4
5
6
7
8
9
gradlePlugin {
plugins {
ScooperPackPlugin {
id = "scooper.pack"
implementationClass = 'cn.com.scooper.packplugin.MainPlugin'
}

}
}

配置上传信息

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
task sourcesJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}

publishing {
publications {
plugin(MavenPublication) {
from components.java
group project.group
artifactId project.rootProject.name
version project.version


artifact jar
artifact sourcesJar

}

}
repositories {
maven {
url = 'http://192.168.103.133:8080/repository/scooper-trial/'
credentials {
username scooperTrialUser
password ******
}
}
}
}

执行 task publish

全部配置

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
plugins {
id 'groovy'
id 'java'
// id 'com.gradle.plugin-publish' version '0.10.0'
// id 'maven'
id 'maven-publish' //该插件可以将打包的jar发送到maven库
id 'java-gradle-plugin'
}

group 'cn.com.scooper'
version '1.0.18-SNAPSHOT'


sourceCompatibility = 1.8

repositories {
mavenCentral()
maven {
url = 'http://192.168.103.133:8080/repository/scooper-trial/'
credentials {
username scooperTrialUser
password ******
}
}
}

dependencies {
// compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'

implementation localGroovy()
implementation gradleApi()
}

gradlePlugin {
plugins {
ScooperPackPlugin {
id = "scooper.pack"
implementationClass = 'cn.com.scooper.packplugin.MainPlugin'
}

}
}


task sourcesJar(type: Jar) {
from sourceSets.main.allSource
classifier = 'sources'
}


artifacts {
archives jar
archives sourcesJar
}


publishing {
publications {
plugin(MavenPublication) {
// from components.java
group project.group
artifactId project.rootProject.name
version project.version


artifact jar
artifact sourcesJar

}

}
repositories {
maven {
url = 'http://192.168.103.133:8080/repository/scooper-trial/'
credentials {
username scooperTrialUser
password ******
}
}
}

}

//uploadArchives {
// repositories.mavenDeployer {
// repository(url: 'http://192.168.103.133:8080/repository/scooper-trial/') {// 仓库地址
//// 这里配置上传账户的用户名和密码
// authentication(userName: scooperTrialUser, password: ******)
// }
// pom.version = project.version
// pom.groupId = project.group
// pom.artifactId = project.rootProject.name
// }
//}