簡體   English   中英

將sourcesJar任務添加到自定義Gradle插件

[英]Add sourcesJar task to custom Gradle plugin

我的公司最近編寫了用於vanilla配置的gradle插件(存儲庫,跨項目的公共依賴項等)。 總的來說,這大大簡化了我們的構建過程,並發現了不同項目之間的一些不一致。 我們最近嘗試將一個sourcesJar任務添加到插件中,但它無法正常工作。

這是破碎的插件:

package com.mycompany.plugins

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.tasks.bundling.Jar

class OurJavaPlugin implements Plugin<Project> {

    void apply(Project project) {

        def date = com.mycompany.util.UtilityFunctions.getDate()

        project.configure(project) {
            println('Applying Java properties to: ' + project.name)

            apply plugin: 'java'
            apply plugin: 'maven'
            apply plugin: 'idea'
            apply plugin: 'eclipse'

            version = date

            // Use the local repos
            repositories {
                maven {
                    url "$externalDependenciesRepo"
                }
                maven {
                    url "$internalDependenciesRepo"
                }
            }

            uploadArchives {
                repositories {
                    mavenDeployer {
                        // Deploy to internal repo
                        repository(url: "$internalDependenciesRepo")
                    }
                }
            }

            // Common dependencies
            dependencies {
                compile group: 'log4j', name: 'log4j', version:'1.2.17'
                compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.6.6'
                compile group: 'org.slf4j', name: 'slf4j-api', version:'1.6.6'
                testCompile "junit:junit:$junit_version"
            }

            eclipse.project {
              natures 'org.springsource.ide.eclipse.gradle.core.nature'
            }

            task sourcesJar(type: Jar, dependsOn: classes) {
                classifier = 'sources'
                from sourceSets.main.allSource
            }

            artifacts {
                archives sourcesJar
            }
        }
    }
}

除了sourcesJar之外,這個插件工作得很好。 當我將它添加到混合中(並編譯/部署到我們的本地repo)時,當我嘗試構建使用該插件的項目時,我收到此錯誤:

$ gradle :myProject:clean -x Test
Applying Java properties to: myProject

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\me\Documents\code\root\myProject\build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':myProject'.
> Failed to apply plugin [id 'customjava']
   > Could not find method sourcesJar() for arguments [{type=class org.gradle.api.tasks.bundling.Jar, dependsOn=task ':analytics-isr:classes'}, com.mycompany.plugins.OurJavaPlugin $_apply_closure1$_closure6@4c1d59cd] on project ':myProject'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.352 secs

build.gradle腳本中定義任務時,會build.gradle一個( build.gradle 4個) task方法 - 您可以在此處看到第一個task 您還可以看到這些方法都不匹配DSL - 您可以在build.gradle定義任務。 為什么? 因為在執行之前,每個build.gradle被評估為 DSL 轉換為適當的方法調用。 有關詳細信息,請查看問題。

提到的機制在自定義插件中不起作用 - 這里代碼被解釋翻譯它。 正如您在此處所看到的, Project類上沒有定義sourcesJar方法。 要在插件中創建任務,您需要調用上述task方法,例如:

task('sourcesJar', type: Jar, dependsOn: classes) {
   classifier = 'sources'
   from sourceSets.main.allSource
}

它完全調用這個方法(我知道參數順序是不同的,但這是groovy的工作原理 - 相信我)。

你也不需要project.configure(project) {...}project.with {...}就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM