Why are we doing this

Source Parsing & Optimization for Elasticsearch (chapter 2

Prepare the compile and debug environment

But after trying, or failed to end, I believe that read this book friends must also have a lot of encountered such things. The inability to run debug source code basically declares the book over early.

Therefore, the search network information, coupled with their own hands-on try, finally successful, to share the experience to everyone.

The preparatory work

  • Java 1.8
  • Elasticsearch 6.1.2
  • win7
  • Intellij IDEA 2018.1.6 x64
  • Gradle 4.3

Import the project

1. Download the es source code

Click here to select Source code in ZIP format to download and unzip it.

2. Download gradle

What version should I play? Open the \ ElasticSearch-6.1.2 \gradle\wrapper\gradle-wrapper.properties file and you can see the following:

distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME ZipStorePath = wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-all.zip distributionSha256Sum=b3afcc2d5aaf4d23eeab2409d64c54046147322d05acc7fb5a63f84d8a2b8bd7Copy the code

Gradle 4.3 gradle4.3

Download gradle-4.3-all.zip to elasticSearch \gradle\wrapper directory. Make sure you are in the same directory as ElasticSearch \gradle\wrapper\gradle-wrapper. For elasticSearch \gradle\wrapper\gradle-wrapper.

DistributionUrl = gradle - 4.3 - all. ZipCopy the code

3. Change the Maven repository address

This step is not specifically for this article, but is mainly due to the default foreign Maven repository download speed.

So this step applies to any project that needs to download maven dependencies

Of course, if you’re out of the country, forget it

You need to modify the maven URL configuration for the following files:

  • elasticsearch\benchmarks\build.gradle
  • elasticsearch\client\benchmark\build.gradle

Orientation to the inside of the above two files repositories – maven – the value of the url is configured to ali cloud maven address: maven.aliyun.com/nexus/conte…

Create a new file init.gradle under $USER_HOME/.gradle/, enter the following content and save:

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        all {
            ArtifactRepository repo ->
    if (repo instanceof MavenArtifactRepository) {
                def url = repo.url.toString()
                if (url.startsWith('https://repo.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}
Copy the code

Gradle/USER_HOME/. Gradle/is your own gradle installation directory. Example value: C: Users\Administrator. Gradle. The above script replaced the url matched to the warehouse into ali cloud warehouse, if there is not matched to the compilation failure, you can add matching conditions in imitation.

Compile the source code with Gradle

For Windows, run the CMD command to enter the DOS command line, switch to the root directory of elasticSearch source code, and run the following command to compile elasticsearch as an idea project:

gradlew idea
Copy the code

Logs are generated after successful compilation:

BUILD SUCCESSFUL in 3m 03s
Copy the code

5. Import the project into idea

Select the root directory of Elasticsearch you downloaded From Existing Sources and click Open. Import project from external model -> Gradle, select Use auto-import. Then wait for Idea gradle to compile again.

Start the project

Es the boot method is elasticsearch/server/SRC/main/org/elasticsearch/bootstrap/elasticsearch. In Java main () method. But starting like this will report an error. We need to do some configuration.

1. The config directory

Go to www.elastic.co/cn/download… Download the compiled version of Elasticsearch, make sure it has the same version number, copy the config directory into the source directory’s home directory and open Edit Configurations. Add the following configuration to the VM options TAB

- Des. Path. The conf = D: \ elasticsearch - 6.3.2 \ home \ configCopy the code

2. Configuration path. Home

Still VM Options, new

- Des. Path. Home = D: \ elasticsearch - 6.3.2 \ homeCopy the code

And copy the modules folder from the ES6.3.2 distribution to the home directory

3. Enable log4j JMX

-Dlog4j2.disable.jmx=true
Copy the code

4. Maven dependency scope

Open IDEA Edit Configurations and tick Include Dependencies with Provided Scope

5. Configure security policies

Create a java.policy file in the home/config directory

grant {
    permission java.lang.RuntimePermission "createClassLoader";
};
Copy the code

Then add the java.security.Policy setting in VM Options to point to this file

- Djava. Security. The policy = D: \ elasticsearch - 6.3.2 \ home \ config \ Java. The policyCopy the code

At this point, the parameters of VM Options of the final version are as follows: