Idea 导入ES源码工程
准备工作
1)导入源码前重新启动电脑,保证gradle、jdk9和IntelliJ IDEA 2017.2.3环境是好的
源码根目录
E:\workspace\idea\elasticsearch-6.2.4
导入步骤
根据CONTRIBUTING.md说明:
then `File->New Project From Existing Sources`.
Point to the root ofthe source directory,
select`Import project from external model->Gradle`,
enable`Use auto-import`
ES Mian class
org.elasticsearch.bootstrap.Elasticsearch
/**
* Main entry point for starting elasticsearch
*/
public static void main(final String[] args) throws Exception {
// we want the JVM to think there is a security manager installed so that if internal policy decisions that would be based on the
// presence of a security manager or lack thereof act as if there is a security manager present (e.g., DNS cache policy)
System.setSecurityManager(new SecurityManager() {
@Override
public void checkPermission(Permission perm) {
// grant all permissions so that we can later set the security manager to the one that we want
}
});
LogConfigurator.registerErrorListener();
final Elasticsearch elasticsearch = new Elasticsearch();
int status = main(args, elasticsearch, Terminal.DEFAULT);
if (status != ExitCodes.OK) {
exit(status);
}
}
启动main
第一次报错
ERROR: the system property [es.path.conf] must be set
Exception in thread "main" java.lang.IllegalStateException: path.home is not configured
解决方法:
根目录新建home和home\config 目录
E:\workspace\idea\elasticsearch-6.2.4\home
E:\workspace\idea\elasticsearch-6.2.4\home\config
下载同版本号的 Elasticsearch6.2.4发行版可以运行的,将其中 config 目录和modules拷贝到 home 目录中,项目启动要用到,保证config里面的配置是对的配置jvm参数
-Didea.no.launcher=true
-Des.path.conf=E:\workspace\idea\demo\elasticsearch-6.2.4\home\config
-Des.path.home=E:\workspace\idea\demo\elasticsearch-6.2.4\home
继续运行报错
main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
解决方法
配置jvm参数
-Dlog4j2.disable.jmx=true
继续运行报错
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: plugins directory
[E:\workspace\idea\elasticsearch-6.2.4\home\plugins] not found
解决方法
新建目录:E:\workspace\idea\elasticsearch-6.2.4\home\plugins
继续运行报错
[2019-12-05T21:03:31,634][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [node-1] fatal error in thread [main], exiting
java.lang.NoClassDefFoundError: org/elasticsearch/plugins/ExtendedPluginsClassLoader
分析源码:
发现 ExtendedPluginsClassLoader 的模块是Provided,导致编译时找不到此类
解决方法
将 Provided 改成 Compile , 但更改会提示 distribution\bwc 有多个版本5.6 和 6.0
解决多版本冲突方法如下:
将多余版本忽略掉,只保留1个版本 6.x
运行继续报错
rg.elasticsearch.bootstrap.StartupException: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "createClassLoader")
解决方法
新建文件:java.policy
放到目录:E:\workspace\idea\demo\elasticsearch-6.2.4\home\config
内容:
grant {
permission java.lang.RuntimePermission "createClassLoader";
};
配置jvm参数
-Djava.security.policy==E:\workspace\idea\demo\elasticsearch-6.2.4\home\config\java.policy
最终目录
红色框子目录是新建的目录,其他目录源码自己动态生成的
最终JVM参数
-Didea.no.launcher=true
-Dlog4j2.disable.jmx=true
-Des.path.conf=E:\workspace\idea\demo\elasticsearch-6.2.4\home\config
-Des.path.home=E:\workspace\idea\demo\elasticsearch-6.2.4\home
-Djava.security.policy==E:\workspace\idea\demo\elasticsearch-6.2.4\home\config\java.policy
启动成功