从github下载Flink的源码:https://github.com/apache/flink/archive/refs/tags/release-1.15.0.zip
然后进行解压
使用IDEA打开Flink-release-1.15.0,然后配置maven 3.2.5,再配置Java11
对于本地已经配置了其它版本的maven,使用命令行时,可以配置命令行的临时maven
D:\self-directory\Flink-source-read\flink-release-1.15.0>set JAVA_HOME=D:\install_software\java11\zulu11.56.19-ca-jdk11.0.15-win_x64
D:\self-directory\Flink-source-read\flink-release-1.15.0>
D:\self-directory\Flink-source-read\flink-release-1.15.0>set M2_HOME=D:\install_software\maven\apache-maven-3.2.5
D:\self-directory\Flink-source-read\flink-release-1.15.0>
D:\self-directory\Flink-source-read\flink-release-1.15.0>set PATH=%M2_HOME%\bin;%PATH%
D:\self-directory\Flink-source-read\flink-release-1.15.0>
D:\self-directory\Flink-source-read\flink-release-1.15.0>mvn -v
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-15T01:29:23+08:00)
Maven home: D:\install_software\maven\apache-maven-3.2.5
Java version: 11.0.15, vendor: Azul Systems, Inc.
Java home: D:\install_software\java11\zulu11.56.19-ca-jdk11.0.15-win_x64
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "dos"
D:\self-directory\Flink-source-read\flink-release-1.15.0>
最后导入Flink的各种dependencies
D:\self-directory\Flink-source-read\flink-release-1.15.0> mvn spotless:apply
不然会报异常:The following files had format violations
慢速编译方法如下:
D:\self-directory\Flink-source-read\flink-release-1.15.0> mvn clean install -DskipTests -Drat.skip=true -Pjava11-target
参数说明如下:
快速编译方法如下:
D:\self-directory\Flink-source-read\flink-release-1.15.0> mvn clean install -DskipTests -Dfast -Pjava11-target,skip-webui-build -T 1C
参数说明如下:
编译后的可执行文件不是一个jar包,是一个文件夹:flink-release-1.15.0\flink-dist\target\flink-1.15.0-bin\flink-1.15.0
protoc did not exit cleanly
这是protobuf写入protoc文件失败,导致protoc退出异常。我这里的原因是flink-release-1.15.0文件夹的父文件夹名称包含中文,将父文件夹名称修改成英文就好了
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.4.2:apply (default-cli) on project flink-annotations: Execution default-cli
of goal com.diffplug.spotless:spotless-maven-plugin:2.4.2:apply failed: Unable to resolve dependencies: Failed to collect dependencies at com.google.go
oglejavaformat:google-java-format:jar:1.7 -> com.google.errorprone:javac-shaded:jar:9+181-r4173-1: Failed to read artifact descriptor for com.google.err
orprone:javac-shaded:jar:9+181-r4173-1: Could not transfer artifact com.google.errorprone:javac-shaded:pom:9+181-r4173-1 from/to alimaven (https://maven
.aliyun.com/repository/central): authorization failed for https://maven.aliyun.com/repository/central/com/google/errorprone/javac-shaded/9+181-r4173-1/j
avac-shaded-9+181-r4173-1.pom, status: 403 Forbidden -> [Help 1]
[ERROR]
查看aliyun仓库有这个pom文件和jar包,但是去下载的时候,还是会出错
这大概率是你使用的maven版本不对,推荐使用maven 3.2.5,因为其它版本maven不能shade away certain dependencies。如果还是报错,可以使用如下3种方法的其中1种
第一:换成从maven central下载
第二:手动从maven central下载pom、pom.sha1、jar、jar.sha1到本地仓库
第三:添加
到pom.xml(flink-parent)如下位置
......省略部分......
......省略部分......
com.diffplug.spotless
spotless-maven-plugin
${spotless.version}
true
1.7
org.apache.flink,org.apache.flink.shaded,,javax,java,scala,\#
spotless-check
validate
check
......省略部分......
让flink不使用spotless-maven-plugin插件,这样也就不会去下载javac-shaded的pom和jar包了
这样编译flink源码的时候就不会进行QA代码审查。同样也不用执行mvn spotless:apply
命令对代码进行格式化了
使用IDEA调试Flink程序时,会报如下错误:
java: 程序包sun.misc不存在
但是我们看IDEA配置的zulu-11的jdk-unsupported模块下是有sun.misc的,但是它明确表明这是不支持的。而flink-parent这个pom默认是使用jdk8的,所以才会报程序包不存在的错误
在flink-parent的pom中给id为java11-target的profile,配置activateByDefault为true,这个对IDEA调式程序是不生效的。可以通过如下方式在IDEA中明确激活profile
先选择java11-target这个profile,然后点击刷新按钮进行刷新,就可以在IDEA中使用jdk11进行调试了