从源目录应用gradle插件

从源目录应用gradle插件,gradle,groovy,gradle-plugin,Gradle,Groovy,Gradle Plugin,应用放在src/main/groovy中的gradle插件的正确方法是什么 假设这是src/main/groovy/GreetingPlugin.groovy class GreetingPlugin implements Plugin<Project> { void apply(Project project) { // Add the 'greeting' extension object project.extensions.create

应用放在
src/main/groovy
中的gradle插件的正确方法是什么

假设这是
src/main/groovy/GreetingPlugin.groovy

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        // Add the 'greeting' extension object
        project.extensions.create("greeting", GreetingPluginExtension)
        // Add a task that uses the configuration
        project.task('hello') {
            doLast {
                println project.greeting.message
            }
        }
    }
}
class GreetingPlugin实现插件{
无效申请(项目){
//添加“问候语”扩展对象
project.extensions.create(“问候语”,问候语)
//添加使用配置的任务
project.task('hello'){
多拉斯特{
println project.greeting.message
}
}
}
}

我需要在我的
build.gradle
中做什么才能使呼叫
gradle-q hello
在终端中工作?互联网搜索给了我答案,但没有帮我解决问题。

你无法解决母鸡和鸡蛋的问题。这意味着您不能在构建脚本的生产源中添加定义的插件,因为您需要构建脚本来构建插件,但构建脚本需要插件。明白我的意思吗

如果此项目是关于构建该插件的,并且您还希望在自己的构建中使用该插件,那么您始终只能使用以前发布/构建的版本来构建下一个版本

如果你的构建不是关于构建那个插件,而是关于构建其他东西,并且你想使用那个插件来构建项目,那么你的源代码放错了地方。如果您想在多个版本中使用此插件,请将其作为自己的项目进行构建和发布,然后在项目中使用。如果它仅与此项目相关,则只需在构建脚本中定义插件,然后应用它,或者将其填充到
buildSrc
项目中


buildSrc
项目位于名为
buildSrc
的子文件夹或您的根项目中,是一个完整的多项目渐变构建,您可以在其中拥有插件、任务等,以用于构建主项目。
buildSrc
build是在主构建启动并添加到其类路径之前自动生成的。

您可以将插件放在顶层的“buildSrc”目录中(您可能需要创建它)。该目录与其他gradle项目几乎相同。在其中创建通常的“src/main/groovy”或“src/main/java”,并将插件源代码放入其中。
然后,您可以在任何项目中应用它:
apply plugin:GreetingPlugin

我已经能够为我的项目解决鸡和蛋的问题。我在这里为OP和任何其他正在寻找解决方案的人发布解决方案

My gradle插件是一个独立的git项目,旨在供我们的各种应用程序和库使用,以提供一致的构建环境。基本上,该插件封装了几个常见插件(java、groovy、jib、helm插件、npm插件、axion发行版等),并提供了配置使用模型的通用约定。想要融入我们生态系统的应用程序或库只需应用此插件并定义非常少的信息(docker映像名称、组/模块名称、工件凭证等),插件就可以做到 以前在应用程序或库自己的build.gradle中显式声明的所有工作。该插件实际上是6个独立的插件,可以独立使用,只要应用程序/库根项目首先应用“核心”插件

以下是此插件的原始目录布局:

.
├── README.md
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── plugin
│   ├── build.gradle
│   └── src
│       ├── main
│       │   └── groovy
│       │       └── com
│       │           └── jerry
│       │               └── work
│       │                   └── gradle
│       │                       └── plugin
│       │                           ├── core
│       │                           ├── docker
│       │                           ├── helm
│       │                           ├── java
│       │                           ├── nodejs
│       │                           └── release
│       └── test
│           └── groovy
│               └── com
│                   └── jerry
│                       └── work
│                           └── gradle
│                               └── plugin
├── settings.gradle
该插件运行良好,但为了保持有序,根项目的build.gradle首先应用了该插件的旧版本,以便该插件使用自身(尽管是以前的版本)来构建新版本。当然,当想要添加新特性时,这也有其缺点,因为我们必须“预构建”一个虚拟版本(发布到local~/.m2),然后进行真正的构建

要解决此问题并允许插件使用自身构建自身,而不需要两次构建,请执行以下步骤:

  • 依赖项
    块和
    gradlePlugin
    块拆分为一个单独的可包含文件,名为common.gradle
  • 按照常规创建buildSrc/build.gradle以预构建插件,但将
    sourceset
    指向plugin/src文件夹
  • 将根项目的build.gradle更改为仅按id应用核心插件
  • 下面是根项目build.gradle的净化版本:

    // Here's where we apply the plugin to itself
    apply plugin: 'com.jerry.work.core'
    
    apply plugin: 'com.jerry.work.gradlejar'
    
    my_custom_dsl {
        jar {
            groupId = 'com.jerry.work'
            name = 'jerry-gradle-plugin'
        }
    }
    
    apply from: '../common.gradle'
    
    // Add a source set for the functional test suite
    sourceSets {
        functionalTest {
        }
    }
    
    gradlePlugin.testSourceSets(sourceSets.functionalTest)
    configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)
    
    // Add a task to run the functional tests
    task functionalTest(type: Test) {
        testClassesDirs = sourceSets.functionalTest.output.classesDirs
        classpath = sourceSets.functionalTest.runtimeClasspath
    }
    
    check {
        // Run the functional tests as part of `check`
        dependsOn(tasks.functionalTest)
    }
    
    // Standard artifactory repo resolution, to download whatever dependencies we need at build time.
    repositories {
        mavenLocal()
        maven {
            url "https://${System.env.ARTIFACTORY_DNS ?: artifactory_dns}"
            credentials {
                username = System.env.ARTIFACTORY_USER ?: artifactory_user
                password = System.env.ARTIFACTORY_API_KEY ?: artifactory_api_key
            }
        }
    }
    
    // We need groovy and gradle plugin support
    apply plugin: 'groovy'
    apply plugin: 'java-gradle-plugin'
    
    dependencies {
        compile gradleApi()
        compile localGroovy()
    }
    
    // load root project's gradle.properties, since gradle won't do it automatically.
    Properties properties = new Properties()
    FileInputStream input = new FileInputStream(project.file('../gradle.properties'))
    properties.load(input)
    input.close()
    
    for (String key : properties.stringPropertyNames()) {
        this.project.ext.set(key, properties.getProperty(key))
    }
    
    // Let groovy compiler know where to find the source files.
    sourceSets {
        main {
            groovy {
                srcDirs = ['../plugin/src/main/groovy']
            }
        }
    }
    
    apply from: '../common.gradle'
    
    ./build.gradle:

    // Here's where we apply the plugin to itself
    apply plugin: 'com.jerry.work.core'
    
    apply plugin: 'com.jerry.work.gradlejar'
    
    my_custom_dsl {
        jar {
            groupId = 'com.jerry.work'
            name = 'jerry-gradle-plugin'
        }
    }
    
    apply from: '../common.gradle'
    
    // Add a source set for the functional test suite
    sourceSets {
        functionalTest {
        }
    }
    
    gradlePlugin.testSourceSets(sourceSets.functionalTest)
    configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)
    
    // Add a task to run the functional tests
    task functionalTest(type: Test) {
        testClassesDirs = sourceSets.functionalTest.output.classesDirs
        classpath = sourceSets.functionalTest.runtimeClasspath
    }
    
    check {
        // Run the functional tests as part of `check`
        dependsOn(tasks.functionalTest)
    }
    
    // Standard artifactory repo resolution, to download whatever dependencies we need at build time.
    repositories {
        mavenLocal()
        maven {
            url "https://${System.env.ARTIFACTORY_DNS ?: artifactory_dns}"
            credentials {
                username = System.env.ARTIFACTORY_USER ?: artifactory_user
                password = System.env.ARTIFACTORY_API_KEY ?: artifactory_api_key
            }
        }
    }
    
    // We need groovy and gradle plugin support
    apply plugin: 'groovy'
    apply plugin: 'java-gradle-plugin'
    
    dependencies {
        compile gradleApi()
        compile localGroovy()
    }
    
    // load root project's gradle.properties, since gradle won't do it automatically.
    Properties properties = new Properties()
    FileInputStream input = new FileInputStream(project.file('../gradle.properties'))
    properties.load(input)
    input.close()
    
    for (String key : properties.stringPropertyNames()) {
        this.project.ext.set(key, properties.getProperty(key))
    }
    
    // Let groovy compiler know where to find the source files.
    sourceSets {
        main {
            groovy {
                srcDirs = ['../plugin/src/main/groovy']
            }
        }
    }
    
    apply from: '../common.gradle'
    
    以下是common.gradle的净化版本:

    // This gradle file is used by both plugins/build.gradle and buildSrc/build.gradle, so there is one place
    // where we define dependencies and plugin definitions.
    
    dependencies {
        implementation (group: 'io.spring.gradle', name: 'dependency-management-plugin', version: dependency_management_plugin_version)
        implementation (group: 'org.sonarsource.scanner.gradle', name: 'sonarqube-gradle-plugin', version: sonar_qube_plugin_version)
        implementation (group: 'org.unbroken-dome.gradle-plugins.helm', name: 'gradle-helm-plugin', version: helm_plugin_version)
        implementation (group: 'pl.allegro.tech.build', name: 'axion-release-plugin', version: axion_release_plugin_version)
        implementation (group: 'com.github.jk1', name: 'gradle-license-report', version: gradle_license_plugin_version)
        implementation (group: 'org.ajoberstar.grgit', name: 'grgit-core', version: grgit_plugin_version) {
            exclude group: 'org.codehaus.groovy', module: 'groovy'
        }
        implementation (group: 'org.ajoberstar.grgit', name: 'grgit-gradle', version: grgit_plugin_version) {
            exclude group: 'org.codehaus.groovy', module: 'groovy'
        }
        implementation (group: 'gradle.plugin.com.google.cloud.tools', name: 'jib-gradle-plugin', version: jib_plugin_version)
        implementation (group: 'com.github.node-gradle', name: 'gradle-node-plugin', version: node_plugin_version)
        implementation (group: 'org.jacoco', name: 'org.jacoco.agent', version: jacoco_version)
        implementation (group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: kotlin_stdlib_version)
        implementation (group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: jfrog_version)
        testImplementation (group: 'org.jacoco', name: 'org.jacoco.agent', version: jacoco_version)
        testImplementation (group: 'org.ajoberstar', name: 'grgit', version: grgit_lib_version) {
            exclude group: 'org.eclipse.jgit', module: 'org.eclipse.jgit.ui'
            exclude group: 'org.eclipse.jgit', module: 'org.eclipse.jgit'
        }
        // Use the awesome Spock testing and specification framework
        testImplementation ('org.spockframework:spock-core:1.3-groovy-2.5')
    }
    
    gradlePlugin {
        // Define the plugin
        plugins {
            core {
                id = 'com.jerry.work.core'
                implementationClass = 'com.jerry.work.gradle.plugin.core.GradleCorePlugin'
            }
            release {
                id = 'com.jerry.work.release'
                implementationClass = 'com.jerry.work.gradle.plugin.release.GradleReleasePlugin'
            }
            helm {
                id = 'com.jerry.work.helm'
                implementationClass = 'com.jerry.work.gradle.plugin.helm.GradleHelmPlugin'
            }
            javaapp {
                id = 'com.jerry.work.javaapp'
                implementationClass = 'com.jerry.work.gradle.plugin.java.GradleJavaAppPlugin'
            }
            javajar {
                id = 'com.jerry.work.javajar'
                implementationClass = 'com.jerry.work.gradle.plugin.java.GradleJavaLibPlugin'
            }
            gradlejar {
                id = 'com.jerry.work.gradlejar'
                implementationClass = 'com.jerry.work.gradle.plugin.java.GradleGradlePlugin'
            }
            nodejs {
                id = 'com.jerry.work.nodejs'
                implementationClass = 'com.jerry.work.gradle.plugin.nodejs.GradleNodejsPlugin'
            }
        }
    }
    
    if (project.hasProperty('validatePlugins_version) {
        // NpmInstallTask has warnings that we can't get rid of, so disable failing the build during validation phase.
        validatePlugins {
            failOnWarning = false
        }
    }
    
    以下是plugin/build.gradle:

    // Here's where we apply the plugin to itself
    apply plugin: 'com.jerry.work.core'
    
    apply plugin: 'com.jerry.work.gradlejar'
    
    my_custom_dsl {
        jar {
            groupId = 'com.jerry.work'
            name = 'jerry-gradle-plugin'
        }
    }
    
    apply from: '../common.gradle'
    
    // Add a source set for the functional test suite
    sourceSets {
        functionalTest {
        }
    }
    
    gradlePlugin.testSourceSets(sourceSets.functionalTest)
    configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)
    
    // Add a task to run the functional tests
    task functionalTest(type: Test) {
        testClassesDirs = sourceSets.functionalTest.output.classesDirs
        classpath = sourceSets.functionalTest.runtimeClasspath
    }
    
    check {
        // Run the functional tests as part of `check`
        dependsOn(tasks.functionalTest)
    }
    
    // Standard artifactory repo resolution, to download whatever dependencies we need at build time.
    repositories {
        mavenLocal()
        maven {
            url "https://${System.env.ARTIFACTORY_DNS ?: artifactory_dns}"
            credentials {
                username = System.env.ARTIFACTORY_USER ?: artifactory_user
                password = System.env.ARTIFACTORY_API_KEY ?: artifactory_api_key
            }
        }
    }
    
    // We need groovy and gradle plugin support
    apply plugin: 'groovy'
    apply plugin: 'java-gradle-plugin'
    
    dependencies {
        compile gradleApi()
        compile localGroovy()
    }
    
    // load root project's gradle.properties, since gradle won't do it automatically.
    Properties properties = new Properties()
    FileInputStream input = new FileInputStream(project.file('../gradle.properties'))
    properties.load(input)
    input.close()
    
    for (String key : properties.stringPropertyNames()) {
        this.project.ext.set(key, properties.getProperty(key))
    }
    
    // Let groovy compiler know where to find the source files.
    sourceSets {
        main {
            groovy {
                srcDirs = ['../plugin/src/main/groovy']
            }
        }
    }
    
    apply from: '../common.gradle'
    
    以下是buildSrc/build.gradle:

    // Here's where we apply the plugin to itself
    apply plugin: 'com.jerry.work.core'
    
    apply plugin: 'com.jerry.work.gradlejar'
    
    my_custom_dsl {
        jar {
            groupId = 'com.jerry.work'
            name = 'jerry-gradle-plugin'
        }
    }
    
    apply from: '../common.gradle'
    
    // Add a source set for the functional test suite
    sourceSets {
        functionalTest {
        }
    }
    
    gradlePlugin.testSourceSets(sourceSets.functionalTest)
    configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)
    
    // Add a task to run the functional tests
    task functionalTest(type: Test) {
        testClassesDirs = sourceSets.functionalTest.output.classesDirs
        classpath = sourceSets.functionalTest.runtimeClasspath
    }
    
    check {
        // Run the functional tests as part of `check`
        dependsOn(tasks.functionalTest)
    }
    
    // Standard artifactory repo resolution, to download whatever dependencies we need at build time.
    repositories {
        mavenLocal()
        maven {
            url "https://${System.env.ARTIFACTORY_DNS ?: artifactory_dns}"
            credentials {
                username = System.env.ARTIFACTORY_USER ?: artifactory_user
                password = System.env.ARTIFACTORY_API_KEY ?: artifactory_api_key
            }
        }
    }
    
    // We need groovy and gradle plugin support
    apply plugin: 'groovy'
    apply plugin: 'java-gradle-plugin'
    
    dependencies {
        compile gradleApi()
        compile localGroovy()
    }
    
    // load root project's gradle.properties, since gradle won't do it automatically.
    Properties properties = new Properties()
    FileInputStream input = new FileInputStream(project.file('../gradle.properties'))
    properties.load(input)
    input.close()
    
    for (String key : properties.stringPropertyNames()) {
        this.project.ext.set(key, properties.getProperty(key))
    }
    
    // Let groovy compiler know where to find the source files.
    sourceSets {
        main {
            groovy {
                srcDirs = ['../plugin/src/main/groovy']
            }
        }
    }
    
    apply from: '../common.gradle'
    
    这是gradle.properties:

    release.disableRemoteCheck=true
    dependency_management_plugin_version=1.0.8.RELEASE
    sonar_qube_plugin_version=2.8
    helm_plugin_version=0.4.4
    axion_release_plugin_version=1.10.2
    grgit_plugin_version=4.0.0
    grgit_lib_version=1.7.2
    jib_plugin_version=1.8.0
    jacoco_version=0.8.2
    gradle_license_plugin_version=1.12
    node_plugin_version=2.1.1
    kotlin_stdlib_version=1.3.11
    jfrog_version=4.11.0
    
    最后是settings.gradle:

    rootProject.name = 'jerry-gradle-plugin'
    include ':plugin'
    
    现在,要构建和发布gradle插件,只需使用

    ./gradlew出版社

    (发布任务由插件代码中的构建任务显式“finalized”)

    还要注意,我们使用的是axion发布插件,它通过git标记管理插件版本控制


    我还没有透露该插件自动应用的所有标准插件和第三方插件,但是读者可以通过查看上面列出的common.gradle文件中的依赖项声明来辨别我们使用了什么。

    这似乎与“您可以将插件的源代码放在rootProjectDir/buildSrc/src/main/groovy目录中“,从“否”开始,它并不矛盾,但它确切地告诉你这一点。你什么也没说
    buildSrc
    。如果您的插件实际位于
    buildSrc
    中,请修复您的问题,我将更新我的答案。你说它在
    src/main/groovy
    中是主要项目,而不是
    buildSrc/src/main/groovy
    中是我在最后一段中提到的
    buildSrc
    项目。我会自己解决的。我认为
    buildSrc
    src/main/groovy
    。我不知道