Introduction to OpenGrok – OpenGrok 简介

最后修改: 2024年 2月 9日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

OpenGrok is an open-source and powerful source code search and cross-reference engine. It allows us to explore, search, and navigate through the source code of various projects efficiently.

OpenGrok 是一个开源且功能强大的源代码搜索和交叉引用引擎。通过它,我们可以高效地探索、搜索和浏览各种项目的源代码。

In this article, we’ll explore the features and benefits of OpenGrok and see how to leverage its capabilities for effective code browsing.

在本文中,我们将探讨 OpenGrok 的功能和优势,并了解如何利用其功能进行有效的代码浏览。

2. What Is OpenGrok?

2.什么是 OpenGrok?

OpenGrok is a fast and scalable source code search and cross-reference engine that provides a user-friendly web interface for exploring code repositories. It is developed in Java and supports various programming languages, making it versatile and suitable for diverse projects.

OpenGrok是一个快速、可扩展的源代码搜索和交叉引用引擎,为探索代码库提供了一个用户友好的网络界面。它采用 Java 开发,支持多种编程语言,因此用途广泛,适用于各种项目。

Here are some standout features of OpenGrok:

以下是 OpenGrok 的一些突出功能:

  • Cross-Referencing: It excels in creating cross-references for code elements and linking related functions, classes, and variables. It facilitates seamless code navigation, allowing us to gain insights into the relationship between different parts of the codebase.
  • Searching Capabilities: We can search for specific terms, methods, or symbols across the codebase. The results are displayed within context, providing a comprehensive overview of the instances where the term is used.
  • Syntax highlighting: OpenGrok enhances code readability by highlighting syntax for various programming languages. It aids in quickly identifying different elements in the code, making the reviewing process more efficient.

3. Installation

3.安装

Setting up OpenGrok involves several steps, including installing the software, configuring it, indexing the source code, and deploying the web interface. OpenGrok requires Java 11 or later, a servlet container, and Universal ctags.

设置 OpenGrok 需要几个步骤,包括安装软件、配置、索引源代码和部署 Web 界面。OpenGrok 需要 Java 11 或更高版本、servlet 容器和 Universal ctags

Depending on our application requirements, there are multiple ways to install OpenGrok.

根据我们的应用要求,有多种安装 OpenGrok 的方法。

3.1. Using Docker Image

3.1.使用 Docker 映像

We can use a Docker image for running the OpenGrok instance by pulling it locally:

我们可以使用 Docker 镜像来运行 OpenGrok 实例,方法是将其拉到本地:

$ docker pull opengrok/docker

3.2. Using Distribution Tar

3.2.使用分配焦油

We can download the latest version of OpenGrok suitable for our operating system from their repository. Let’s prepare the directories:

我们可以从资源库下载适合我们操作系统的最新版 OpenGrok。让我们准备好目录:

$ mkdir opengrok/{src,data,dist,etc,log}

After downloading the tar file, we can extract the archive to our preferred installation directory:

下载 tar 文件后,我们可以将压缩包解压到首选的安装目录:

$ tar -C opengrok/dist --strip-components=1 -xzf opengrok-1.13.2.tar.gz

Copying the logging configuration:

复制日志记录配置:

$ cp opengrok/dist/doc/logging.properties opengrok/etc

We need to make sure to place the source.war file in a location where the application container can identify and deploy the web application. The release archive includes the WAR file, located within the lib directory:

我们需要确保将 source.war 文件放置在应用容器可以识别和部署网络应用的位置。发布归档包括位于 lib 目录中的 WAR 文件:

$ cp opengrok/dist/lib/source.war /opt/homebrew/Cellar/tomcat/10.1.18/libexec/webapps

In the case of Tomcat 8, the path could be /opt/homebrew/Cellar/tomcat/10.1.18/libexec/webapps, though it may vary depending on the operating system.

Tomcat 8 的情况下,路径可能是 /opt/homebrew/Cellar/tomcat/10.1.18/libexec/webapps,但可能因操作系统而异。

4. Running OpenGrok

4. 运行 OpenGrok

4.1. Using Docker Container

4.1.使用 Docker 容器

Running OpenGrok using a docker is a straightforward process. We can fire up an OpenGrok instance:

使用 docker 运行 OpenGrok 是一个简单明了的过程。我们可以启动一个 OpenGrok 实例:

$ docker run -d -v <path/to/repository>:/opengrok/src -p 8080:8080 opengrok/docker:latest

The container makes OpenGrok available at http://localhost:8080/. The directory linked to /opengrok/src should have the projects we want to search (in subdirectories).

容器会在 http://localhost:8080/ 中提供 OpenGrok。链接到 /opengrok/src 的目录应该包含我们要搜索的项目(在子目录中)。

However, this image is a simple wrapper around the OpenGrok environment. We can consider it as a compact utility. The indexer and the web container are not set up to handle a lot of work at once.

不过,该图像是 OpenGrok 环境的一个简单封装。我们可以将其视为一个紧凑的实用程序。索引器和网络容器的设置并不适合同时处理大量工作。

4.2. Using Distribution Tar

4.2.使用分发 Tar

To proceed with running OpenGrok in a standalone environment, the repository to be scanned must be present locally at location opengrok/src. Now, let’s execute the below command:

要在单机环境中运行 OpenGrok,需要扫描的软件源必须位于本地 opengrok/src 位置。现在,让我们执行下面的命令:

$ java \
    -Djava.util.logging.config.file=opengrok/etc/logging.properties \
    -jar opengrok/dist/lib/opengrok.jar \
    -c usr/local/bin/ctags \
    -s opengrok/src -d /opengrok/data -H -P -S -G \
    -W opengrok/etc/configuration.xml -U http://localhost:8080/source

The command mentioned above initiates the creation of the index, allows the index to generate the configuration file, and informs the web application that a new index is now accessible.

上述命令启动了索引的创建,允许索引生成配置文件,并通知网络应用程序现在可以访问新索引。

We can access the application at http://address:port/source, where the address and port depend on the application server configuration, and the source represents the WAR file name:

我们可以通过 http://address:port/source 访问应用程序,其中地址端口取决于应用程序服务器配置,代表 WAR 文件名:

OpenGrok UI

We can see two repositories being available for code search, offering various options to provide flexibility in searching. Let’s now try to search keyword url in XML files only:

我们可以看到有两个资源库可用于代码搜索,它们提供了各种选项,为搜索提供了灵活性。现在,让我们尝试仅在 XML 文件中搜索关键字 url

OpenGrok Search

The output shows a search for the url keyword in all XML files in both OpenGrok and hellogitworld repositories.

输出结果显示在 OpenGrokhellogitworld 资源库中的所有 XML 文件中搜索 url 关键字。

5. Conclusion

5.结论

In this article, we explored the capabilities of OpenGrok—a potent source code search engine.

在本文中,我们探讨了 OpenGrok 的功能–一个强大的源代码搜索引擎。

With efficient code search, cross-referencing, and support for multiple version control systems, OpenGrok emerges as an indispensable tool, fostering streamlined development and in-depth code exploration.

OpenGrok 具有高效的代码搜索、交叉引用功能,并支持多个版本控制系统,是一款不可或缺的工具,有助于简化开发流程和深入探索代码。