-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
207 lines (175 loc) · 5.58 KB
/
Copy pathbuild.gradle
File metadata and controls
207 lines (175 loc) · 5.58 KB
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
/**
* Composite project example with different samples of how to do different tasks
*
* Useful docs:
*
* Getting started: https://docs.gradle.org/current/userguide/tutorial_java_projects.html
*
* Guides: https://gradle.org/guides/
*
* Reference
* - Top-level Gradle DSL reference: https://docs.gradle.org/current/dsl/
* - Composite Builds: https://docs.gradle.org/current/userguide/composite_builds.html
* - Working with files and file tress: https://docs.gradle.org/current/userguide/working_with_files.html
* - Understanding configurations and dependencies: https://docs.gradle.org/current/userguide/managing_dependency_configurations.html
* - Methods/properties available on the configuration object: https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html
* - Declaring dependencies: https://docs.gradle.org/current/userguide/declaring_dependencies.html
*
* To build compB independent of compA: cd compA and run: gradle publishToMavenLocal
* Then go to compB/nested and: gradle build
*/
apply plugin: 'idea'
apply plugin: 'distribution'
group "org.example.gradle"
version "1.0"
repositories {
mavenLocal()
mavenCentral()
jcenter()
}
// Defining multiple configurations
configurations {
config1
config2
}
// gradle hooks!
// see: https://docs.gradle.org/current/javadoc/org/gradle/api/invocation/Gradle.html#settingsEvaluated-groovy.lang.Closure-
gradle.projectsLoaded{ g->
println "projectsLoaded running"
}
gradle.beforeProject{
println "beforeProject running"
}
gradle.afterProject{
println "afterProject running"
}
gradle.buildFinished{
println "buildFinished"
}
gradle.settingsEvaluated{
println "settingsEvaluated"
}
gradle.taskGraph.whenReady{
println "taskGraph.whenReady"
}
project.ext {
custom1 = 'YoYoMa'
}
task printProperties {
doLast {
println "Extra Properties: ${project.ext.properties}"
println "custom1: ${project.ext.custom1}"
println "project.properties: ${project.properties.hasProperty('custom1')}"
// project properties
}
}
dependencies {
config1 'org.apache.httpcomponents:httpclient:4.5.6'
config1 "org.webjars:swagger-ui:2.1.5"
config2 group:"org.apache.tomcat", name:"tomcat", version:"10.1.28", ext: "zip"
config2 group: 'junit', name: 'junit', version: '4.+'
}
//
// Custom tasks
// - custom tasks need to be assigned to a group or they won't display when 'gradle tasks' is run
// they will show using 'gradle tasks --all'
//
task custom {
inputs.file("src/main/resources/custom-task.properties")
outputs.file("${buildDir}/custom.properties")
doLast {
println "Copying file..."
ant.copy file: "src/main/resources/custom-task.properties",
tofile: "${buildDir}/custom.properties"
}
}
task showResolvedArtifacts() {
doLast {
project.configurations.config2.resolvedConfiguration.resolvedArtifacts.each {
println "Artifact name: ${it.name}"
println "Artifact module.group: ${it.moduleVersion.id.group}"
println "Artifact module.version: ${it.moduleVersion.id.version}"
// it.file
}
}
}
task test {
group = "Test"
description = "gradle test from the top-level does not run tests for the included composite builds, not sure if this is a bug or a feature"
dependsOn gradle.includedBuilds*.task(':test')
}
//
// Debugging build scripts
//
task debug1 {
group = "Debug"
doFirst {
println "** config1 dependencies: ${configurations.config1.files}"
def swaggerFile = configurations.config1.filter {f -> f.name.contains('swagger')}
println "** Swagger file : ${swaggerFile.singleFile}"
// inspect an object's type and properties
println "Configuration type: ${configurations.config1.getClass()}"
//println "Configuration object properties: ${configurations.config1.properties}"
// use println() to debug lambdas on-the-fly
def tomcatZip = configurations.config2.filter {f -> println("on the fly: ${f}") || f.name.contains('tomcat')}.singleFile
println "Tomcat Zip File: ${tomcatZip}"
}
doLast {
println "** config2 dependencies: ${configurations.config2.files}"
}
}
//
// Working with files
//
//
// Building distributions
//
distributions {
main {
baseName = 'testdistro1'
contents {
// extracts the tomcat artifact from the config2 configuration and puts it into an extracted directory
from(zipTree(configurations.config2.filter {f -> f.name.contains('tomcat')}.singleFile)) {
into('extracted')
}
}
}
}
//
// Artifacts and Publications using the maven-publish plugin
//
// say we want to publish multiple artifacts from the same project, a tar file and a custom artifact that's just a flat file
// Try this using 'gradle publishToMavenLocal'
apply plugin: 'maven-publish'
def generated = "${buildDir}/generated.txt"
task generate{
doFirst {
new File(generated) << "This is my generated artifact published as part of my project "
}
}
publish.dependsOn('generate')
publishToMavenLocal.dependsOn('generate')
publishing.publications {
tar(MavenPublication) {
artifact distTar
}
maven(MavenPublication) {
artifact generated
artifactId "MyGeneratedArtifact"
}
}
//
// Building war files
//
//war {
// rootSpec.exclude("**/*someserver*tar.gz")
// archiveName = "rest-${version}.war"
// from(zipTree(configurations.api.singleFile).matching {include "META-INF/resources/webjars/swagger-ui/${swagger_ui_version}/**"}) {
// exclude("**/index.html*")
// into('api-docs')
// eachFile { f ->
// f.path = f.path.replaceFirst("META-INF/resources/webjars/swagger-ui/${swagger_ui_version}", '')
// }
// includeEmptyDirs false
// }
//}