8
8
- Kotlin
9
9
---
10
10
11
+ Java、PHP 作为中国软件外包行业的扛把子,自然是码农最多、甲方最爱的后端技术栈,我自主创业以来也有不少甲方要求 Java + Spring 的架构。
12
+
13
+ 同时,JSer 一直很反感 Java 庞大的代码量,即便后来大行其道的 [ TypeScript] [ 1 ] 为 [ JavaScript] [ 2 ] 生态引入强大的** 类型系统** ,但感谢 TS 之父也创造了 [ C#] [ 3 ] ,其代码也比 Java 简洁很多。
14
+
15
+ 于是,Java IDE 界新星 IntelliJ IDEA 的东家 JetBrains 发明的 [ Kotlin 语言] [ 4 ] ,便因其现代性而受到 [ Google Android] [ 5 ] 和我的青睐~
16
+
17
+ 虽然 Kotlin 官网首页非常小清新地给初学者展示了语言的几大核心用法,但正式文档的入口却繁杂而令人困惑,让我这种觉得他们家 IDE 太重的 [ VS Code] [ 6 ] 铁粉无所适从……
18
+
19
+ 故而有了以下徒手学习之旅 ——
20
+
11
21
## Hello, Kotlin!
12
22
13
23
### Just for "fun"
@@ -20,11 +30,11 @@ fun main() {
20
30
}
21
31
```
22
32
23
- 此处参考了 https://kotlinlang.org/#try-kotlin
33
+ > 参考: https://kotlinlang.org/#try-kotlin
24
34
25
35
### 安装编译器
26
36
27
- https://tech-query.me/development/coder-start-kit/
37
+ 2018 年做 Python 教练的经历,给了我 [ 装环境一把梭 ] [ 7 ] 的底气。
28
38
29
39
#### Windows
30
40
@@ -40,15 +50,19 @@ brew install kotlin
40
50
41
51
### 运行
42
52
53
+ 我只实验成功了“编译为 JAR 包再运行”的方法:
54
+
43
55
``` shell
44
56
kotlinc hello.kt -include-runtime -d hello.jar
45
57
java -jar hello.jar
46
58
```
47
59
48
- 此处参考了 https://www.runoob.com/kotlin/kotlin-command-line.html
60
+ > 参考: https://www.runoob.com/kotlin/kotlin-command-line.html
49
61
50
62
## Boot Spring
51
63
64
+ [ Spring Boot] [ 8 ] 算是 Java 最典范的后端 MVC 框架,再配上 Kotlin,写起来很像 [ Node.js + TypeScript] [ 9 ] 、Python + [ Flask] [ 10 ] /Django 等技术栈,利于工程师在它们之间风骚走位,自然值得学习。
65
+
52
66
### 下载脚手架
53
67
54
68
``` shell
@@ -61,10 +75,16 @@ curl https://start.spring.io/starter.zip \
61
75
-o web.zip
62
76
```
63
77
64
- 此处参考了 https://spring.io/guides/tutorials/spring-boot-kotlin/#using-command-line
78
+ > 参考: https://spring.io/guides/tutorials/spring-boot-kotlin/#using-command-line
65
79
66
80
### 安装包管理器
67
81
82
+ 我选择 [ Gradle] [ 11 ] 作为 JVM 语言的包管理器,主要因为它的配置文件不像老牌 Maven 的 XML 那样繁杂,而且支持 ** Kotlin 脚本语法** ,让我可以全身心地学习 Kotlin。
83
+
84
+ 感谢 [ @mythcsj ] [ 12 ] 的解答,让我理解了:
85
+
86
+ > 对于 JSer 而言,Maven 类似 NPM,既是个包管理器,也有自建包服务器;而 Gradle 类似 [ Yarn] [ 13 ] ,是构建在前者成熟包服务器之上的新包管理器。
87
+
68
88
#### Windows
69
89
70
90
``` powershell
@@ -79,6 +99,10 @@ brew install gradle
79
99
80
100
### 墙内镜像
81
101
102
+ 懂的都懂,感谢阿里~
103
+
104
+ ` build.gradle.kts `
105
+
82
106
``` diff
83
107
repositories {
84
108
mavenCentral()
@@ -89,40 +113,173 @@ repositories {
89
113
}
90
114
```
91
115
92
- 此处参考了 https://developer.aliyun.com/mvn/guide
93
-
94
- ### 运行
95
-
96
- ``` shell
97
- gradle build
98
- java -jar build/libs/demo-0.0.1-SNAPSHOT.jar
99
- ```
100
-
101
- 此处参考了 https://spring.io/guides/gs/rest-service/#_build_an_executable_jar
116
+ > 参考:https://developer.aliyun.com/mvn/guide
102
117
103
118
### Hello, RESTful!
104
119
105
- ` src\main\kotlin\com\example\web\HttpControllers .kt `
120
+ ` src\main\kotlin\com\example\web\HelloController .kt `
106
121
107
122
``` kotlin
123
+ package com.example.web ;
124
+
108
125
import org.springframework.web.bind.annotation.RestController;
109
126
import org.springframework.web.bind.annotation.GetMapping;
127
+ import org.springframework.web.bind.annotation.ResponseBody;
110
128
111
129
@RestController
112
130
class HelloController {
113
131
@GetMapping(" /" )
114
- fun foo (): MutableMap <String , Int > {
132
+ @ResponseBody
133
+ fun getData (): MutableMap <String , Int > {
115
134
return mutableMapOf (" a" to 1 )
116
135
}
117
136
}
118
137
```
119
138
120
- 此处参考了:
139
+ > 参考:
140
+ >
141
+ > 1 . https://spring.io/guides/tutorials/spring-boot-kotlin/#_exposing_http_api
142
+ > 2 . https://spring.io/guides/gs/rest-service/#_create_a_resource_controller
143
+
144
+ ### 运行
145
+
146
+ 感谢 [ @aruis ] [ 14 ] 的解答,才让我知道官方脚手架 ` HELP.md ` 一大堆链接里都没明确提及的本地运行命令是啥……
147
+
148
+ ``` shell
149
+ ./gradlew bootRun
150
+ ```
121
151
122
- 1 . https://spring.io/guides/tutorials/spring-boot-kotlin/#_exposing_http_api
123
- 2 . https://spring.io/guides/gs/rest-service/#_create_a_resource_controller
152
+ ### Docker 镜像
153
+
154
+ 再次感谢 [ @aruis 发起的 Pull Request] [ 15 ] ,不仅帮我修复了团队脚手架的 bug,还写了 ` Dockerfile ` :
155
+
156
+ ``` dockerfile
157
+ FROM openjdk:11.0-jre-slim
158
+
159
+ ENV JAR_FILE_NAME demo-0.0.1-SNAPSHOT.jar
160
+ COPY build/libs/$JAR_FILE_NAME app.jar
161
+ EXPOSE 8080
162
+ ENTRYPOINT ["java" , "-jar" , "app.jar" ]
163
+ ```
164
+
165
+ 于是,我们可以在传统构建后,再构建 Docker 镜像:
166
+
167
+ ``` shell
168
+ ./gradlew build
169
+ docker build .
170
+ ```
171
+
172
+ > 参考:https://spring.io/guides/gs/spring-boot-docker/
173
+
174
+ ### GitHub actions + packages
175
+
176
+ 每次手动构建 Docker 镜像很烦,[ Docker Hub] [ 16 ] 官方账号我也总是忘记,那就 GitHub [ actions] [ 17 ] + [ packages] [ 18 ] 服务一把梭吧~
177
+
178
+ ` .github\workflows\release.yml `
179
+
180
+ ``` yaml
181
+ name : Release Docker image
182
+ on :
183
+ push :
184
+ tags :
185
+ - v*
186
+ jobs :
187
+ push_to_registries :
188
+ name : Push Docker image to GitHub registry
189
+ runs-on : ubuntu-latest
190
+ permissions :
191
+ packages : write
192
+ contents : read
193
+ steps :
194
+ - name : Check out the repo
195
+ uses : actions/checkout@v2
196
+ - name : Set up JDK 17
197
+ uses : actions/setup-java@v2
198
+ with :
199
+ java-version : 17
200
+ distribution : adopt
201
+ cache : gradle
202
+ - name : Set up QEMU
203
+ uses : docker/setup-qemu-action@v1
204
+ - name : Set up Docker Buildx
205
+ uses : docker/setup-buildx-action@v1
206
+ - name : Login to GitHub Container Registry
207
+ uses : docker/login-action@v1
208
+ with :
209
+ registry : ghcr.io
210
+ username : ${{ github.repository_owner }}
211
+ password : ${{ secrets.GITHUB_TOKEN }}
212
+ - name : Build Kotlin
213
+ run : |
214
+ chmod +x ./gradlew
215
+ ./gradlew build
216
+ - id : ImageName
217
+ uses : ASzc/change-string-case-action@v2
218
+ with :
219
+ string : ${{ github.repository }}
220
+ - name : Push Image
221
+ uses : docker/build-push-action@v2
222
+ with :
223
+ context : .
224
+ platforms : linux/amd64
225
+ push : true
226
+ tags : |
227
+ ghcr.io/${{ steps.ImageName.outputs.lowercase }}:${{ github.ref_name }}
228
+ ghcr.io/${{ steps.ImageName.outputs.lowercase }}:latest
229
+ ` ` `
230
+
231
+ 每次发版只需用 [Git][19] 打个 tag 即可:
232
+
233
+ ` ` ` shell
234
+ git tag vX.Y.Z HEAD
235
+ git push origin --tags
236
+ ```
237
+
238
+ > 参考:
239
+ >
240
+ > 1 . https://github.com/docker/build-push-action/blob/master/docs/advanced/push-multi-registries.md
241
+ > 2 . https://tomgregory.com/build-gradle-projects-with-github-actions/
242
+
243
+ ### 模板仓库
244
+
245
+ 借助 [ GitHub template 仓库] [ 20 ] 这一实用新特性,我们可以轻松创建新项目,最大限度地复用团队沉淀下来的项目公用代码。
246
+
247
+ 以上讲解的所有内容都汇总在我们 [ idea2app 团队] [ 21 ] 的脚手架中,欢迎大家试用、反馈:
248
+
249
+ https://github.com/idea2app/Kotlin-Spring-Boot
250
+
251
+ ## VS Code 扩展
252
+
253
+ 对于近些年风靡 ** Web 全栈开发** 领域的 VS Code,Java 生态支持自然不能少,虽然以下两个推荐的扩展插件还都是* 预览版* ,但对于日常应用开发足够了:
254
+
255
+ 1 . https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack
256
+ 2 . https://marketplace.visualstudio.com/items?itemName=fwcd.kotlin
257
+
258
+ > 参考:https://www.jianshu.com/p/90158cdc6d18
124
259
125
260
## 更多资料
126
261
127
262
1 . Kotlin 常用代码:https://devhints.io/kotlin
128
263
2 . Kotlin 文档速查:https://devdocs.io/kotlin~1.6/
264
+
265
+ [ 1 ] : https://www.typescriptlang.org/
266
+ [ 2 ] : https://jscig.github.io/
267
+ [ 3 ] : https://docs.microsoft.com/en-us/dotnet/csharp/
268
+ [ 4 ] : https://kotlinlang.org/
269
+ [ 5 ] : https://www.android.com/
270
+ [ 6 ] : https://code.visualstudio.com/
271
+ [ 7 ] : https://tech-query.me/development/coder-start-kit/
272
+ [ 8 ] : https://spring.io/projects/spring-boot
273
+ [ 9 ] : https://github.com/idea2app/NodeTS-LeanCloud
274
+ [ 10 ] : https://flask.palletsprojects.com/
275
+ [ 11 ] : https://gradle.org/
276
+ [ 12 ] : https://github.com/mythcsj
277
+ [ 13 ] : https://yarnpkg.com/
278
+ [ 14 ] : https://github.com/aruis
279
+ [ 15 ] : https://github.com/idea2app/Kotlin-Spring-Boot/pull/2
280
+ [ 16 ] : https://hub.docker.com/
281
+ [ 17 ] : https://github.com/features/actions
282
+ [ 18 ] : https://github.com/features/packages
283
+ [ 19 ] : https://git-scm.com/
284
+ [ 20 ] : https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-template-repository
285
+ [ 21 ] : https://ideapp.dev/
0 commit comments