Focus

159 阅读2分钟

一个 Gradle 插件,通过排除不必要的模块来帮助你加快构建速度。可生成特定于模块的 settings.gradle 文件,让你可以专注于特定功能或模块,而无需同步 monorepo 的其余部分。

Focus 插件评估你的项目设置并为你要关注的模块创建一个唯一的 settings.gradle 文件,该文件仅包含该模块所需的依赖项。然后它会创建一个引用当前焦点模块的 .focus 文件。

有了这些文件,当你同步项目时,Gradle 将只配置你需要的模块。删除 .focus 文件(可以使用 clearFocus 任务完成)将恢复为使用包含文件来配置整个项目。

设置

在你的 settings.gradle 文件中应用插件。

// settings.gradle(.kts)
pluginManagement {
  repositories {
    mavenCentral()
    gradlePluginPortal()
  }
}

plugins {
  id("com.dropbox.focus") version "0.4.0"
}

请注意,该插件当前已发布到 Maven Central,因此需要将其添加到 pluginsManagement 块中的存储库列表中。

将所有非必需的包含语句移动到 settings-all.gradle。始终包含的项目可以保留在你的主 settings.gradle 文件中。

// settings-all.gradle(.kts)
include ':sample:app2'
include ':sample:lib2c'
include ':sample:lib-shared'

include ':sample:moved'
project(':sample:moved').projectDir = new File("sample/lib-moved")

如果想使用与默认设置不同的设置文件,可以选择配置插件:

// settings.gradle(.kts)
focus {
  // The name of the settings file
  allSettingsFileName = "settings-all.gradle" // Default
  focusFileName = ".focus"  // Default
}

无论是否配置自定义focus文件,都应将其添加到 .gitignore 文件中,因为它适用于特定开发人员的工作流程。

用法

Focus 插件添加了一些任务供你在 Gradle 构建中与之交互。使用这些任务,可以创建模块特定的设置文件,Gradle 将自动使用这些设置文件仅配置所需的模块。

例如,假设当前正在处理应用程序模块 :sample:app2 并且只需要运行该模块及其依赖项。可以使用以下流程来减少加载并同步到 IDE 中的模块数量,从而加快开发速度。

# When you start work on the app2 module, bring it into focus
./gradlew :sample:app2:focus

# Click the Sync Elephant to have your IDE reload the gradle config, and you'll only have
# :sample:app2 and it's dependencies loaded by the IDE, allowing you to build and run the sample app
# and it's tests without having to sync the rest of the project.

# If you want to spend time in a specific dependency, you can bring that into focus and sync your
# IDE for even more fine grained development
./gradlew :sample:lib2b:focus

# When you want to clear focus and get back to the entire project, simply use the clearFocus task.
./gradlew clearFocus

任务

focus

将focus任务添加到所有子项目,并允许你只关注该模块。

createFocusSettings

为每个子项目创建一个 createFocusSettings 任务,负责查找模块的依赖项并创建模块特定的设置文件。这是focus任务的依赖项,可能不需要自己调用。

clearFocus

将 clearFocus 任务添加到根项目中,并允许你删除任何以前关注的模块。