gradle任务构建已存在问题

Dea*_*ler 17 gradle

我收到了错误

Cannot add task ':webserver:build' as a task with that name already exists.
Run Code Online (Sandbox Code Playgroud)

奇怪的是我的hello任务很好,但我的build任务不是和是,我试图覆盖Java插件的构建任务.

build.gradle文件:

allprojects {
   apply plugin: 'java'
   apply plugin: 'eclipse'

   task hello << { task -> println "I'm $task.project.name" }
   task build << { task -> println "I'm building now" }
}

subprojects {

    hello << {println "- I depend on stserver"}

    build << { println "source sets=$sourceSets.main.java.srcDirs" }
}
Run Code Online (Sandbox Code Playgroud)

我的孩子网络服务器build.gradle文件:

sourceSets.main{
  java.srcDirs = ['app']
}

build << { println "source sets=$sourceSets.main.java.srcDirs" }

hello << {println "- Do something specific xxxx"}
Run Code Online (Sandbox Code Playgroud)

这里的交易是什么,是压倒build特殊还是什么?覆盖我自己的hello任务工作得很好,我认为压倒一切都很build简单?

Pet*_*ser 10

您没有覆盖hello任务,只是添加了更多任务操作.您可以使用覆盖任务task foo(overwrite: true).我没有理由超越build任务; 可能有更好的方法来实现你想要的.


rod*_*ion 9

什么是交易,覆盖构建是特殊的或什么的.覆盖我自己的hello任务工作得很好,我认为覆盖构建会一样简单吗?

行为看起来不同的原因是因为build任务已经存在hello而不存在(而不是因为build特殊).

在gradle你不能这样做:

task hello << { print "hello" }
task hello << { print "hello again" }
Run Code Online (Sandbox Code Playgroud)

这将因熟悉的错误而失败:"Cannot add task ':hello' as a task with that name already exists.".

由于build任务已经存在,因此有一秒钟是违法的task build << { ... }.但是,它适用于hello任务,因为它不存在,因此task hello << { ... }是合法的,因为它是第一个hello任务声明.

如果你替换你task build << { ... }build << { ... },只是为现有任务增加了更多的行为,它将"编译"正常.