Skip to content

Commit da3e777

Browse files
committed
[add] more sections of Kotlin post
[fix] meta data of 2 posts
1 parent 055cd86 commit da3e777

File tree

3 files changed

+182
-20
lines changed

3 files changed

+182
-20
lines changed

source/_posts/Community/spiritual-inspiration-of-Open-Source-to-China.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
---
22
title: 开源给中国的精神启示
3-
slidehtml: true
3+
date: 2021-10-25 21:00:00
44
categories:
55
- Community
6+
tags:
7+
- open-source
8+
- China
9+
slidehtml: true
610
---
711

812
COSCon 2021

source/_posts/Development/Web-progress-2021.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: 2021 年的 Web 标准进展
3+
date: 2021-12-11 14:00:00
34
categories:
45
- Development
56
tags:

source/_posts/Development/start-Kotlin-without-IDE.md

Lines changed: 176 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ tags:
88
- Kotlin
99
---
1010

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+
1121
## Hello, Kotlin!
1222

1323
### Just for "fun"
@@ -20,11 +30,11 @@ fun main() {
2030
}
2131
```
2232

23-
此处参考了 https://kotlinlang.org/#try-kotlin
33+
> 参考:https://kotlinlang.org/#try-kotlin
2434
2535
### 安装编译器
2636

27-
https://tech-query.me/development/coder-start-kit/
37+
2018 年做 Python 教练的经历,给了我[装环境一把梭][7]的底气。
2838

2939
#### Windows
3040

@@ -40,15 +50,19 @@ brew install kotlin
4050

4151
### 运行
4252

53+
我只实验成功了“编译为 JAR 包再运行”的方法:
54+
4355
```shell
4456
kotlinc hello.kt -include-runtime -d hello.jar
4557
java -jar hello.jar
4658
```
4759

48-
此处参考了 https://www.runoob.com/kotlin/kotlin-command-line.html
60+
> 参考:https://www.runoob.com/kotlin/kotlin-command-line.html
4961
5062
## Boot Spring
5163

64+
[Spring Boot][8] 算是 Java 最典范的后端 MVC 框架,再配上 Kotlin,写起来很像 [Node.js + TypeScript][9]、Python + [Flask][10]/Django 等技术栈,利于工程师在它们之间风骚走位,自然值得学习。
65+
5266
### 下载脚手架
5367

5468
```shell
@@ -61,10 +75,16 @@ curl https://start.spring.io/starter.zip \
6175
-o web.zip
6276
```
6377

64-
此处参考了 https://spring.io/guides/tutorials/spring-boot-kotlin/#using-command-line
78+
> 参考:https://spring.io/guides/tutorials/spring-boot-kotlin/#using-command-line
6579
6680
### 安装包管理器
6781

82+
我选择 [Gradle][11] 作为 JVM 语言的包管理器,主要因为它的配置文件不像老牌 Maven 的 XML 那样繁杂,而且支持 **Kotlin 脚本语法**,让我可以全身心地学习 Kotlin。
83+
84+
感谢 [@mythcsj][12] 的解答,让我理解了:
85+
86+
> 对于 JSer 而言,Maven 类似 NPM,既是个包管理器,也有自建包服务器;而 Gradle 类似 [Yarn][13],是构建在前者成熟包服务器之上的新包管理器。
87+
6888
#### Windows
6989

7090
```powershell
@@ -79,6 +99,10 @@ brew install gradle
7999

80100
### 墙内镜像
81101

102+
懂的都懂,感谢阿里~
103+
104+
`build.gradle.kts`
105+
82106
```diff
83107
repositories {
84108
mavenCentral()
@@ -89,40 +113,173 @@ repositories {
89113
}
90114
```
91115

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
102117
103118
### Hello, RESTful!
104119

105-
`src\main\kotlin\com\example\web\HttpControllers.kt`
120+
`src\main\kotlin\com\example\web\HelloController.kt`
106121

107122
```kotlin
123+
package com.example.web;
124+
108125
import org.springframework.web.bind.annotation.RestController;
109126
import org.springframework.web.bind.annotation.GetMapping;
127+
import org.springframework.web.bind.annotation.ResponseBody;
110128

111129
@RestController
112130
class HelloController {
113131
@GetMapping("/")
114-
fun foo(): MutableMap<String, Int> {
132+
@ResponseBody
133+
fun getData(): MutableMap<String, Int> {
115134
return mutableMapOf("a" to 1)
116135
}
117136
}
118137
```
119138

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+
```
121151

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
124259
125260
## 更多资料
126261

127262
1. Kotlin 常用代码:https://devhints.io/kotlin
128263
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

Comments
 (0)