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
啓動成功