博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
工欲善其事,必先利其器之—使用OpenGrok阅读源码
阅读量:4030 次
发布时间:2019-05-24

本文共 4866 字,大约阅读时间需要 16 分钟。

OpenGrok的介绍

OpenGrok is a fast and usable source code search and cross reference engine. It helps you search, cross-reference and navigate your source tree. It understands various program file formats and history from many Source Code Management systems. In other words it lets you grok (profoundly understand) source code and is developed in the open, hence the name OpenGrok. It is written in Java.

OpenGrok的安装

前置环境

  • Java运行环境至少是1.8以上
  • 安装Java Web容器(classfish或tomcat8.x以上版本)
  • 安装Universal ctags

Tomcat的安装

把下载的压缩包解压,并放到你喜欢的某个本地目录,如解压后的目录为

/Users/luogw/dev_tool/apache-tomcat-8.5.31

注:解压的文件默认没有执行权限的,需要对该目录的所有文件加上可执行权限

drwxr-xr-x@ 25 luogw  staff     850  4 27 21:25 ./drwxr-xr-x@ 13 luogw  staff     442  4 27 21:26 ../-rw-r--r--@  1 luogw  staff   34985  4 27 21:24 bootstrap.jar-rw-r--r--@  1 luogw  staff    1703  4 27 21:24 catalina-tasks.xml-rw-r--r--@  1 luogw  staff   15900  4 27 21:24 catalina.bat-rw-r--r--@  1 luogw  staff   23463  4 27 21:25 catalina.sh-rw-r--r--@  1 luogw  staff  207125  4 27 21:25 commons-daemon-native.tar.gz-rw-r--r--@  1 luogw  staff   25145  4 27 21:24 commons-daemon.jar-rw-r--r--@  1 luogw  staff    2040  4 27 21:24 configtest.bat-rw-r--r--@  1 luogw  staff    1922  4 27 21:25 configtest.sh-rw-r--r--@  1 luogw  staff    8509  4 27 21:25 daemon.sh-rw-r--r--@  1 luogw  staff    2091  4 27 21:24 digest.bat-rw-r--r--@  1 luogw  staff    1965  4 27 21:25 digest.sh-rw-r--r--@  1 luogw  staff    3574  4 27 21:24 setclasspath.bat-rw-r--r--@  1 luogw  staff    3680  4 27 21:25 setclasspath.sh-rw-r--r--@  1 luogw  staff    2020  4 27 21:24 shutdown.bat-rw-r--r--@  1 luogw  staff    1902  4 27 21:25 shutdown.sh-rw-r--r--@  1 luogw  staff    2022  4 27 21:24 startup.bat-rw-r--r--@  1 luogw  staff    1904  4 27 21:25 startup.sh-rw-r--r--@  1 luogw  staff   49336  4 27 21:24 tomcat-juli.jar-rw-r--r--@  1 luogw  staff  405109  4 27 21:25 tomcat-native.tar.gz-rw-r--r--@  1 luogw  staff    4574  4 27 21:24 tool-wrapper.bat-rw-r--r--@  1 luogw  staff    5483  4 27 21:25 tool-wrapper.sh-rw-r--r--@  1 luogw  staff    2026  4 27 21:24 version.bat-rw-r--r--@  1 luogw  staff    1908  4 27 21:25 version.sh

添加可执行权限

luogw@luogw-MacBook-Pro dev_tool$ chmod -R +x apache-tomcat-8.5.31# 启动Tomcatluogw@luogw-MacBook-Pro dev_tool$ ./apache-tomcat-8.5.31/bin/startup.shUsing CATALINA_BASE:   /Users/luogw/dev_tool/apache-tomcat-8.5.31Using CATALINA_HOME:   /Users/luogw/dev_tool/apache-tomcat-8.5.31Using CATALINA_TMPDIR: /Users/luogw/dev_tool/apache-tomcat-8.5.31/tempUsing JRE_HOME:        /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/HomeUsing CLASSPATH:       /Users/luogw/dev_tool/apache-tomcat-8.5.31/bin/bootstrap.jar:/Users/luogw/dev_tool/apache-tomcat-8.5.31/bin/tomcat-juli.jarTomcat started.

用浏览器打开 ,正常情况下看到所示页面,表明安装成功

这里写图片描述
在.bash_profile中添加如下启动与并闭的Tomcat的alias

# for tomcatalias start_tomcat='${HOME}/dev_tool/apache-tomcat-8.5.31/bin/startup.sh'alias close_tomcat='${HOME}/dev_tool/apache-tomcat-8.5.31/bin/shutdown.sh

Universal Ctags的安装

使用brew安装Ctags

brew install --HEAD universal-ctags/universal-ctags/universal-ctags

OpenGrok的安装

下载对应的压缩包并解压,解压出来的目录放在你喜欢的某个目标即可,如我的解压目录位置是 /Users/luogw/dev_tool/opengrok-1.1-rc29

OpenGrok的使用实践

示例:搭建JDK源码的阅读环境

环境配置

在.bash_profile文件中添加如下环境变量的配置

# for tomcatalias start_tomcat='${HOME}/dev_tool/apache-tomcat-8.5.31/bin/startup.sh'alias close_tomcat='${HOME}/dev_tool/apache-tomcat-8.5.31/bin/shutdown.sh'# for opengrok,实际目录位置视具体的情况更改OPENGROK_INSTANCE_BASE=${HOME}/dev_tool/opengrok-1.1-rc29export OPENGROK_INSTANCE_BASEOPENGROK_TOMCAT_BASE=${HOME}/dev_tool/apache-tomcat-8.5.31export OPENGROK_TOMCAT_BASEOPENGROK_CTAGS=/usr/local/bin/ctagsexport OPENGROK_CTAGSOPENGROK_SRC_ROOT=${HOME}/personal/codeexport OPENGROK_SRC_ROOTOPENGROK_DATA_ROOT=${HOME}/personal/opengrok_indexexport OPENGROK_DATA_ROOTOPENGROK_VERBOSE=trueexport OPENGROK_VERBOSEalias opengrok='${HOME}/dev_tool/opengrok-1.1-rc29/bin/OpenGrok'

发布Web应用

luogw@luogw-MacBook-Pro opengrok_index$ opengrok deployLoading the default instance configuration ...Installing /Users/luogw/dev_tool/opengrok-1.1-rc29/bin/../lib/source.war to /Users/luogw/dev_tool/apache-tomcat-8.5.31/webapps ...Start your application server (Tomcat),  if it is not alreadyrunning, or wait until it loads the just installed web  application.OpenGrok should be available on 
:
/source where HOST and PORT are configured in Tomcat.

生成索引

luogw@luogw-MacBook-Pro notes$ source ~/.bash_profileluogw@luogw-MacBook-Pro notes$ opengrok indexLoading the default instance configuration ...WARNING: OpenGrok generated etc path /Users/luogw/dev_tool/opengrok-1.1-rc29/etc  doesn't exist  Attempting to create generated etc directory ...  Creating default /Users/luogw/dev_tool/opengrok-1.1-rc29/logging.properties ...

生成索引需要些时间…..

搜索代码

# 启动Tomcatstart_tomcat# 浏览器打开链接 [http://localhost:8080/source/](http://localhost:8080/source/)

效果截图如下

这里写图片描述

这里写图片描述

参考资料

你可能感兴趣的文章
IP校验和详解
查看>>
C++中使用Mongo执行count和distinct运算
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
C++获取文件大小常用技巧分享
查看>>
未来5年大机遇:做贩卖多巴胺的超级玩家
查看>>
关于AIS编码解码的两个小问题
查看>>
GitHub 万星推荐:黑客成长技术清单
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
昨夜今晨最大八卦终于坐实——人类首次直接探测到了引力波
查看>>
如何优雅、机智地和新公司谈薪水?
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何确保自己的Mac数据安全呢?这里有四个“小秘诀”
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
第一性原理:戳中问题本质的人是怎么思考的?
查看>>
No.147 - LeetCode1108
查看>>
No.148 - LeetCode771
查看>>