从源目录中应用gradle插件

时间:2017-06-26 16:08:46

标签: gradle groovy gradle-plugin

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

假设这(取自here)是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
            }
        }
    }
}

我需要在build.gradle中做什么才能使电话gradle -q hello在终端中发挥作用?互联网搜索给了我this,但它并没有帮助我解决问题。

3 个答案:

答案 0 :(得分:1)

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

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

如果您的构建不是构建该插件,而是构建其他内容并且您希望使用该插件来构建项目,那么您的源代码就在错误的位置。如果您想在多个版本中使用此插件,请将其设置为您自己构建和发布的项目,然后在项目中使用。如果它仅与此项目相关,则只需在构建脚本中定义插件,然后应用它,或将其填充到buildSrc项目中。

buildSrc项目位于名为buildSrc的子文件夹或您的根项目中,并且是一个自己的完整多项目Gradle构建,您可以在其中拥有所需的插件,任务等用于构建主项目。 buildSrc构建在主构建开始之前自动构建并添加到其类路径中。

答案 1 :(得分:0)

您可以将插件放入&#39; buildSrc&#39;顶级目录(您可能需要创建它)。该目录几乎与其他gradle项目一样。创建通常的&s; src / main / groovy&#39;或者&#39; src / main / java&#39;在里面,把你的插件源放到那里。 然后,您可以将其应用于任何项目:apply plugin: GreetingPlugin

答案 2 :(得分:0)

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

我的gradle插件是一个独立的git项目,旨在供我们的各种应用程序和库使用,以提供一致的构建环境。基本上,该插件包装了几个通用插件(java,groovy,jib,helm-plugin,npm插件,axion-release等),并提供了有关配置使用模型的通用约定。想要适合我们生态系统的应用程序或库只需应用此插件并定义非常少的信息(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首先应用了旧版本的插件,以便插件可以使用自身(尽管是以前的版本)来构建新版本。当然,当要添加新功能时,这样做有其缺点,因为我们必须先“预构建”一个虚拟版本(发布到本地〜/ .m2),然后再进行实际构建。

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

  1. dependencies块和gradlePlugin块拆分成一个单独的包含文件,称为common.gradle。
  2. 按照常规https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources创建buildSrc / build.gradle来预先构建插件,但将sourceSets指向plugin / src文件夹
  3. 更改根项目的build.gradle以仅通过id应用核心插件。

这是根项目build.gradle的净化版本:

./ build.gradle:

// Here's where we apply the plugin to itself
apply plugin: 'com.jerry.work.core'

这是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:

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

这是buildSrc / build.gradle:

// 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发布

(发布任务在插件代码中明确地通过构建任务“ finalizedBy”)

还要注意,我们正在使用axion-release-plugin,该插件通过git标签管理插件版本控制。

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

相关问题