gradle的buildSrc文件夹的用途是什么

gradle的buildSrc文件夹的用途是什么

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

问题描述

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



如果我的布局是

  src 
主要
Java

  buildSrc 
src
main
Java

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

  proj1 
src


proj2
src

然后只有一个顶级构建。 gradle(与 proj1 proj2 处于同一级别)定义了项目中的通用设置?

解决方案

buildSrc 是一个单独的 build ,其目的是进行构建任何打算在主构建的构建脚本中使用的任务,插件或其他类,但不必在各个构建之间共享。(*)这是不可能的构建此类作为主构建的一部分,因为它们必须在主构建的构建脚本甚至可以被编译/评估之前就存在,并且Gradle在完成任何工作(配置与执行阶段)之前就编译/评估所有构建脚本。 / p>

与将所有构建代码放入构建脚本相比, buildSrc 为您提供了y开发更类似于常规代码的构建代码,以及可以测试的类,将其导入到IDE等。这是一种使构建脚本保持简单和干燥的方法,即使对于更复杂的构建也是如此。


$在多项目构建中更经常看到b $ b

buildSrc ,仅仅是因为大型构建更可能实现自己的自定义任务和插件。



随着时间的流逝, buildSrc 将发展为更通用的功能,可以在单个Gradle中执行多个相关的 build



(*)可以跨构建共享类,但涉及更多。特别是,您需要将这些类发布到存储库,而消费版本必须从那里显式导入它们,就像在两个版本之间共享生产库时一样。


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)?

If I have a layout of

src
   main
      java

or

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

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 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).

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 is more often seen in multi-project builds simply because larger builds are more likely to implement their own custom tasks and plugins.

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文件夹的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:11