Determine if a task is defined in an external build.gradle file

时光总嘲笑我的痴心妄想 提交于 2021-02-08 13:43:23

问题


I have a gradle task that is created at runtime to call another task ("myOtherTask") which is in a separate gradle file. The problem is if that other task doesn't exist an exception will be thrown. Is it possible to check that a task exists in an external gradle file before attempting to call it?

Example:

  task mainTaskBlah(dependsOn: ':setupThings')
    task setupThings(){
    //...
    createMyOtherTask(/*...*/)
    //...
}
def createMyOtherTask(projName, appGradleDir) {
    def taskName = projName + 'blahTest'
    task "$taskName"(type: GradleBuild) {
        buildFile = appGradleDir + '/build.gradle'
        dir = appGradleDir
        tasks = ['myOtherTask']
    }
    mainTaskBlah.dependsOn "$taskName"
}

回答1:


You can check if the tasks exists. For example if we wanted to simulate this we could make the task creation triggered by a command line property

apply plugin: "groovy"

group = 'com.jbirdvegas.q41227870'
version = '0.1'

repositories {
    jcenter()
}

dependencies {
    compile localGroovy()
}

// if user supplied our parameter (superman) then add the task
// simulates if the project has or doesn't have the task
if (project.hasProperty('superman')) {
    // create task like normal
    project.tasks.create('superman', GradleBuild) {
        println "SUPERMAN!!!!"
        buildFile = project.projectDir.absolutePath + '/build.gradle'
        dir = project.projectDir.absolutePath
        tasks = ['myOtherTask']
    }
}

// check if the task we are interested in exists on the current project
if (project.tasks.findByName('superman')) {
    // task superman exists here we do whatever work we need to do
    // when the task is present
    def supermanTask = project.tasks.findByName('superman')
    project.tasks.findByName('classes').dependsOn supermanTask
} else {
    // here we do the work needed if the task is missing
    println "Superman not yet added"
}

Then we can see both uses cases rather easily

$ ./gradlew -q build -Psuperman
SUPERMAN!!!!
$ ./gradlew -q build
Superman not yet added



回答2:


This won't help you find if the task is in a specific external file, but if you just want to determine if a task is defined in any of your imported gradle files...

From gradlew help I see there is a tasks task.

Sadly, gradlew tasks doesn't always show all taks. Some of my projects have an integrationTest task while others do not, in which case I can only go as far as build. However, the default tasks command lists integrationTestClasses but not integrationTest.

From gradlew help --task tasks I can see there is a report expanding --all parameter that we can use.

Now, I can see all tasks via gradlew tasks --all, so a simple grep can tell me whether or not the task I want exists. In bash, this might look like:

TASK="integrationTest"
if gradlew tasks --all | grep -qw "^$TASK"
then
    gradlew clean integrationTest
else
    gradlew clean build
fi

FYI -- Personally, I needed something to tell me in a git pre-commit hook whether the integrationTest task existed or not, so I know whether I can run gradlew integrationTest or if I have to stop at gradlew build. Not finding an answer here, I kept looking, and this is what I came up with to solve my problem. Hopefully, this is of use to others as well.




回答3:


I made a little "tool" for stuff like that - maybe it comes in handy for some of you...

$ cat if_gradle_task_exists
#!/bin/sh

TASKS=$(./gradlew tasks --all)
BUILD="./gradlew "

for COMMAND in $@; do
        echo "$TASKS" | grep -q "$COMMAND" && BUILD="$BUILD $COMMAND"
done
$BUILD

You can append as many tasks as you like and only the existing ones are executed.

I use it in combination with alias as a kind of super api wrapper for tasks I want to be done in different projects (and I don't want to have to care if any of the specific tasks really do exist):

alias ge='~/.config/bin/if_gradle_task_exists eclipse initDb createTestUsers startLdapServerMock startBrokerMock'

That allows me to be as lazy as ge

stumpf@HV000408:/c/devel/workspace/myproject $> ge

to set up the project for eclipse and prepare all needed servers for local development.

The list of tasks produced will need some time to be set-up, so I wouldn't recommend to use it as a full gradlew wrapper though.

Regards



来源:https://stackoverflow.com/questions/41227870/determine-if-a-task-is-defined-in-an-external-build-gradle-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!