无法将任务“X”添加为具有该名称的任务已存在

Posted

技术标签:

【中文标题】无法将任务“X”添加为具有该名称的任务已存在【英文标题】:Cannot add task 'X' as a task with that name already exists 【发布时间】:2016-07-10 15:10:31 【问题描述】:

我有这个gradle.build

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'maven-publish'
apply from: ext.gradleDir + '/common.gradle'
apply from: ext.gradleDir +'/base.gradle'
apply from: ext.gradleDir + '/integration_test.gradle'

sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'

project.archivesBaseName = 'client-services-lib'
uploadArchives.repositories.mavenDeployer.pom.groupId="com.waze.automation"


dependencies 
    compile('com.waze.automation:common:1.0.58')

    compile('com.sun.jersey:jersey-bundle:1.19')
    compile('com.sun.jersey:jersey-json:1.19')
    compile group:  'org.codehaus.jackson', name:'jackson-jaxrs', version: '1.1.1'

    compile('com.google.guava:guava:18.0')

    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile('org.hamcrest:hamcrest-all:1.3')
    testCompile('org.mockito:mockito-all:1.9.5')



war 
    from 'src/main' // include source files in war

我运行这个

❯ ./gradlew build -i                                                                                                                                                                                                                      [12:40:40]
Starting Build
Settings evaluated using settings file '/Users/eladb/WorkspaceQa/java/MobileAutomationWebService/settings.gradle'.
Projects loaded. Root project using empty build file.
Included projects: [root project 'MobileAutomationWebService', project ':library-services', project ':web-services']
Evaluating root project 'MobileAutomationWebService' using empty build file.
Evaluating project ':library-services' using build file '/Users/eladb/WorkspaceQa/java/MobileAutomationWebService/library-services/build.gradle'.
Compiling build file '/Users/eladb/WorkspaceQa/java/MobileAutomationWebService/library-services/build.gradle' using StatementExtractingScriptTransformer.
Compiling build file '/Users/eladb/WorkspaceQa/java/MobileAutomationWebService/library-services/build.gradle' using BuildScriptTransformer.

FAILURE: Build failed with an exception.

* Where:
Script '/Users/eladb/WorkspaceQa/gradle/base.gradle' line: 28

* What went wrong:
A problem occurred evaluating script.
> Cannot add task ':library-services:wrapper' as a task with that name already exists.

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

BUILD FAILED

还有这个base.gradle

project.ext.flag =  String name ->
    if (!project.hasProperty(name)) 
            return false
    

    String value = project.ext[name]
    value = value.toLowerCase()

    if (value == "true" || value == "yes" || value == "1") 
            return true
    

    if (value == "false" || value == "no" || value == "0" || value == "") 
            return false
    

    throw new IllegalArgumentException("Invalid value of flag property \"$name\": \"$value\", must be one of true, false, yes, no, 0, 1 or empty")


////////////////////////////////////////////////////////////////////////////////////////
// Standard settings (can be overridden on a per-project basis)
////////////////////////////////////////////////////////////////////////////////////////

sourceCompatibility = 1.6

compileJava.options.encoding = 'UTF-8'

task wrapper(type: Wrapper) 
    gradleVersion = '1.12'


////////////////////////////////////////////////////////////////////////////////////////
// Waze "Be Fast" (tm)
////////////////////////////////////////////////////////////////////////////////////////
configurations.all 
    if (flag('waze.beFast')) 
        resolutionStrategy.cacheDynamicVersionsFor 1, 'hours'
    
    else 
        resolutionStrategy.cacheDynamicVersionsFor 0, 'minutes'
    


////////////////////////////////////////////////////////////////////////////////////////
// Common repositories config.
////////////////////////////////////////////////////////////////////////////////////////
repositories 
    mavenCentral()
    maven 
        url getWazeRepoUrl()
    
    maven 
        url "http://www.hibernatespatial.org/repository"
    


def getWazeRepoUrl() 
    if (project.hasProperty('buildMachine')) 
        "/data/archiva/repositories/internal/"
     else 
        "https://waze-repo.corp.google.com/archiva/repository/internal/"
    


////////////////////////////////////////////////////////////////////////////////////////
// Add 'provided' scope which is missing from Gradle by default.
////////////////////////////////////////////////////////////////////////////////////////
configurations 
    provided


sourceSets 
    main 
        compileClasspath += configurations.provided
        runtimeClasspath += configurations.provided
    
    test 
        compileClasspath += configurations.provided
        runtimeClasspath += configurations.provided
    


////////////////////////////////////////////////////////////////////////////////////////
// Eclipse-specific configuration.
////////////////////////////////////////////////////////////////////////////////////////
    if (hasProperty('web') && web) 
        apply plugin: 'eclipse-wtp'
     else 
        apply plugin: 'eclipse'
    

    tasks.eclipse.dependsOn(cleanEclipse)

    eclipse 
        pathVariables 'GRADLE_USER_HOME': gradle.gradleUserHomeDir

        classpath 
            plusConfigurations += [configurations.provided]
            noExportConfigurations += [configurations.provided]
        
    
    project.afterEvaluate 
      // use jre lib matching version used by project, not the workspace default
        if (project.sourceCompatibility != null) 
            def target = project.targetCompatibility.toString()
            def containerPrefix = "org.eclipse.jdt.launching.JRE_CONTAINER"
            def containerSuffix = '/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-' + target
            if (containerSuffix != null) 
                project.eclipse.classpath 
                    containers.removeAll  it.startsWith(containerPrefix) 
                    containers.add(containerPrefix + containerSuffix)
                
            
        
    

////////////////////////////////////////////////////////////////////////////////////////
// IntelliJ-specific configuration.
////////////////////////////////////////////////////////////////////////////////////////
if (flag('waze.idea')) 
    apply plugin: 'idea'

    idea 
        module 
            scopes.PROVIDED.plus += [configurations.provided]
        
    

我不明白为什么上面写着Cannot add task ':library-services:wrapper' as a task with that name already exists.

我看不到我的 gradle.build 甚至尝试添加 "wrapping" 任务

我的 gradle.build 不会尝试添加任务“包装器” 它没有出现在 base.gradle' line: 28

【问题讨论】:

你能显示 library-services/build.gradle 吗? 已添加。但我的 gradle.build 没有尝试添加任务“包装器”,并且它没有出现在 base.gradle' 行:28 【参考方案1】:

您是否将 base.gradle 应用于所有子项目?您是否也将其应用于您的根项目?

gradle 中的 wrapper 任务只添加到根项目中,不添加到子项目中。如果您将 base.gradle 应用于所有项目,那将是您的问题的原因。

【讨论】:

你在哪里看到我添加了“android”插件? 我很抱歉。我正在回答一系列 android-gradle 问题,然后在我的脑海中填写。重新尝试回答。

以上是关于无法将任务“X”添加为具有该名称的任务已存在的主要内容,如果未能解决你的问题,请参考以下文章

Gradle Ant 无法添加任务 ':myproject:test' 作为具有该名称的任务已存在

Firebase云消息传递FCM构建错误无法添加任务':processDebugGoogleServices'作为具有该名称的任务已存在

Gradle productFlavors 引发错误 - 无法添加任务“:assembleTest”,因为具有该名称的任务已存在

无法添加任务':processDebugGoogleServices'作为具有该名称的任务已存在

Gradle:无法添加任务':helloFromBuild1',因为具有该名称的任务已经存在

Grails gradle“具有该名称的任务已经存在”