-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
296 lines (257 loc) · 7.02 KB
/
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
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
plugins {
id 'groovy'
}
group 'cn.adbyte'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
//指定 Java 项目的版本号 影响war包名
version = '1.2'
//增加Java项目的Build Clean JavaDoc task等
apply plugin: 'java'
//sourceCompatibility:指定编译编译.java文件的jdk版本
sourceCompatibility = 1.8
//targetCompatibility:确保class文件与targetCompatibility指定版本
// 或者更新的java虚拟机兼容
targetCompatibility = 1.8
description = 'Gradle 项目测试'
apply from: 'file.gradle'
apply from: 'other.gradle'
apply from: 'fileTree.gradle'
/****************Tomcat plugin***********************/
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.bmuschko:gradle-tomcat-plugin:2.3'
}
}
apply plugin: 'com.bmuschko.tomcat'
dependencies {
def tomcatVersion = '8.5.16'
tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
"org.apache.tomcat.embed:tomcat-embed-logging-juli:8.5.2",
"org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
providedCompile "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}"
}
configurations {
// 所有需要忽略的包定义在此
// all*.exclude group: 'commons-httpclient'
// all*.exclude group: 'commons-logging'
// all*.exclude group: 'commons-beanutils', module: 'commons-beanutils'
}
tomcat {
httpProtocol = 'org.apache.coyote.http11.Http11Nio2Protocol'
ajpProtocol = 'org.apache.coyote.ajp.AjpNio2Protocol'
}
/****************end***********************/
repositories {
//Maven central 仓库
mavenCentral()
//远程的 Maven 仓库
maven {
url "http://repo.mycompany.com/maven2"
}
//远程的 Ivy 仓库
ivy {
url "http://repo.mycompany.com/repo"
}
//本地的 Ivy 目录
ivy {
// URL can refer to a local directory
url "../local-repo"
}
}
dependencies {
/**
* compile 用来编译项目源代码的依赖.
* runtime 在运行时被生成的类使用的依赖. 默认的, 也包含了编译时的依赖.
* testCompile 编译测试代码的依赖. 默认的, 包含生成的类运行所需的依赖和编译源代码的依赖.
* testRuntime 运行测试所需要的依赖. 默认的, 包含上面三个依赖.
*/
// compile group: 'org.springframework', name: 'spring-webmvc', version: '4.+'
//简写形式 compile "group:name:version".
// testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
//META.INF >> MANIFEST.MF文件
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
manifest.attributes provider: 'gradle'
}
//测试阶段加入一个系统属性
test {
systemProperties 'property': 'value'
}
//发布 JAR 文件
//发布的jar文件可以用仓库地址的方式自动提供依赖
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
subprojects {
//操作项目对象的属性
println "name: " + name
println "project.name: " + project.name
println "description: " + this.description
println "path: " + path
println "projectDir: " + projectDir
println "build: " + build
}
task myCopy(type: Copy)
myCopy {
from 'resources'
into 'target'
include('**/*.txt', '**/*.xml', '**/*.properties')
}
//从另外一个项目给任务加入依赖
project(':Gradle:War') {
task taskX(dependsOn: ':Gradle:Test:taskY') {
doLast {
description '从另外一个项目给任务加入依赖'
println 'taskX'
}
}
}
project(':Gradle:Test') {
task taskY {
doLast {
description '从另外一个项目给任务加入依赖'
println 'taskY'
}
}
}
/** 通过任务对象加入依赖 */
task taskX2 {
println 'taskX2'
}
task taskY2 {
println 'taskY2'
}
taskX2.dependsOn taskY2
/** 通过闭包加入依赖 */
task taskX3 {
println 'taskX3'
}
taskX3.dependsOn {
tasks.findAll { task -> task.name.startsWith('lib') }
}
task lib1 {
doLast {
println 'lib1'
}
}
//排序规则: "must run after" 和 "should run after".
lib1.shouldRunAfter(":Gradle:lib2")
task lib2 {
doLast {
println 'lib2'
}
}
task notALib {
doLast {
println 'notALib'
}
}
task copy2(type: Copy) {
description 'Copies the resource directory to the target directory.'
from 'resources'
into 'target'
include('**/*.md', '**/*.xml', '**/*.properties')
}
//覆写一个任务
task copy(overwrite: true) {
doLast {
println('覆写一个任务')
}
}
//使用判断条件跳过一个任务
task hello(overwrite: true) {
doLast {
println 'hello world'
}
}
hello.onlyIf { !project.hasProperty('skipHello') }
//激活和注销 tasks
task disableMe {
doLast {
println 'This should not be printed if the task is disabled.'
}
}
disableMe.enabled = false
//声明任务的输入和输出
//如果一个任务只定义了输出, 如果输出不变的话, 它就会被视为 up-to-date.
task transform {
ext.srcFile = file('knight.xml')
ext.destDir = new File(buildDir, 'generated')
inputs.file srcFile
outputs.dir destDir
doLast {
println "Transforming source file."
destDir.mkdirs()
def mountains = new XmlParser().parse(srcFile)
mountains.mountain.each { mountain ->
def name = mountain.name[0].text()
def height = mountain.height[0].text()
def destFile = new File(destDir, "${name}.txt")
destFile.text = "$name -> ${height}\n"
}
}
}
//任务规则
tasks.addRule("Pattern: ping<ID>") { String taskName ->
if (taskName.startsWith("ping")) {
task(taskName) << {
println "Pinging: " + (taskName - 'ping')
}
}
}
//加入一个任务终止器
task taskX4 {
doLast {
println 'taskX4'
}
// throw new RuntimeException()
}
task taskY4 {
doLast {
println 'taskY4'
}
}
taskX4.finalizedBy taskY4
task printProps {
doLast {
//gradle -q -PcommandLineProjectProp=commandLineProjectPropValue -Dorg.gradle.project.systemProjectProp=systemPropertyValue printProps
println commandLineProjectProp
println gradlePropertiesProp
println systemProjectProp
println envProjectProp
println System.properties['system']
}
}
//使用其他的构建脚本配置项目
//apply from: 'other.gradle'
//使用别的脚本配置配置对象
task config {
doLast {
def pos = new java.text.FieldPosition(10)
// 使用另一个脚本
apply from: 'other.gradle', to: pos
println pos.beginIndex
println pos.endIndex
}
}
/*jar {
//将依赖和资源文件打入jar包
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}*/