求助:如何通过Gradle任务成功删除指定tool.jar文件?
Hey George, I see the issue with your delete task—it's all about the file path! Let's break down what's going wrong and how to fix it.
The Root Problem
Your current task uses delete 'tool.jar', which tells Gradle to look for the jar file in your project root directory (where your build.gradle lives). But your actual jar is buried at build/milestone/deployment/tool/tool.jar, so Gradle can't find it to delete it.
Solutions to Try
1. Use Gradle's Built-in buildDir Property (Recommended)
Gradle provides a buildDir variable that points directly to your project's build folder. This is the most flexible approach (works across different OS and project setups):
task deleteRemainingJarAfterRename (type: Delete) { delete "${buildDir}/milestone/deployment/tool/tool.jar" }
2. Use a File Object for Extra Safety
If you want to avoid any path separator issues (like Windows vs. Unix slashes), you can wrap the path in a file() call:
task deleteRemainingJarAfterRename (type: Delete) { delete file("${buildDir}/milestone/deployment/tool/tool.jar") }
3. Debug to Confirm the Path
If you're still unsure, add a print statement to your task to verify exactly which path Gradle is targeting:
task deleteRemainingJarAfterRename (type: Delete) { def targetJar = "${buildDir}/milestone/deployment/tool/tool.jar" println "Targeting jar for deletion: ${targetJar}" delete targetJar }
Run ./gradlew deploy and check the console output—you'll see the path Gradle is trying to delete, which should match your actual file location.
Double-Check the FinalizedBy Setup
Make sure your deploy task is actually executing, since finalizedBy only runs the delete task after the deploy task completes successfully. If deploy fails or isn't triggered, the delete task won't run either.
Test by running:
./gradlew deploy --info
The --info flag will show you all tasks being executed, so you can confirm if deleteRemainingJarAfterRename is running after deploy.
内容的提问来源于stack exchange,提问作者George Cimpoies

