Gradle Cookie

读音 Gradle [g’reɪdl]

配置国内源

  • 配置用于jar下载的国内镜像 build.gradle
  • 配置gradle自我下载的国内镜像 gradle/wrapper/gradle-wrapper.properties

build.gradle

1
2
3
4
5
6
7
8
9
10
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/repository/public/'}
maven { url 'https://maven.aliyun.com/repository/spring' }
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url "https://repo.spring.io/release" }
maven { url "https://repo.spring.io/libs-release" }
maven { url "https://repo.spring.io/plugins-release/" }
}

gradle-wrapper.properties

编辑 gradle/wrapper/gradle-wrapper.properties 文件
distributionUrl中的services.gradle.org 更换为 downloads.gradle-dn.com

init命令

通过 init 可创建 gradle 项目

1
2
gradle init --type java-application
gradle init --type java-library

小技巧

将maven项目转成gradle项目

1
2
gradle init --type pom

显示gradle中含有的projects

1
gradle projects

显示gradle中含有的task

1
gradle tasks

打印依赖树

1
gradle dependencies

带参数执行gradle强制刷新~/.gradle下的文件缓存

1
gradle build --refresh-dependencies

Gradle中添加 LombOk 依赖

编辑 build.gradle 文件, 添加依赖

1
2
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'

IntelliJ IDEA > Menu > build > Execute > Deployment > Compile > Annotation Processors

打包jar

找包一个jar 不带依赖jar

需要 plugin java 即可,无需其它配置

1
gradle jar  

打包一个jar

1
2
3
4
5
6
7
8
9
10
11
12
jar {
from {
//添加依懒到打包文件
//configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtime.collect{zipTree(it)}
}
manifest {
attributes 'Main-Class': 'cc.stan.example.Application'
}
baseName = '包的名字'
version = '0.0.1-RELEASE'
}

执行命令 gradle build 或者 ./gradlew build ,可在build/libs 查看生成的jar包

打包成多个jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
jar {
manifest {
attributes 'Main-Class': 'cc.stan.example.Application'
}
}

task clearJar(type: Delete) {
delete 'build/libs/lib'
}

task copyJar(type: Copy) {
from configurations.runtime
into('build/libs/lib')
}

task release(type: Copy, dependsOn: [build, clearJar, copyJar])

执行命令 gradle release 或者./gradlew relesse ,可在 build/libs 查看生成的jar包

跳过测试

gradle build -x test