table of Contents
Introduction to OpenGrok
Install OpenGrok
2.1 Install JAVA runtime environment
2.2 Install Web Server-Tomcat
2.3 Install OpenGrok
2.4 Configure OpenGrok
2.5 Install universal-ctags
2.6 Build Source Index
2.6 Update source index
1. Introduction to 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
Requirements:
- Latest Java 1.8
- A servlet container like GlassFish or Tomcat (8.x or later) also running with Java at least 1.8
- Universal ctags (Exuberant ctags work too)
OpenGrok is a fast and easy-to-use source search engine and comparison engine.It can help us quickly search, locate and compare code trees.
system requirement:
- Java 1.8
- Web server GlassFish or Tomcat (8.x or later, here Tomcat8)
- Universal ctags (Exuberant ctags also work, there are pits here, in fact Exuberant ctags do not work, the latest version of OpenGrok will detect whether there are Universal ctags when indexing, and no direct error is reported)
In short, OpenGrok is a millisecond-level code search tool, and its biggest advantage is that the search speed is fast.
2. Install OpenGrok
2.1 Install JAVA Runtime Environment
OpenGrok and Tomcat both rely on JAVA, so we first need a JDK to support its operation
1 | sudo apt-get update |
For specific installation, please refer to the detailed installation of Ubuntu JDK
2.2 Install Web Server-Tomcat
OpenGrok is web-based access and requires a web system to support it, so a web server must be installed.
The Tomcat8 package is already provided in the source of Ubuntu18.04, and Tomcat8 is installed directly from the source.
1 | sudo apt-get install tomcat8 |
http://localhost: 8080
Tomcat8 common commands
1 | sudo service tomcat8 start // Start the Tomcat service |
For more detailed installation, please refer to Ubuntu installation and configuration tomcat8 service
2.3 Install OpenGrok
After installing Tomcat, the next step is to configure OpenGrok.
OpenGrok Download Address:
http://opengrok.github.io/OpenGrok
https://github.com/oracle/opengrok/releases
You can download the compiled file of OpenGrok, or you can download the source file at this URL. Here we directly download the compiled file (the students who are interested in OpenGrok can download the source code to see what it is). After downloading, decompress it with the following command:
tar xvzf opengrok-1.1-rc41.tar.gz -C/opt
2.4 Configure OpenGrok
If you want OpenGrok to run properly, you need a lot of environment variables. If we install jdk, Tomcat and OpenGrok according to the requirements of OpenGrok, and establish a directory structure, these environment variables will be set correctly when running the OpenGrok script, but if we If you want more flexible configuration, you still have to customize the directory structure, and manually configure these environment variables.
The environment variables are configured as follows: opengrokSetEnv.sh
1 | # ------------------------------------------------- ------------------------------ |
Once completed, you can use the opengrokSetEnv.sh script to set environment variables every time before running OpenGrok
1 | source opengrokSetEnv.sh |
For information on OpenGrok scripts, see https://github.com/OpenGrok/OpenGrok/blob/master/OpenGrok
If the error occurs, please check the source code to set the environment variable information.
After decompression, go to the decompression directory, and copy the source.war package in the/opt/opengrok/lib directory to the apache-tomcat/webapps directory
Enter in browser
If you see the OpenGrok page, it proves that OpenGrok is running successfully.
Next you need to change the web.xml configuration file in the apache-tomcat/webapps/source/WEB-INF directory
1 | <context-param> |
Configure the path of configuration.xml to the/opt/opengrok/etc/configuration directory.This configuration.xm will be automatically generated in the following source code indexing steps.
2.5 Install universal-ctags
1 | sudo apt install autoconf |
2.6 Build Source Index
Next, we need to configure the index for our source code. OpenGrok generates the index information of the source code. It looks like it is to establish a relevant database to achieve the purpose of fast search.
Set the following environment variables
Environment variable Description Default value
SRC_ROOT The source code path of the index to be generated $ {OPENGROK_INSTANCE_BASE}/src
DATA_ROOT Path to the generated index $ {OPENGROK_INSTANCE_BASE}/data
Then we can directly create the src and data directories in the opengrok installation directory/opt/opengrok
Then we put the source code that needs to be indexed in the src directory. Of course, we don’t really need to put the source code here, we just need to create a link for it.
1 | cd /opt/opengrok/src |
Generate index
1 | indexer.py -J = -Djava.util.logging.config.file =/opt/opengrok/logging.properties \ |
Then we open
You can see our source code
2.6 Update source index
When the code changes, the source code index needs to be updated to reflect the changes to Opengrok
1 | touch/opt/opengrok/src/* |