Monday, May 23, 2022

Part 1: Compiling elasticsearch from source

I recently came across some complication where I need to change some of the codes in the elasticsearch and compile them into the binary tar.gz, build a docker image and consume it. It is rarely out there in term of documentation, so I decide to share my findings here.


1. Getting the source codes from elasticsearch. I believe this should be easy, and no explanation needed here.

https://github.com/elastic/elasticsearch

For example, I am looking for the latest 8.2 release, so I will checkout the 8.2 branch from now.

2. Install the right Java version. Here, from the builddoc, it says JAVA17 is needed. So, get ready your gear with Java 17, and set a JAVA_HOME pointing to the right version.

❯ update-alternatives --config java 

There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 

* 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 auto mode 

1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode 

2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode

manual mode Press to keep the current choice[*], or type selection number: ^C

3. Fun begins now, from the source codes itself, you can see a gradlew build tool is provided. Digging more onto it, you will discover to build a linux version of binary, you need to run a command like this.

./gradlew :distribution:archives:linux-tar:assemble 

Then end result will be like this,

❯ ./gradlew :distribution:archives:linux-tar:assemble 

Starting a Gradle Daemon (subsequent builds will be faster) ======================================= 

Elasticsearch Build Hamster says Hello! 

Gradle Version : 7.4.2 

OS Info : Linux 5.13.0-41-generic (amd64) 

JDK Version : 17.0.3 (Private Build) 

JAVA_HOME : /usr/lib/jvm/java-17-openjdk-amd64 

Random Testing Seed : D75C9C369C262E41 In FIPS 140 mode : false ======================================= 

> Task :server:compileJava 

Note: Some input files use or override a deprecated API. 

Note: Recompile with -Xlint:deprecation for details. 

Note: Some input files use or override a deprecated API that is marked for removal. 

Note: Recompile with -Xlint:removal for details. 

 BUILD SUCCESSFUL in 2m 18s 499 actionable tasks: 6 executed, 493 up-to-date 


4. Horray! Now you have your build success. You can get the binary tar.gz at here.

❯ ls -al distribution/archives/linux-tar/build/distributions/elasticsearch-8.2.1-SNAPSHOT-linux-x86_64.tar.gz


No comments: