首页 > 解决方案 > Gradle 单项目插件管理块不起作用(Kotlin DSL)

问题描述

我需要将多项目构建更改为单项目构建,因为在这个 repo 中只有一个项目。目前,在 中settings.gradle,我有一个自定义插件 repo,它当前使用一个pluginManagementresolutionStrategy和我的 repo 列表:

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == 'com.meanwhileinhell.plugin') {
                useModule('com.meanwhileinhell:gradle-plugin:1.0.0-SNAPSHOT')
            }
        }
    }

    repositories {
        mavenLocal()
        maven { url "https://repo.spring.io/milestone" }
        maven { url "https://plugins.gradle.org/m2/" }

        // Meanwhileinhell repo
        maven {
            url "s3://mvn.meanwhileinhell.com/releases"
            credentials(AwsCredentials) {
                accessKey s3_access_key
                secretKey s3_access_secret
            }
        }
    }

    plugins {
        ...
        ...
    }
}

但是,删除settings.gradle这个块并将其移动到我的build.gradle.kts(Kotlin DSL)中似乎什么也没做。我试过用一个

configurations {
    all {
        resolutionStrategy {
            eachPlugin {
                ...
            }
        }
    }
}

并且

settings {
    pluginManagement {
        resolutionStrategy {
            eachPlugin {
                ...
            }
        }
    }
}

我找到了一个用于settingsEvaluated获取settings对象的 SO 答案,但这又是不行的。

目前我build.gradle.kts看起来像这样,没有从我的仓库中提取任何插件:

val springBootVersion: String by project

group = "com.meanwhileinhell.myapp"
version = "$version"

repositories {
    mavenCentral()
    mavenLocal()
    maven ("https://repo.spring.io/snapshot")
    maven ("https://repo.spring.io/milestone")
    maven ("https://plugins.gradle.org/m2/")

    maven {
        url = uri("s3://mvn.meanwhileinhell.com/releases")
        credentials(AwsCredentials::class) {
            accessKey = (project.property("s3_access_key") as String)
            secretKey = (project.property("s3_access_secret") as String)
        }
    }
}

plugins {
    base
    eclipse
    idea
    java
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
    // Load but don't apply to root project
    id("org.springframework.boot") version "1.5.14.RELEASE" apply false
}

dependencies {
    ...
}

每当我尝试添加插件 id 时,id("com.meanwhileinhell.plugin.hell2java") version "1.0.0-SNAPSHOT"我都会收到一个错误,看起来它甚至没有查看我的 S3 位置:

* What went wrong:
Plugin [id: 'com.meanwhileinhell.plugin.hell2java', version: '1.0.0-SNAPSHOT'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.meanwhileinhell.plugin.hell2java:com.meanwhileinhell.plugin.hell2java.gradle.plugin:1.0.0-SNAPSHOT')
  Searched in the following repositories:
    Gradle Central Plugin Repository

对此的任何帮助将不胜感激!

编辑 !!!!----------------------

我刚刚在 Gradle 文档中找到了这个: https ://docs.gradle.org/current/userguide/plugins.html#sec:plugin_management

pluginManagement {} 块只能出现在 settings.gradle 文件中...。

看起来我完全走错了路,所以将研究初始化脚本路线。

标签: gradlegradle-plugingradle-kotlin-dsl

解决方案


我想你可能错过了一些关于文件结构的东西。

在 Groovy DSL 中,您有以下文件:

  • 构建.gradle
  • 设置.gradle
  • 初始化.gradle

在 Kotlin DSL 中,您拥有相同的文件,但扩展名为 .kts:

  • 构建.gradle.kts
  • settings.gradle.kts
  • 初始化.gradle.kts

Kotlin DSL 与 Groovy DSL 的放置位置没有什么不同。pluginManagement需要进入设置文件,所以对于 Kotlin 来说就是settings.gradle.kts. 如果您有疑问,请查看文档。对于几乎所有的代码示例,您都可以在 Groovy 和 Kotlin DSL 之间切换以查看如何操作(以及它们应该进入哪些文件)。


推荐阅读