版权声明:本文为博主原创,转载请注明出处!
简介
本文主要解决以下问题:
1、从何处以及如何下载Elasticsearch 5.6.12的源代码?
2、如何将源代码导入到IntelliJ IDEA中进行编辑?
3、如何在IDEA中运行并调试Elasticsearch的源代码?
软件环境
本文写作时在Windows 7
系统上搭建了源代码调试环境,工作中使用相同的步骤在Fedora 24
上搭建了开发环境。本文使用的软件环境如下:
- JDK 1.8.0_144
- Gradle 4.8.1
- IntelliJ IDEA 2017.2.7
源码下载
从elastic官方的GitHub仓库中去下载Elasticsearch 的源代码。你可以使用git clone的方式下载,也可以GitHub的下载链接单独下载ZIP文件。本文采用第二种方式,如下图所示:
导入IDEA
Elasticsearch 5.X 使用Gradle包装器进行构建,在将源码导入IDEA前可以使用gradlew
执行源码中提供的脚本自动生成IDEA需要的配置文件。
1、构建源码
将下载完的源码文件解压,在源码文件的根目录执行如下命令:
gradlew idea
因为需要下载相关依赖,执行的时间可能会比较长。因为网络原因配置过程可能会执行失败,这样的情况多试几次即可。最终构建成功的界面如下图所示:
2、导入IDEA
在IDEA中选择File -> New -> Project From Existing Sources...
在弹出的对话框中选择Elasticsearch 5.6.12源码的根目录,点击OK后在之后出现的对话框中选择Import project from external model -> Gradle
,如下图所示:
点击下方的Next
按钮,在接下来的对话框中选中Use auto-import
并点击下方的Finish
按钮后,IDEA及开始导入Elasticsearch的源码。导入成功后,如下图所示:
运行源代码
在源代码的根目录下新建debug-home
目录,做调试源码时的配置及数据目录。将源码根目录下的distribution.src.main.resources.config
目录及里边的内容拷贝到debug-home
目录中,并在debug-home
目录中创建如下图所示的目录:
在debug-home.config
目录中增加debug-security.policy
文件,并在文件内添加如下内容:
grant {
// needed to generate runtime classes
permission java.lang.RuntimePermission "createClassLoader";
// needed by IndyInterface
permission java.lang.RuntimePermission "getClassLoader";
// needed by groovy engine
permission java.lang.RuntimePermission "accessDeclaredMembers";
permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
permission java.lang.RuntimePermission "accessClassInPackage.jdk.internal.reflect";
// Allow executing groovy scripts with codesource of /untrusted
permission groovy.security.GroovyCodeSourcePermission "/untrusted";
// Standard set of classes
permission org.elasticsearch.script.ClassPermission "<<STANDARD>>";
// groovy runtime (TODO: clean these up if possible)
permission org.elasticsearch.script.ClassPermission "groovy.grape.GrabAnnotationTransformation";
permission org.elasticsearch.script.ClassPermission "groovy.lang.Binding";
permission org.elasticsearch.script.ClassPermission "groovy.lang.GroovyObject";
permission org.elasticsearch.script.ClassPermission "groovy.lang.GString";
permission org.elasticsearch.script.ClassPermission "groovy.lang.Script";
permission org.elasticsearch.script.ClassPermission "groovy.util.GroovyCollections";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.ast.builder.AstBuilderTransformation";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.reflection.ClassInfo";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.GStringImpl";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.powerassert.ValueRecorder";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.powerassert.AssertionRenderer";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.ScriptBytecodeAdapter";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.vmplugin.v7.IndyInterface";
permission org.elasticsearch.script.ClassPermission "sun.reflect.ConstructorAccessorImpl";
permission org.elasticsearch.script.ClassPermission "sun.reflect.MethodAccessorImpl";
permission org.elasticsearch.script.ClassPermission "jdk.internal.reflect.ConstructorAccessorImpl";
permission org.elasticsearch.script.ClassPermission "jdk.internal.reflect.MethodAccessorImpl";
permission org.elasticsearch.script.ClassPermission "groovy.lang.Closure";
permission org.elasticsearch.script.ClassPermission "org.codehaus.groovy.runtime.GeneratedClosure";
permission org.elasticsearch.script.ClassPermission "groovy.lang.MetaClass";
permission org.elasticsearch.script.ClassPermission "groovy.lang.Range";
permission org.elasticsearch.script.ClassPermission "groovy.lang.Reference";
permission java.lang.RuntimePermission "createClassLoader";
permission java.io.FilePermission "/proc/sys/net/core/somaxconn", "read";
permission java.util.PropertyPermission "sun.nio.ch.bugLevel", "write";
// needed to generate runtime classes
permission java.lang.RuntimePermission "createClassLoader";
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
// expression runtime
permission org.elasticsearch.script.ClassPermission "java.lang.String";
permission org.elasticsearch.script.ClassPermission "org.apache.lucene.expressions.Expression";
permission org.elasticsearch.script.ClassPermission "org.apache.lucene.search.DoubleValues";
// available functions
permission org.elasticsearch.script.ClassPermission "java.lang.Math";
permission org.elasticsearch.script.ClassPermission "org.apache.lucene.util.MathUtil";
permission org.elasticsearch.script.ClassPermission "org.apache.lucene.util.SloppyMath";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
};
在debug-home.modules
目录中增加transport-netty4
目录,并在该目录中增加plugin-descriptor.properties
文件,文件内容如下:
# Elasticsearch plugin descriptor file
# This file must exist as 'plugin-descriptor.properties' in a folder named `elasticsearch`
# inside all plugins.
#
### example plugin for "foo"
#
# foo.zip <-- zip file for the plugin, with this structure:
#|____elasticsearch/
#| |____ <arbitrary name1>.jar <-- classes, resources, dependencies
#| |____ <arbitrary nameN>.jar <-- any number of jars
#| |____ plugin-descriptor.properties <-- example contents below:
#
# classname=foo.bar.BazPlugin
# description=My cool plugin
# version=2.0
# elasticsearch.version=2.0
# java.version=1.7
#
### mandatory elements for all plugins:
#
# 'description': simple summary of the plugin
description=Netty 4 based transport implementation
#
# 'version': plugin's version
version=5.6.12
#
# 'name': the plugin name
name=transport-netty4
#
# 'classname': the name of the class to load, fully-qualified.
classname=org.elasticsearch.transport.Netty4Plugin
#
# 'java.version': version of java the code is built against
# use the system property java.specification.version
# version string must be a sequence of nonnegative decimal integers
# separated by "."'s and may have leading zeros
java.version=1.8
#
# 'elasticsearch.version': version of elasticsearch compiled against
elasticsearch.version=5.6.12
### optional elements for plugins:
#
# 'has.native.controller': whether or not the plugin has a native controller
has.native.controller=false
在IDEA中选择Run -> Edit Configurations...
,在弹出的对话框中按如下图填写:
其中VM options
处填写的内容如下:
-Xms4096m
-Xmx4096m
-Dlog4j2.disable.jmx=true
-Des.path.home=E:\idea_workspace\elasticsearch-5.6.12\debug-home
-Des.path.conf=E:\idea_workspace\elasticsearch-5.6.12\debug-home\config
-Djava.security.policy=E:\idea_workspace\elasticsearch-5.6.12\debug-home\config\debug-security.policy
修改core
模块中的build.gradle
文件,在其中依赖配置的末尾增加如下图所示的配置:
至此,通过IDEA中的Run -> Debug 'Elasticsearch'
已经可以成功运行Elasticsearch的源码,但是该工程目前仅加载了transport-netty4
这一个非核心模块。如下图所示: