我在项目的build.gradle文件中添加了以下任务:
task('clearLibCache', type: Delete, group: 'MyGroup',
description: "Deletes any cached artifacts with the domain of com.test in the Gradle or Maven2 cache directories.") << {
def props = project.properties
def userHome = System.getProperty('user.home')
def domain = props['domain'] ?: 'com.test'
def slashyDomain = domain.replaceAll(/\./, '/')
file("${userHome}/.gradle/caches").eachFile { cacheFile ->
if (cacheFile.name =~ "^$domain|^resolved-$domain") delete cacheFile.path
}
delete "${userHome}/.m2/repository/$slashyDomain"
}
当我点击“清洁项目”菜单时,我希望执行此任务,仅在这种情况下。
怎么做?
答案 0 :(得分:1)
那&#34;清洁项目&#34;引擎盖下的菜单项似乎做了两件事(基于Gradle Console窗口的输出,当你点击它时):
./gradlew clean
我会让您的任务成为Gradle清理任务的依赖项,这样无论何时清理项目,都会调用此任务。这可以通过在声明任务后在clean.dependsOn clearLibCache
中添加行build.gradle
来实现。