目录
Elasticsearch 源码构建、导入IDEA
/    

Elasticsearch 源码构建、导入IDEA

Elasticsearch 6.3.2 源码构建

1、下载 elasticsearch 6.3.2的源码

2、进入 elasticsearch 项目根目录

3、运行命令 ./gradlew check 会下载符合当前es版本的gradle工具

4、配置 gradle 环境变量

报错1:

导入 idea 报错

You must run gradle idea from the root of elasticsearch before importing into IntelliJ

执行 gradle idea

如果提示证书问题,就换仓库地址

allprojects{
    repositories {
		mavenLocal()
        maven {
            url 'http://jcenter.bintray.com'
        }
    }
}

报错2:

the environment variable JAVA_HOME must be set to a JDK installation directory for Java 1.10 but is [/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home] corresponding to [1.8]

下载 jdk 1.10

由于 6.3.2 的版本只能用 jdk 10,mac m1 只能zulu jdk,而zulu jdk 没有 10 版本的,放弃了。

window 构建 elasticsearch 6.3.2

报错1:

expecting token of type [START_OBJECT] but found [VALUE_STRING]]

D:\DeskTop\work\2021-06\elasticsearch\distribution\src\config\elasticsearch.yml 中默认是 ${path.logs} 获取的,应该是没有识别到,这里我们手动写死目录

# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: D:\DeskTop\work\2021-06\elasticsearch\data
#
# Path to log files:
#
path.logs: D:\DeskTop\work\2021-06\elasticsearch\logs

报错2:

Caused by: org.elasticsearch.bootstrap.BootstrapException: java.nio.file.NoSuchFileException: D:\DeskTop\work\2021-06\elasticsearch\modules\aggs-matrix-stats\plugin-descriptor.properties

在官网下载已经编译好的 elasticsearch 6.3.2 二进制包,然后将 modules 目录下的所有的文件 复制到 源码中的 modules 目录下。

报错3:

Caused by: org.elasticsearch.bootstrap.BootstrapException: java.nio.file.NoSuchFileException: D:\DeskTop\work\2021-06\elasticsearch\modules\build.gradle\plugin-descriptor.properties

我们将 源码中的 modules 目录下的 build.gradle 文件删除。

Elasticsearch 7.17源码构建

1、下载源码

2、安装 jdk 16

3、执行命令 ./gradlew check 获取对应版本的gradle

4、执行命令 gradle idea 会发现新版本使用这个命令已经不行了,CONTRIBUTING.md 文档上有描述如何导入IDEA

直接导入IDEA,刷新 gradle

如果使用阿里的镜像仓库,可能会有些 jar 包不存在

gradle 的全局配置文件 ~/.gradle/init.gradle

切换成maven原始的仓库

allprojects{
    repositories {
		mavenLocal()
        // def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/nexus/content/groups/public'
        // def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/nexus/content/repositories/jcenter'

		def ALIYUN_JCENTER_URL = 'https://repo1.maven.org/maven2/'
		def ALIYUN_REPOSITORY_URL = 'https://repo1.maven.org/maven2/'
        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
        }
    }
}

运行 org.elasticsearch.bootstrap.Elasticsearch main 方法

1、报错1:

ERROR: the system property [es.path.conf] must be set

-Des.path.conf=/Users/gitsilence/OpenSource/elasticsearch-7.17/distribution/src/config -Des.path.home=/Users/gitsilence/OpenSource/elasticsearch-7.17

2、报错2

2022-01-02 14:47:30,229 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")

编辑启动配置,设置VM参数:-Dlog4j2.disable.jmx=true

3、报错3:

found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 33, column 1:
    @path.data@

4、报错4:

gradle found character '@' that cannot start any token. (Do not use @ for indentation)

暂时找不到好的解决办法,项目根目录下创建 data 和 logs 目录

然后修改 /Users/gitsilence/OpenSource/elasticsearch-7.17/distribution/src/config/elasticsearch.yml 文件

#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /Users/gitsilence/OpenSource/elasticsearch-7.17/data
#
# Path to log files:
#
path.logs: /Users/gitsilence/OpenSource/elasticsearch-7.17/logs

5、报错5:

org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: modules directory [/Users/gitsilence/OpenSource/elasticsearch-7.17/distribution/modules] not found

This is mainly the runtime , By default, the configuration file under the plug-in or module will be read , terms of settlement , Use the release version elasticsearch-7.10.2-windows-x86_64 Of plugins and modules All replacement .

百度翻译:这主要是在运行时,默认情况下,插件或模块下的配置文件将被读取,结算条款,使用elasticsearch-7.10.2-windows-x86_64发布版本的插件和模块全部替换。

这里不太容易明白,这里的意思应该这个模块主要用于运行时,将这个模块打包后,直接引用就行了。

我这里处理方式,去官网下载了编译好的,然后将这两个模块(plugins和modules)复制进来。

plugins 我直接干掉了🤡

6、报错6

Failed to apply plugin [id 'elasticsearch.build']
the environment xxx Java 11

JAVA_HOME 明明是 java 10,报错说 不用用 java 11构建。

直接修改文件 buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

找到

final String javaHome = System.getenv('JAVA_HOME')

修改成固定路径

final String javaHome = "D:\\Program Files\\jdk\\jdk-10.0.2+13"

Elasticsearch 7.16.2 源码构建

此版本 目前 是最新的release 版本。

老样子

1、./gradlew check

有报错,估计是jdk版本的,先导入 idea,然后配置指定版本的jdk

为gradle 配置 jdk 16,编译成功

参考7.17 版本遇到的报错问题 解决

报错1、:

/Users/gitsilence/OpenSource/elasticsearch-7.16.2/modules/elasticsearch.modules.iml/plugin-descriptor.properties: Not a directory

把modules 移除模块,删除.impl、build.gradle

报错2、:

org.elasticsearch.bootstrap.StartupException: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "createClassLoader")

启动的VM参数中指定这个policy文件:

-Djava.security.policy=your_path/my.policy

3、elasticsearch.yml 文件中配置:

xpack.ml.enabled: false

加上自定义的 my.policy

my.policy

grant {
	permission javax.management.MBeanTrustPermission "register";
	permission javax.management.MBeanServerPermission "createMBeanServer";
	permission java.lang.RuntimePermission "createClassLoader";
	permission java.lang.RuntimePermission "getClassLoader";
	permission java.lang.RuntimePermission "setContextClassLoader";
};

删除 /Users/gitsilence/OpenSource/elasticsearch-7.16.2/server/src/main/resources/org/elasticsearch/bootstrap/security.policy 中一些内容

到这里已经可以启动了

报错3、:

java.security.AccessControlException: access denied ("org.elasticsearch.secure_sm.ThreadPermission" "modifyArbitraryThreadGroup")

在 my.policy 文件中加入内容

// 生产环境下,不必要的权限不要开
permission org.elasticsearch.secure_sm.ThreadPermission "modifyArbitraryThreadGroup";
permission org.elasticsearch.secure_sm.ThreadPermission "modifyArbitraryThread";

运行成功

😑😑😑

总结

1、下载源码

2、下载合适 gradle 和 jdk 版本(jdk 版本一般报错都有提示,下载之后,给gradle配置对应的jdk版本)

3、导入到IDEA,保证编译成功(具体问题具体分析)

3、main 方法 org.elasticsearch.bootstrap.Elasticsearch#main(java.lang.String[])

4、配置VM参数

-Des.path.conf=/Users/gitsilence/OpenSource/elasticsearch-7.16.2/distribution/src/config -Des.path.home=/Users/gitsilence/OpenSource/elasticsearch-7.16.2 -Dlog4j2.disable.jmx=true -Djava.security.policy=/Users/gitsilence/OpenSource/elasticsearch-7.16.2/server/src/main/resources/org/elasticsearch/bootstrap/my.policy

这里的 my.policy内容

grant {
	permission javax.management.MBeanTrustPermission "register";
	permission javax.management.MBeanServerPermission "createMBeanServer";
	permission java.lang.RuntimePermission "createClassLoader";
	permission java.lang.RuntimePermission "getClassLoader";
	permission java.lang.RuntimePermission "setContextClassLoader";
	// 生产环境下,不必要的权限不要开
	permission org.elasticsearch.secure_sm.ThreadPermission "modifyArbitraryThreadGroup";
    permission org.elasticsearch.secure_sm.ThreadPermission "modifyArbitraryThread";

};

去官网下载源码对应版本已经编译好的,我们要使用它的 modules模块,直接复制我们项目里面对应目录

然后将 modules 移除模块,删除文件 xx.iml、systemd模块、build.gradle等文件,自己可以根据报错进行删除。

将 plugins 模块中的全部删除掉,会有报错,暂时没解决,那就干掉。

-Des.path.conf=/Users/gitsilence/OpenSource/elasticsearch-7.16.2/distribution/src/config 这个目录的 elasticsearch.yml

path.data: /Users/gitsilence/OpenSource/elasticsearch-7.16.2/data
#
# Path to log files:
#
path.logs: /Users/gitsilence/OpenSource/elasticsearch-7.16.2/logs

xpack.ml.enabled: false

这个文件默认是 用 @xxx@ 配置的,会报错,暂时未解决,直接写死,然后手动在项目根目录创建对应目录

参考地址

https://blog.csdn.net/CSDN_WYL2016/article/details/120552915

https://cdmana.com/2021/03/20210331123429924m.html

https://elasticsearch.cn/question/11513 答案在评论里

https://discuss.elastic.co/t/org-elasticsearch-threadpermission-modifyarbitrarythreadgroup/79527


标题:Elasticsearch 源码构建、导入IDEA
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2022/01/02/1641121296783.html