玩转Solr源码之(一)Solr源码导入IDE

玩转Solr源码之—Solr源码导入IDE

源码下载

这个比较简单,直接到solr的官网上下载源码即可,这里以Solr 7.7.2为例子。

1
curl "https://www.apache.org/dyn/closer.lua/lucene/solr/7.7.2/solr-7.7.2-src.tgz" -o solr-7.7.2-src.tgz && tar -xvf solr-7.7.2-src.tgz

安装依赖

  • jdk 1.8
  • ant

笔者使用的是RedHat 系统,所以jdk的安装直接使用yum就行

1
yum -y install  java-1.8.0-openjdk-devel.x86_64

ant需要配置一下环境,也不算复杂

1
2
3
curl "http://mirrors.tuna.tsinghua.edu.cn/apache//ant/binaries/apache-ant-1.9.14-bin.tar.gz" -o apache-ant-1.9.14-bin.tar.gz
tar -xvf apache-ant-1.9.14-bin.tar.gz
rm -rf apache-ant-1.9.14-bin.tar.gz

配置环境变量(以RedHat为例),追加以下信息到~/.bash_profile (~/.bash_profile),这里我把ant安装到了/opt目录下

1
2
export ANT_HOME=/opt/apache-ant-1.9.14
export PATH=$PATH:$ANT_HOME/bin

简单验证下,看到以下结果就算ok了(source ~/.bash_profile 是为了使刚刚的配置生效)

1
2
3
$source ~/.bash_profile
$ant -version
Apache Ant(TM) version 1.9.14 compiled on March 12 2019

Build源码

Solr的源码是使用ivy来管理依赖的,所以需要安装ivy的lib包,但是Solr的项目中可以通过ant ivy-bootstrap直接下载ivy的jar包,这里看到”BUILD SUCCESSFUL”表示执行成功了。

1
2
3
4
5
6
7
8
9
10
11
12
$cd solr-7.7.2
$ant ivy-bootstrap
-ivy-bootstrap1:
[mkdir] Created dir: /home/fql/.ant/lib
[echo] installing ivy 2.4.0 to /home/fql/.ant/lib
[get] Getting: https://repo1.maven.org/maven2/org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar
[get] To: /home/fql/.ant/lib/ivy-2.4.0.jar
-ivy-bootstrap2:
-ivy-checksum:
-ivy-remove-old-versions:
ivy-bootstrap:
BUILD SUCCESSFUL

生成Intelij Idea/Eclipse项目

Intelij Idea 是笔者从2013年开始用的,之前用的都是Eclipse,这里不比较两个工具的好坏,能完成项目开发就行,这里以导入Intellij Idea 为例子 (如果是eclipse则为ant eclipse)

1
2
$cd solr-7.7.2
$ant idea

这个执行的过程比较长,是因为ivy会从maven的官方repo下载依赖,这里可以去喝杯茶等着程序自动执行完成就行.

如何解决下载不了org.restlet打头的包

可能你在执行ant test的时候会遇到类似以下的错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
resolve:
[ivy:retrieve]
[ivy:retrieve] :: problems summary ::
[ivy:retrieve] :::: WARNINGS
[ivy:retrieve] [FAILED ] org.restlet.jee#org.restlet;2.3.0!org.restlet.jar: (0ms)
[ivy:retrieve] ==== shared: tried
[ivy:retrieve] /home/fql/.ivy2/shared/org.restlet.jee/org.restlet/2.3.0/jars/org.restlet.jar
[ivy:retrieve] ==== public: tried
[ivy:retrieve] https://repo1.maven.org/maven2/org/restlet/jee/org.restlet/2.3.0/org.restlet-2.3.0.jar
[ivy:retrieve] [FAILED ] org.restlet.jee#org.restlet.ext.servlet;2.3.0!org.restlet.ext.servlet.jar: (0ms)
[ivy:retrieve] ==== shared: tried
[ivy:retrieve] /home/fql/.ivy2/shared/org.restlet.jee/org.restlet.ext.servlet/2.3.0/jars/org.restlet.ext.servlet.jar
[ivy:retrieve] ==== public: tried
[ivy:retrieve] https://repo1.maven.org/maven2/org/restlet/jee/org.restlet.ext.servlet/2.3.0/org.restlet.ext.servlet-2.3.0.jar
[ivy:retrieve] ::::::::::::::::::::::::::::::::::::::::::::::
[ivy:retrieve] :: FAILED DOWNLOADS ::
[ivy:retrieve] :: ^ see resolution messages for details ^ ::
[ivy:retrieve] ::::::::::::::::::::::::::::::::::::::::::::::
[ivy:retrieve] :: org.restlet.jee#org.restlet;2.3.0!org.restlet.jar
[ivy:retrieve] :: org.restlet.jee#org.restlet.ext.servlet;2.3.0!org.restlet.ext.servlet.jar
[ivy:retrieve] ::::::::::::::::::::::::::::::::::::::::::::::
[ivy:retrieve]
[ivy:retrieve] :: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS

BUILD FAILED
/home/fql/IdeaProjects/solr-7.7.2/build.xml:140: The following error occurred while executing this line:
/home/fql/IdeaProjects/solr-7.7.2/solr/build.xml:602: The following error occurred while executing this line:
/home/fql/IdeaProjects/solr-7.7.2/solr/core/build.xml:68: impossible to resolve dependencies:
resolve failed - see output for details

这是因为这个依赖的目录已经在~/.ivy2/cache下面的目录生成了,但是jar下载失败导致的,直接删除对应的目录重新执行ant test 就行了,以上面的错误为例,具体执行的命令为

1
2
rm -rf ~/.ivy2/cache/org.restlet*
ant test

导入Idea

执行完ant idea之后,如果看见以下类似输出表示build成功,我们可以开始将项目导入Intelij Idea

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-post-idea-instructions:
[echo]
[echo] To complete IntelliJ IDEA setup, you must manually configure
[echo] File | Project Structure | Project | Project SDK.
[echo]
[echo] You won't have to do this in the future if you define property
[echo] ${idea.jdk}, e.g. in ~/lucene.build.properties, ~/build.properties
[echo] or lucene/build.properties, with a value consisting of the
[echo] following two XML attributes/values (adjust values according to
[echo] JDKs you have defined locally - see
[echo] File | Project Structure | Platform Settings | SDKs):
[echo]
[echo] idea.jdk = project-jdk-name="1.8" project-jdk-type="JavaSDK"
[echo]

BUILD SUCCESSFUL

打开Intelij Idea , 选择文件->打开->选择源码的路径打开即可,Eclipse 直接选择从本地文件系统中打开项目即可。
项目导入
至此,我们已经完成将项目导入到Intelij Idea,下面我会分享如何在Intelij Idea中debug Solr 的源码。

参考的网址