如何在包装器任务

时间:2017-08-13 22:21:29

标签: gradle

这看起来应该很简单,但我无法让它发挥作用。我正在尝试设置包装器任务的distributionType属性,但它不起作用:

task wrapper(type: Wrapper) {
    gradleVersion = '4.1'
    distributionType = DistributionType.ALL
}

当我尝试运行包装器任务时,出现以下错误:

$ gradle wrapper

FAILURE: Build failed with an exception.

* Where:
Build file '.../build.gradle' line: 6

* What went wrong:
A problem occurred evaluating root project 'project'.
> Could not get unknown property 'DistributionType' for task ':wrapper' of type org.gradle.api.tasks.wrapper.Wrapper.

* 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: 0.613 secs

请注意我已下载最新的Gradle版本(4.1)并将其添加到我的bash路径中:

$ gradle -version

------------------------------------------------------------
Gradle 4.1
------------------------------------------------------------

Build time:   2017-08-07 14:38:48 UTC
Revision:     941559e020f6c357ebb08d5c67acdb858a3defc2

Groovy:       2.4.11
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_144 (Oracle Corporation 25.144-b01)
OS:           Mac OS X 10.12.6 x86_64

我试图设置它的方式似乎是合理的,因为它与我设置gradleVersion的方式相同,并且考虑到Wrapper.java的实现:

public class Wrapper extends DefaultTask {

    public enum DistributionType {
        /**
         * binary-only Gradle distribution without sources and documentation
         */
        BIN,
        /**
         * complete Gradle distribution with binaries, sources and documentation
         */
        ALL
    }

    //...

    public void setGradleVersion(String gradleVersion) {
        this.gradleVersion = GradleVersion.version(gradleVersion);
    }

    // ...

    public void setDistributionType(DistributionType distributionType) {
        this.distributionType = distributionType;
    }

    // ...

}

我已经看到了这种解决方法(如this StackOverflow回答中所述),这有效,但感觉有点太hacky ......

task wrapper(type: Wrapper) {
    gradleVersion = '2.13'
    distributionUrl = distributionUrl.replace("bin", "all")
}

我做错了什么?

我还与--stacktrace --debug重新合作。我将发布看似最相关的部分,而不是发布整个输出(非常大):

> Could not get unknown property 'DistributionType' for task ':wrapper' of type org.gradle.api.tasks.wrapper.Wrapper.

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'slackscheduler'.
  at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:92)
  at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$2.run(DefaultScriptPluginFactory.java:176)
  at org.gradle.configuration.ProjectScriptTarget.addConfiguration(ProjectScriptTarget.java:77)
  // many, many lines omitted
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'DistributionType' for task ':wrapper' of type org.gradle.api.tasks.wrapper.Wrapper.
  at org.gradle.internal.metaobject.AbstractDynamicObject.getMissingProperty(AbstractDynamicObject.java:85)
  at org.gradle.internal.metaobject.ConfigureDelegate.getProperty(ConfigureDelegate.java:134)
  at build_90tinl1ipqqsdzvv3o3xs2adt$_run_closure1.doCall(/Users/justinrogers/Development/slack-scheduler/build.gradle:6)
  at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:70)

其中,在一天结束时,表明我们失败here

1 个答案:

答案 0 :(得分:8)

我的猜测是:

ConfigureUtil帮助程序集将策略解析为Closure.OWNER_ONLY。在build.gradle中,您无权访问DistributionType枚举。如果您在任务之前的某处导入此枚举:

import org.gradle.api.tasks.wrapper.Wrapper.DistributionType

没关系。另一个解决方案是分配

distributionType = Wrapper.DistributionType.ALL