-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.gradle
313 lines (256 loc) · 9.75 KB
/
core.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.guardsquare:proguard-gradle:7.2.1' // The ProGuard Gradle plugin.
}
}
ext {
// useful macros, you can add your own
macros = [
'all' : [
'clean',
'classpath',
'build',
'minify',
'generatePomFileForInternalPublication', // needed to generate internal POM
'publishMavenJavaPublicationToMavenLocal' // real publication to Maven Local (~/.m2/repository)
],
'sonatype' : [
'generatePomFileForInternalPublication',
'publishMavenJavaPublicationToOssrhRepository',
'closeAndReleaseRepository'
],
'deploy' : [
'uploadArchives',
'closeAndReleaseRepository'
],
'classpath' : [
'cleanEclipseClasspath',
'eclipseClasspath',
'eclipseFactoryPath',
'cleanIdeaModule',
'ideaModule'
],
]
// package patterns to exclude from Eclipse
excludeFromEclipse = []
}
// -------------------------------------------
// REPOSITORIES / PUBLISHING
// -------------------------------------------
repositories {
// prefer locally built artifacts
mavenLocal()
// use external repos as fallback
mavenCentral()
}
task sourcesJar(type: Jar) {
archiveClassifier.set('sources')
from sourceSets.main.allJava
}
task javadocJar(type: Jar, dependsOn: classes) {
archiveClassifier.set('javadoc')
from javadoc
}
task testsJar(type:Jar, dependsOn: testClasses) {
archiveClassifier.set('tests')
from sourceSets.test.output
}
test {
// enable JUnit 5 tests
useJUnitPlatform()
}
// -------------------------------------------
// JAVA COMPILER
// -------------------------------------------
tasks.withType(JavaCompile) { task ->
sourceCompatibility = 11
targetCompatibility = 11
// always UTF-8
options.encoding = 'UTF-8'
// java 8 option which export names of constructor and method parameter names; no longer
// have to declare parameter names with @JsonCreator
options.compilerArgs << "-parameters"
options.compilerArgs << '-Xlint:unchecked'
if (project.plugins.hasPlugin('net.ltgt.errorprone')) {
// Eclipse code formatting removes extraneous parenthesis which errorprone complains about
options.errorprone.disable 'OperatorPrecedence'
// we don't need to check return value always
options.errorprone.disable 'FutureReturnValueIgnored'
// generated code can have lots of bogus warnings
options.errorprone.disableWarningsInGeneratedCode = true
// bogus warning
options.errorprone.disable 'StringSplitter'
// ignore all generated source folders
options.errorprone.excludedPaths = '.*generated.*'
}
}
// -------------------------------------------
// SHADOW JAR
// -------------------------------------------
if(plugins.hasPlugin("com.github.johnrengelman.shadow")) {
jar {
archiveClassifier.set('original')
}
shadowJar {
archiveClassifier.set('shadow')
}
sourceSets {
// shadow configuration is added by Shadow plugin, but it's only configured for the main sourceset
test.compileClasspath += configurations.shadow
test.runtimeClasspath += configurations.shadow
}
}
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
publishing {
if(project.hasProperty('nexusUsername')) {
repositories {
maven {
name = "ossrh"
url = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
credentials {
username = project.nexusUsername
password = project.nexusPassword
}
}
}
}
publications {
mavenJava {
artifact sourcesJar
artifact javadocJar
artifact testsJar
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
}
signing {
sign publishing.publications.mavenJava
}
nexusStaging {
repositoryDescription project.name
}
// -------------------------------------------
// ECLIPSE
// -------------------------------------------
eclipse {
classpath {
// override default 'bin'
defaultOutputDir = project.file('bin/classes')
// we want source files
downloadSources = true
downloadJavadoc = false
// customize generated .classpath file
file {
def project_refs = []
def remove_entries = []
// closure executed after .classpath content is loaded from existing file
// and after gradle build information is merged
whenMerged { classpath ->
// build list of dependencies that we want to replace with Eclipse project refs
println 'Finding local projects'
def use_eclipse_project_refs = []
new File(project.projectDir, "..").eachDir {
if(new File("${it}/build.gradle").exists()) {
use_eclipse_project_refs.add it.name
}
}
println 'Generating Eclipse .classpath file'
def kindOrder = [ 'src':1, 'con':2, 'lib':3, 'output':0 ];
classpath.entries.sort(true, { a,b ->
def order = kindOrder[a.kind] <=> kindOrder[b.kind]
order != 0 ? order : a.path <=> b.path
} as Comparator).each { entry ->
if(entry.kind.equals('lib')) {
use_eclipse_project_refs.each { name ->
def regex = '/(' + ( name.endsWith('-') ?
java.util.regex.Pattern.quote(name.substring(0,name.length()-1)) + '(?:-[A-Za-z]+)*'
: java.util.regex.Pattern.quote(name) ) + ')-([\\w\\.]+?)(-[A-Za-z]+)?\\.jar$'
def pattern = java.util.regex.Pattern.compile(regex)
def matcher = pattern.matcher(entry.path)
if(matcher.find()) {
def match = matcher.group(1)
println match + ' (' + matcher.group(2) + ') matched ' + entry.path
remove_entries += [entry]
project_refs += [match]
}
}
entry.exported = true
} else if(entry.kind.equals('src')) {
project.ext.excludeFromEclipse.each { path ->
if(entry.path.equals(path)) {
remove_entries += [entry]
}
}
}
}
classpath.entries.removeAll(remove_entries)
}
// final adjustments to .classpath file before it is saved
withXml { xml ->
def node = xml.asNode()
project_refs.unique(false).each { name ->
println "Creating Eclipse project dependency: " + name
node.appendNode('classpathentry', [ combineaccessrules: false, exported: true, kind: 'src', path: '/' + name ])
}
def apt = ['.apt_generated_test': 'bin/test',
'.apt_generated': 'bin/main']
apt.each { path, output ->
def atts = node.appendNode('classpathentry', [kind:'src', output: output, path: path]).appendNode('attributes')
atts.appendNode('attribute', [name: 'ignore_optional_problems', value: true])
atts.appendNode('attribute', [name: 'test', value: output.contains('test')])
atts.appendNode('attribute', [name: 'optional', value: true])
}
}
}
}
}
// -------------------------------------------
// README
// -------------------------------------------
task readme {
ant.replaceregexp(match:'\\<version\\>([0-9\\.]+)\\<\\/version\\>', replace:"<version>${version}</version>", flags:'g', byline:true) {
fileset(dir: '.', includes: 'README.md')
}
ant.replaceregexp(match:'com\\.arakelian\\:' + project.name + ':([0-9\\.]+)', replace:"com.arakelian:${project.name}:${version}", flags:'g', byline:true) {
fileset(dir: '.', includes: 'README.md')
}
}
// -------------------------------------------
// SHORTCUT TASKS
// -------------------------------------------
// This code allows us to define aliases, such as "all", so that when we do "gradle all",
// we can substitute in a series of other gradle tasks
// see: https://caffeineinduced.wordpress.com/2015/01/25/run-a-list-of-gradle-tasks-in-specific-order/
def newTasks = []
// gradle respects ordering of tasks specified on command line, so we replace shortcuts
// with equivalent commands as though they were specified by user
gradle.startParameter.taskNames.each { param ->
def macro = project.ext.macros[param]
if( macro ) {
macro.each { task ->
if(project.tasks.names.contains(task)) {
newTasks << task
}
}
} else {
newTasks << param
}
}
// replace command line arguments
gradle.startParameter.taskNames = newTasks.flatten()