【问题标题】:build.gradle - Could not find method copyDeps() for argumentsbuild.gradle - 找不到参数的方法 copyDeps()
【发布时间】:2020-03-19 03:29:59
【问题描述】:

下面是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 

问题代码sn-p:

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 命令使用以下代码 sn-p 语法,不带括号:

    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 有语法问题吗?使用括号...我们更喜欢使用括号

【问题讨论】:

  • 好的,所以如果我从上一个问题和这个问题中理解,你不想使用 Gradle 提供的任何 DSL 并使用 vanilla/scripted 语法正确吗?
  • @FranciscoMateo 我总是更喜欢使用 sripted 语法在 Jenkins 中编写 groovy 管道脚本。这是一个例子:edureka.co/community/54705/…
  • @FranciscoMateo 我想使用语法 id("com.github.johnrengelman.shadow").version("5.2.0") 而不是 id "com.github.johnrengelman.shadow" version "5.2.0" 因为以前的语法更具可读性,因为以前的语法清楚地告诉我这是链式表达式,因为使用了括号和点运算符。我回答你的问题了吗?
  • 哦,好吧,我想我现在明白你提到了 Jenkins。我从经验中知道,如果您不知道语法/步骤,Jenkinsfile 并不完全友好。对于您的问题 1 和 2,这是正确的,应该通过查看它来工作。一般来说,无论() 被省略,你都可以放回去。 . 被省略的地方你也可以放回去。

标签: java gradle groovy build.gradle dsl


【解决方案1】:

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)

【讨论】:

  • 与 testReport 和 wrapper 任务相同吗?你能完成答案吗?
猜你喜欢
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 2018-06-15
  • 2017-10-23
  • 2017-01-23
  • 2017-03-22
  • 2018-05-24
  • 2021-11-08
相关资源
最近更新 更多