源码环境搭建

环境准备

  • JDK 1.8
1
2
3
4
C:\Users\Administrator>java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
  • Gradle 5.6

    解压下载的安装到,移动到目标目录。添加对应 bin 目录到环境变量 path。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
C:\Users\Administrator>gradle -v

------------------------------------------------------------
Gradle 5.6.2
------------------------------------------------------------

Build time: 2019-09-05 16:13:54 UTC
Revision: 55a5e53d855db8fc7b0e494412fc624051a8e781

Kotlin: 1.3.41
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.14 compiled on March 12 2019
JVM: 1.8.0_201 (Oracle Corporation 25.201-b09)
OS: Windows 10 10.0 amd64
  • Scala
1
2
C:\Users\Administrator>scala -version
Scala code runner version 2.11.12 -- Copyright 2002-2017, LAMP/EPFL

源码下载

git clone https://gitee.com/romandata/Kafka.git

切换到对应分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ git checkout origin/2.3
Checking out files: 100% (3374/3374), done.
Note: checking out 'origin/2.3'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>

HEAD is now at 3e3419a... Add recent versions of Kafka to the matrix of ConnectD istributedTest (#7024)

修改源码目录下的 gradle.properties 文件,修改scala版本为本机环境的scala版本。

源码目录下执行 gradle idea 或者直接用IDEA打开项目,指定 gradle home。

编译慢的原因是 gradle 镜像在国外,修改为使用阿里云镜像:

  1. gradle home下的init.d目录下加入一个名叫 init.gradle 的文件

  2. 添加以下配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    allprojects{
    repositories {
    def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
    def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
    all { ArtifactRepository repo ->
    if(repo instanceof MavenArtifactRepository){
    def url = repo.url.toString()
    if (url.startsWith('https://repo1.maven.org/maven2')) {
    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
    remove repo
    }
    if (url.startsWith('https://jcenter.bintray.com/')) {
    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
    remove repo
    }
    }
    }
    maven {
    url ALIYUN_REPOSITORY_URL
    url ALIYUN_JCENTER_URL
    }
    }
    }

如果出现以下错误

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
kafka-1.1.1-src gradle idea

> Configure project :
Building project 'core' with Scala version 2.11.12

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/bibo/.Trash/kafka-1.1.1-src/build.gradle' line: 552

* What went wrong:
A problem occurred evaluating root project 'kafka-1.1.1-src'.
> Failed to apply plugin [id 'org.scoverage']
> Could not create an instance of type org.scoverage.ScoverageExtension.
> You can't map a property that does not exist: propertyName=testClassesDir

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2s

解决方法参考:KAFKA-7706

修改 build.gradle 文件,将org.scoverage:gradle-scoverage 版本修改,2.1.0修改为2.5.0,重新执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
buildscript {
repositories {
mavenCentral()
jcenter()
}
apply from: file('gradle/buildscript.gradle'), to: buildscript

dependencies {
// For Apache Rat plugin to ignore non-Git files
classpath "org.ajoberstar:grgit:1.7.0"
classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0'
classpath 'org.scoverage:gradle-scoverage:2.5.0' // 之前是2.1.0
}
}

IDEA 打开项目目录