如何在 settings.gradle 文件中声明构建脚本、依赖项和插件?

How do I declare a buildscript, dependencies and plugins in the settings.gradle file?

TD;LR: 我希望能够通过应用一些必需的依赖项和插件来构建项目,以便能够做一种轻量级的 CI/CD 在 GitLab 中构建。

详细解释: Liferay 为您提供了所谓的工作区,它通过在 settings.gradle 文件中声明一个 workspace 插件来添加某些依赖项。然而,为了创建这样的工作区,需要下载一个 400Mb 的 SDK。我可以在预装了 Blade CLI 的 Docker 集线器中创建一个映像——这根本不是一个坏主意——但我想知道是否可以只使用 Gradle .

  1. 我不能包含或触摸项目的 build.gradle 文件,在添加一些 include 的意义上,因为这会破坏 IDE 的编译。

创建新项目时,项目的build.gradle文件包含以下内容:

dependencies {
        compileOnly group: "com.liferay", name: "com.liferay.asset.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.comment.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.dynamic.section"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.form.navigator"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.util"
        compileOnly group: "com.liferay", name: "com.liferay.journal.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.layout.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.site.taglib"
        compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
        compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib"
        compileOnly group: "javax.portlet", name: "portlet-api"
        compileOnly group: "javax.servlet", name: "javax.servlet-api"
        compileOnly group: "jstl", name: "jstl"
        compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations"

        cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
}

为了能够在 Liferay 的工作区之外编译它,我需​​要添加以下内容:

buildscript {
        dependencies {
                classpath group: 'com.liferay', name: 'com.liferay.gradle.plugins', version: '11.0.74'
                classpath group: "com.liferay", name: "com.liferay.gradle.plugins.target.platform", version: "2.1.9"
        }

        repositories {
                maven {
                        url "https://repository-cdn.liferay.com/nexus/content/groups/public"
                }
        }
}

repositories {
    mavenCentral()
}

apply plugin: "com.liferay.plugin"
apply plugin: "com.liferay.target.platform"

dependencies {
        targetPlatformBoms group: "com.liferay.portal", name: "release.portal.bom", version: "7.2.1"
        targetPlatformBoms group: "com.liferay.portal", name: "release.portal.bom.compile.only", version: "7.2.1"
        targetPlatformBoms group: "com.liferay.portal", name: "release.portal.bom.third.party", version: "7.2.1"
        compileOnly group: "com.liferay", name: "com.liferay.asset.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.comment.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.dynamic.section"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.form.navigator"
        compileOnly group: "com.liferay", name: "com.liferay.frontend.taglib.util"
        compileOnly group: "com.liferay", name: "com.liferay.journal.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.layout.taglib"
        compileOnly group: "com.liferay", name: "com.liferay.site.taglib"
        compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
        compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib"
        compileOnly group: "javax.portlet", name: "portlet-api"
        compileOnly group: "javax.servlet", name: "javax.servlet-api"
        compileOnly group: "jstl", name: "jstl"
        compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations"

        cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
}

我的想法是拥有一个具有以下结构的“父项目”,将其克隆到 CI/CD 管道中,并将要编译的项目移动到“模块”目录中:

.
├── modules
│   └── my-project-portlet
│       ├── bnd.bnd
│       ├── build
│       ├── build.gradle
│       └── src
└── settings.gradle

问题是:如何在 settings.gradle 文件中包含 buildscriptapply plugin 和四个 targetPlatformBoms

我读到也许有一种方法可以使用 buildSrc 来完成它,但似乎不鼓励 [1][2][3] 支持复合构建。所以我不知道从这里去哪里...

提前致谢

[1]: https://proandroiddev.com/stop-using-gradle-buildsrc-use-composite-builds-instead-3c38ac7a2ab3

[2]: https://medium.com/@wzieba/an-argument-against-buildsrc-defined-dependencies-or-how-to-seamlessly-keep-your-gradle-8e0bfd7ee8e3

[3]: https://androidsweets.ongoodbits.com/2020/05/29/stop-using-gradle-buildsrc

如果我没看错,你不能修改你的 build.gradle,但你可以修改你的 settings.gradle 或者你想添加一个 settings.gradle 文件到一个项目现在没有 settings.gradle 文件。在这种情况下,您可以使用用于评估 settings.gradle 文件的 Gradle instance provided by the Settings 对象简单地配置您的项目:

gradle.rootProject {
    // modify your root project
}

gradle.allprojects {
    // modify all projects in your build
}

但是,对于您的特定用例,我建议改用 initialization script。初始化脚本只有在它们被放置在本地用户目录的 .gradle 文件夹中时才会自动执行,因此您可以将它们存储在其他地方(甚至直接与您的项目一起)并使用 [=21= 将它们添加到您的构建中] 仅在需要时使用命令行选项(例如,当 CI 服务器上的 运行 时)。

您可以对初始化脚本使用与上述示例相同的代码,但是您可以省略语句中的 gradle. 部分,因为初始化脚本是直接针对 Gradle 实例求值的。

settings.gradle 文件或初始化脚本中添加存储库或依赖项应该没有问题,因此您可以像这样启动初始化脚本:

allprojects {
    repositories {
        mavenCentral()
    }
    dependencies {
         targetPlatformBoms group: "com.liferay.portal", name: "release.portal.bom", version: "7.2.1"
         targetPlatformBoms group: "com.liferay.portal", name: "release.portal.bom.compile.only", version: "7.2.1"
         targetPlatformBoms group: "com.liferay.portal", name: "release.portal.bom.third.party", version: "7.2.1"
         // ...
    }
}

我不确定是否可以从 settings.gradle 文件或初始化脚本调用 buildscript,因为这个块在它被评估的方式上有点特殊Gradle。还有一个问题是您无法使用插件标识符从 build.gradle 外部应用插件。您必须使用插件类型(实现插件的 Java class)代替:

apply plugin: "com.liferay.plugin"
// ->
apply plugin: package.path.to.LiferayPlugin

多亏了 Lukas 的回答,我终于能够让它工作了。正如他指出的那样,我无法使用 buildscript 代码,因为在尝试应用插件时 Gradle 抱怨找不到它们。所以,我使用了 initscript 而不是 another answer in the Gradle user forums.

所指出的

我通过查看以下目录的 JAR 找到了组 + class 名称:

  • ~/.gradle/caches/modules-2/files-2.1/com.liferay/com.liferay.gradle.plugins.target.platform/2.1.9/
  • ~/.gradle/caches/modules-2/files-2.1/com.liferay/com.liferay.gradle.plugins/11.0.74
initscript {
    repositories {
        maven {
            url 'https://repository-cdn.liferay.com/nexus/content/groups/public'
        }
    }

    dependencies {
        classpath group: 'com.liferay', name: 'com.liferay.gradle.plugins', version: '11.0.74'
        classpath group: 'com.liferay', name: 'com.liferay.gradle.plugins.target.platform', version: '2.1.9'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }

    apply plugin: com.liferay.gradle.plugins.LiferayPlugin
    apply plugin: com.liferay.gradle.plugins.target.platform.TargetPlatformPlugin

    dependencies {
        targetPlatformBoms group: 'com.liferay.portal', name: 'release.portal.bom', version: '7.2.1'
        targetPlatformBoms group: 'com.liferay.portal', name: 'release.portal.bom.compile.only', version: '7.2.1'
        targetPlatformBoms group: 'com.liferay.portal', name: 'release.portal.bom.third.party', version: '7.2.1'
    }
}