Mrli
别装作很努力,
因为结局不会陪你演戏。
Contacts:
QQ博客园

Gradle学习

2021/12/02 Java
Word count: 1,044 | Reading time: 5min

Gradle学习

在kotlin中使用mapstruct出现了些问题, 网上搜寻后得到结论:需要使用kapt(Kotlin annotation processing tool 即kotlin注解处理工具), 但给出的大多都是通过Gradle来进行配置的。因此借此机会学习了下Gradle:

plugins和apply plugin:

由于文档写到了, 由于不清楚这些在哪些位置加入, 因此查看了plugins和apply plugin:的区别:

1
2
3
4
5
6
7
8
9
10
11
> 添加 kapt 插件
> apply plugin: 'kotlin-kapt'
>
> 然后在项目中添加如下依赖:
> api("com.github.pozo:mapstruct-kotlin:1.3.1.2")
> kapt("com.github.pozo:mapstruct-kotlin-processor:1.3.1.2")
>
> 另外,还需要添加如下依赖:
> api("org.mapstruct:mapstruct:1.4.0.Beta3")
> kapt("org.mapstruct:mapstruct-processor:1.4.0.Beta3")
>

“plugins {}”块导入的是Gradle官方插件仓库里的插件。如果使用“buildscript {}”块指定第三方库作为Gradle插件的话,指定插件就需要使用“apply plugin”了。

刚刚翻了翻文档,“apply plugin”本身似乎还有更多的用途,而“plugins {}”块似乎是一个新引入的还不足够稳定的特性。题主想要知道更多的话直接看看文档就行了。

最终的Plain项目的依赖如下所示:

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
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

apply plugin: 'kotlin-kapt'

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

api("com.github.pozo:mapstruct-kotlin:1.3.1.2")
kapt("com.github.pozo:mapstruct-kotlin-processor:1.3.1.2")
api("org.mapstruct:mapstruct:1.4.0.Beta3")
kapt("org.mapstruct:mapstruct-processor:1.4.0.Beta3")
}

test {
useJUnitPlatform()
}

maven使用kapt

在 kotlin-maven-plugin 中的在compile 之前添加 kapt 目标的执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<!-- 在此处指定你的注解处理器。 -->
<annotationProcessorPath>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.9</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>

请注意,IntelliJ IDEA 自身的构建系统目前还不支持 kapt。当你想要重新运行注解处理时,请从“Maven Projects”工具栏启动构建。

from: https://www.kancloud.cn/alex_wsc/android_kotlin/1318386#_Maven__110

gradle中api、implementation和compile的区别

我们在gradle中引用第三方maven库时,一般通过下面的语句去引用:compile 'com.android.support:appcompat-v7:26.1.0' , 然后sync project,就能成功引用对应的包到项目之中了,十分方便。除了使用compile关键字,使用implementation和api也能将包引用到项目中

api和compile关键字作用效果是一样的,使用时可以互相替换。实际上,api关键字是用来替代compile关键字的,因为compile关键字将来会被弃用。在高版本的gradle,使用compile关键字会报错并提示使用api关键字代替。

Q:看起来这三个关键字的作用是一样的,那么,他们到底有什么区别呢?

A:在同一个module下,implementation和compile的使用效果相同,但是在不同module下,就会有所区别了。

api或compile关键字引用的包对于其他module来说是可见的,而implementation关键字引用的包对于其他module来说是不可见的。

Maven仓库列表

仓库名 简介 实际地址 使用地址
jcenter JFrog公司提供的仓库 http://jcenter.bintray.com https://maven.aliyun.com/repository/jcenter https://maven.aliyun.com/nexus/content/repositories/jcenter
mavenLocal 本台电脑上的仓库 {USER_HOME}/.m2/repository C:/Users/liyujiang/.m2/repository (Windows) /home/liyujiang/.m2/repository (Linux)
mavenCentral Sonatype公司提供的中央库 http://central.maven.org/maven2 https://maven.aliyun.com/repository/central https://maven.aliyun.com/nexus/content/repositories/central
google Google公司提供的仓库 https://maven.google.com https://maven.aliyun.com/repository/google https://maven.aliyun.com/nexus/content/repositories/google https://dl.google.com/dl/android/maven2
jitpack JitPack提供的仓库 https://jitpack.io https://jitpack.io
public jcenter和mavenCentral的聚合仓库 https://maven.aliyun.com/repository/public https://maven.aliyun.com/nexus/content/groups/public
gradle-plugin Gradle插件仓库 https://plugins.gradle.org/m2 https://maven.aliyun.com/repository/gradle-plugin https://maven.aliyun.com/nexus/content/repositories/gradle-plugin
1
2
3
4
5
6
7
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}

jcenter()
}

gradle全局换源

kotlin-mapstruct

https://www.imooc.com/article/309984

Author: Mrli

Link: https://nymrli.top/2021/12/02/Gradle学习/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
Mongodb学习
NextPost >
Vue-Router细节记录
CATALOG
  1. 1. Gradle学习
    1. 1.1. plugins和apply plugin:
    2. 1.2. maven使用kapt
    3. 1.3. gradle中api、implementation和compile的区别
    4. 1.4. Maven仓库列表
      1. 1.4.1. gradle全局换源
    5. 1.5. kotlin-mapstruct