需要学习的语言:

  • java
  • kotlin

为什么要选择Kotlin而不是groovy? 我主要考量是Kotlin是DSL, 具体的一些分析可以见此链接

关于Kotlin

初始化项目

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
$ gradle init

Select type of project to generate:
1: basic
2: cpp-application
3: cpp-library
4: groovy-application
5: groovy-library
6: java-application
7: java-library
8: kotlin-application
9: kotlin-library
10: scala-library
Enter selection (default: basic) [1..10] 6

Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: groovy) [1..2]

Select test framework:
1: junit
2: testng
3: spock
Enter selection (default: junit) [1..3]

Project name (default: demo):

Source package (default: demo):


BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

init主要任务是生成wrapper文件、项目配置依赖(build.gradle.kts,settings.gradle.kts)和创建默认的项目目录.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
├── build.gradle.kts
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── src
├── main
│ └── java
│ └── demo
│ └── App.java
└── test
└── java
└── demo
└── AppTest.java

settings.gradle.kts
1
rootProject.name = "demo"
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
plugins {
java
application
}

repositories {
jcenter()
}

dependencies {
implementation("com.google.guava:guava:26.0-jre")

testImplementation("junit:junit:4.12")
}

application {
mainClassName = "demo.App"
}

dependencies : 依赖

  • implementation : 如果我们使用了implementation方式来依赖项目的话,那么这个库就在编译时期,只对当前的module可见,对其他的module不可见,但是在运行使其是可见的,这种方式的好处是可以显著减少 build项目的时间,因为假如该依赖库有接口或者代码变动,那么Gradle只会去重新编译和它有直接依赖关系的module,也就是该库不存在传递性
  • api: 使用api方式来依赖项目或者库的话,那么这个库,在编译时期和运行时期都可以对其他module可见
  • compileOnly: 假如在项目中,对某些库你只是想要在编译时期使用,而在运行时期并不需要这个库,你可以使用这种方式!
  • runtimeOnly: Gradle 在运行时会将该库添加到 build 的 output 中去

关于编译

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
> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'demo3'.
components - Displays the components produced by root project 'demo3'. [incubating]
dependencies - Displays all dependencies declared in root project 'demo3'.
dependencyInsight - Displays the insight into a specific dependency in root project 'demo3'.
dependentComponents - Displays the dependent components of components in root project 'demo3'. [incubating]
help - Displays a help message.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
model - Displays the configuration model of root project 'demo3'. [incubating]
projects - Displays the sub-projects of root project 'demo3'.
properties - Displays the properties of root project 'demo3'.
tasks - Displays the tasks runnable from root project 'demo3'.