build.gradle - 找不到参数的方法 copyDeps()

build.gradle - Could not find method copyDeps() for arguments

下面是 build.gradle 文件:

plugins({
  id('application')
  id 'java'
  id('com.github.johnrengelman.shadow').version('4.0.1')
})

allprojects( 
  {
    apply(plugin: 'application')
    apply(plugin: 'java')
    apply(plugin: 'com.github.johnrengelman.shadow')

    repositories({
      mavenCentral()
    })

    ext({
      vertxVersion = '3.7.0'
      commitTimestamp = {
        return "git log -1 --pretty=format:%cd --date=format:%Y%m%d%H%M%S".execute().text.trim()
      }
      commitId = {
        return "git rev-parse --short HEAD".execute().text.trim()
      }
      buildId = {
        if (System.getenv("BUILD_ID") != null) return ".${System.getenv("BUILD_ID")}"
        else return ""
      }
    })

    group = 'com.pluralsight.docker-production-aws'
    version = System.getenv("APP_VERSION") ?: "${commitTimestamp()}.${commitId()}${buildId()}"
    sourceCompatibility = '1.8'
    mainClassName = 'io.vertx.core.Launcher'

    dependencies({
      compile("io.vertx:vertx-core:$vertxVersion")
      compile("io.vertx:vertx-hazelcast:$vertxVersion")
      compile("io.vertx:vertx-service-discovery:$vertxVersion")
      compile("io.vertx:vertx-dropwizard-metrics:$vertxVersion")
      compile("com.typesafe:config:1.3.0")
      compile("com.hazelcast:hazelcast-cloud:3.6.5")
      testCompile("io.vertx:vertx-unit:$vertxVersion")
      testCompile("junit:junit:4.12")
      testCompile("org.assertj:assertj-core:3.5.2")
      testCompile("com.jayway.awaitility:awaitility:1.7.0")
    })

    task(copyDeps(type: Copy), {
        from (configurations.runtime + configurations.testRuntime).exclude('*')
        into('/tmp')
      }
    )

    test(
      {
        testLogging(
          {
          events("passed", "skipped", "failed")
          }
        )
        reports(
          {
            junitXml.enabled = true
            junitXml.destination = file("${rootProject.projectDir}/build/test-results/junit")
            html.enabled = false
          }
        )
      }
    )

  }
)

task(testReport(type: TestReport), {
    destinationDir = file("${rootProject.projectDir}/build/test-results/html")
    reportOn(subprojects*.test)
  }
)

test(
  {
    dependsOn(testReport)
  }
)

configure(
  (subprojects - project(':microtrader-common')), 
  {
    shadowJar(
      {
        destinationDir = file("${rootProject.projectDir}/build/jars")
        classifier = 'fat'
        mergeServiceFiles(
          {
            include('META-INF/services/io.vertx.core.spi.VerticleFactory')
          }
        )
      }
    )
  }
)

task(
  wrapper(type: Wrapper), 
  {
    gradleVersion = '4.10.2'
  }
)

/gradlew clean test shadowJar 上给出以下错误:

    > Could not find method copyDeps() for arguments 

问题代码段:

task(copyDeps(type: Copy), {
    from (configurations.runtime + configurations.testRuntime).exclude('*')
    into('/tmp')
  }

task(testReport(type: TestReport), {
    destinationDir = file("${rootProject.projectDir}/build/test-results/html")
    reportOn(subprojects*.test)
  }
)

task(
  wrapper(type: Wrapper), 
  {
    gradleVersion = '4.10.2'
  }
)

./gradlew 命令适用于以下不带括号的代码片段语法:

    task copyDeps(type: Copy) {
      from (configurations.runtime + configurations.testRuntime) exclude '*'
      into '/tmp'
    }


task testReport(type: TestReport) {
  destinationDir = file("${rootProject.projectDir}/build/test-results/html")
  reportOn subprojects*.test
}


task wrapper(type: Wrapper) {
  gradleVersion = '4.10.2'
}

build.gradle 语法有问题吗?使用括号...我们更喜欢使用括号

https://docs.gradle.org/current/userguide/more_about_tasks.html 显示了如何定义自定义任务的示例。

这里是您如何以详细的方式定义您的任务。

tasks.create('copyDeps', Copy, {
    from(file('srcDir'))
    into(buildDir)
})

任务是使用 TaskContainer 创建的,它为 create 方法提供了多个重载。这是一个子集:

  • create​(String name)
  • create​(String name, Closure configureClosure)
  • create​(String name, Class<T> type, Action<? super T> configuration) <-- 这是上面用的那个
  • create​(Map<String,​?> options, Closure configureClosure)