Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom task does not run

My following project structure looks like:

.
├── build.gradle
├── out.txt
├── Server
│   ├── build.gradle
│   ├── plugins
│   │   └── TestPlugin-0.1.0.zip
│   └── src
├── pluginApi
│   ├── build
│   ├── build.gradle
│   └── src
├── plugins
│   └── testPlugin
│       ├── build
│       │   ├── libs
│       │   │   └── TestPlugin-0.1.0.zip
│       ├── build.gradle
│       └── src
└── settings.gradle

Now I want to create a gradle task in the main build.gradle which should do something like that:

  • Delete the content of Server/plugins
  • Execute the "createZip" task for every gradle project inside plugins/. E.g. plugins/testPlugin:createZip
  • Copy the output from every plugin (plugins//build/libs/.zip) into Server/plugins

I managed to write a simple task like this:

task compilePluginsAndCopy() {
    delete 'Server/plugins'
    mkdir 'Server/plugins'

    subprojects.each { p ->
        if (p.path.contains("plugins/")) {

            p.createZip

            doLast {
                copy {
                    from p.path.substring(1) + '/build/libs'
                    into 'Server/plugins'
                }
            }
        }
    }
}

When I now run this task the zip file doesn't get created and nothing gets deleted. However when I run the "createZip" task on one of the subprojects the task compilePluginsAndCopy gets executed.

Could somebody help me, please?

like image 914
Lyze Avatar asked Aug 11 '26 10:08

Lyze


1 Answers

Your task definitions should look like this:

task compilePluginsAndCopy() << {
...
}

note the <<.

<< is short hand for doLast. The non short hand way to do this would be:

task compilePluginsAndCopy() {
    doLast {
        ...
    }
}

If your code is not contained in a doLast block, it is executed in the configuration phase, which is what you're seeing when you run a different task.

like image 76
RaGe Avatar answered Aug 13 '26 16:08

RaGe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!