gradle 的 buildSrc 文件夹的目的是什么? [英] What is the purpose of gradle's buildSrc folder?

查看:56
本文介绍了gradle 的 buildSrc 文件夹的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 gradle 中,使用 buildSrc 文件作为顶层的目的是什么,而不仅仅是典型的 java 项目布局(src/main/java)?

In gradle, what is the purpose of using a buildSrc file as a top level, as opposed to just a typical java project layout (src/main/java)?

如果我有一个布局

src
   main
      java

buildSrc
    src
       main
          java

有什么区别(或这样做的原因)?它在多模块项目中更有用吗?即使对于多模块项目,我也不能做类似的事情

What would be the difference (or the reason for doing this)? Is it more useful in multi module projects? Even for a multi module project, couldn't I do something like

proj1
  src


proj2
  src

然后只有一个顶级 build.gradle(与 proj1proj2 处于同一级别)来定义跨项目的通用设置?

And then just have a top level build.gradle (at the same level as proj1 and proj2) that defines common settings across the projects?

推荐答案

buildSrc 是一个单独的 build,其目的是构建任何任务、插件或其他类旨在用于主构建的构建脚本,但不必跨构建共享.(*) 将此类类构建为主构建的一部分是不可能的,因为它们必须在主构建的构建脚本甚至可以被编译/评估之前存在,并且 Gradle 在它执行任何工作之前编译/评估所有构建脚本(配置与执行阶段).

buildSrc is a separate build whose purpose is to build any tasks, plugins, or other classes which are intended to be used in build scripts of the main build, but don't have to be shared across builds.(*) It wouldn't be possible to build such classes as part of the main build, because they have to exist before the main build's build scripts can even be compiled/evaluated, and Gradle compiles/evaluates all build scripts before it does any work (configuration vs. execution phase).

与将所有构建代码放入构建脚本相比,buildSrc 为您提供了一种开发构建代码的方法,更像常规代码,作为您可以测试的类,导入到您的 IDE 等.它是即使对于更复杂的构建,也可以保持构建脚本简单和 DRY 的一种方法.

Compared to putting all build code into build scripts, buildSrc gives you a way to develop build code more like regular code, as classes that you can test, import into your IDE, etc. It is one way to keep build scripts simple and DRY even for more complicated builds.

buildSrc 在多项目构建中更常见,因为更大的构建更有可能实现自己的自定义任务和插件.

buildSrc is more often seen in multi-project builds simply because larger builds are more likely to implement their own custom tasks and plugins.

随着时间的推移,buildSrc 将发展成为在单个 Gradle 调用中执行多个依赖构建的更通用的能力.

Over time, buildSrc will grow into a more general capability of executing multiple dependent builds in a single Gradle invocation.

(*) 跨构建共享类是可能的,但涉及更多.特别是,您需要将类发布到存储库,并且使用构建必须从那里显式导入它们,类似于在构建之间共享生产库时.

(*) Sharing classes across builds is possible but more involved. In particular, you'll need to publish the classes to a repository, and consuming builds have to explicitly import them from there, similar to when sharing production libraries between builds.

这篇关于gradle 的 buildSrc 文件夹的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆