详尽kapt的使用说明,将一些模棱两可,可能导致错误的地方进行了指正和说明。新的kapt在maven中的配置得到了稳定,现在它可以在运行前如期生成代码了。

This commit is contained in:
CloudPlayer 2023-08-09 22:20:04 +08:00
parent d6bff881f6
commit f1c2cd7725
2 changed files with 48 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

View File

@ -19,12 +19,12 @@
```groovy
plugins {
id "org.jetbrains.kotlin.kapt" version "1.9.0"
id 'org.jetbrains.kotlin.kapt' version '1.9.0'
}
```
2. 在 dependencies 块中使用 kapt 配置添加相应的依赖项
**【Kotlin】**
**【Kotlin】**
```kotlin
dependencies {
@ -33,6 +33,7 @@ dependencies {
```
**【Groovy】**
```groovy
dependencies {
kapt 'org.springframework.boot:spring-boot-configuration-processor:1.5.6'
@ -40,9 +41,11 @@ dependencies {
```
## 在Maven中使用
在 compile 之前在 kotlin-maven-plugin 中添加 kapt 目标的执行:
1. 将以下kapt配置插入指定位置。
```xml
<execution>
<id>kapt</id>
<goals>
@ -60,4 +63,45 @@ dependencies {
</execution>
```
> 关于Kapt更详细的说明请看[Kotlin官网说明](https://book.kotlincn.net/text/kapt.html),或[Kotlin语言中文站](https://www.kotlincn.net/docs/reference/kapt.html)。
你需要使kapt在compile前工作。将其插入到`kotlin-maven-plugin`中的compile前
然后将compile的时机改为`process-sources`
```xml
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<!--上述代码需插入到此处compile前-->
<execution>
<id>compile</id>
<!--将此处的phase改为process-sources-->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
```
2. 令kapt在构建前运行
以idea举例
1. 点击maven图标
2. 找到对应项目
3. 点击插件
4. 点击kotlin
5. 右击kotlin:kapt在选项中点击"**构建前执行**"以让kapt能够正确的生成代码。
![](../../assets/images/kapt1.png)
> 关于Kapt更详细的说明请看[Kotlin官网说明](https://book.kotlincn.net/text/kapt.html)
> ,或[Kotlin语言中文站](https://www.kotlincn.net/docs/reference/kapt.html)。