You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle中如何替换leftShift运算符?解决Task.leftShift弃用警告

Fixing the Gradle 5.0 Task.leftShift(<<) Deprecation Warning

Hey there! Let’s work through this deprecation warning you’re hitting in Jenkins when using your custom plugin. The warning calls out replacing << with doLast, but I know real-world scenarios can throw curveballs—here’s how to handle common cases:

1. Quick Fix for Your Own Task Code

If the warning points to line 12 in your app/build.gradle (your own code), swapping << for doLast is straightforward:

Old Code (Deprecated):

task myCustomTask << {
    println "Executing my custom task logic"
}

Updated Code:

task myCustomTask {
    doLast {
        println "Executing my custom task logic"
    }
}

For super simple tasks, you can even condense it to:

task myCustomTask { doLast { println "Executing my custom task logic" } }

2. When the Warning Comes From the Third-Party Plugin

Since you’re applying com.company.gradleplugins.plugin, the warning might be originating from the plugin’s internal code, not your build script. Here’s what to do:

  • Reach out to the plugin maintainers: Let them know their code uses a deprecated Gradle API and ask for an update to support Gradle 5.0+. This is the cleanest long-term fix.
  • Temporary Workaround (Use With Caution): If you can’t wait for an update, you can override the plugin’s task in your build.gradle to replace the deprecated logic:
    // Replace 'pluginProvidedTask' with the actual task name from the plugin
    tasks.named('pluginProvidedTask') {
        // Optional: Clear existing actions if you want to fully replace them
        actions.clear()
        // Add your updated logic with doLast
        doLast {
            // Replicate the original task's behavior here—you might need to check the plugin's source code to get this right
        }
    }
    
    Heads up: This workaround can break if the plugin updates its task logic later, so only use it as a stopgap.

3. Handling Complex Scenarios (Multiple Actions, Dependencies)

If your tasks have multiple execution steps or depend on other tasks, keep these tips in mind:

  • Multiple Actions: You can call doLast multiple times to add separate execution blocks—they’ll run in the order you define them:
    task multiStepTask {
        doLast {
            println "First step complete"
        }
        doLast {
            println "Second step complete"
        }
    }
    
  • Pre-Execution Setup: If you need logic to run before the main task, use doFirst instead of trying to shoehorn it into << (remember, << only adds to the end of the task’s action list):
    task prepareThenRun {
        doFirst {
            println "Setting up prerequisites..."
        }
        doLast {
            println "Running the main task logic..."
        }
    }
    

4. Verify Your Fix

After making changes, test locally first: run gradle <your-task-name> to confirm the task works as expected, and check if the deprecation warning is gone. Then push your changes to Jenkins and re-run the build to ensure the warning disappears there too.


内容的提问来源于stack exchange,提问作者Óscar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:37:05