Is there a way to configure multiple gradle tasks at once? - gradle

I have the following snippet in a build.gradle file for a project I work on:
tasks.named('test').configure {
dependsOn customTask
}
tasks.named('compileTestJava').configure {
dependsOn customTask
}
tasks.named('jar').configure {
dependsOn customTask
}
tasks.named('build').configure {
dependsOn customTask
}
It seems redundant to have to specify the the same configuration for 4 tasks separately. Is there a way to select and configure multiple tasks at once? Perhaps something like:
tasks.named('test', 'compileTestJava', 'jar', 'build').configure {
dependsOn customTask
}
(I tried this code, but it does not work.)

You can do
['a', 'b', 'c'].each {
tasks.named(it).configure {
dependsOn 'customTask'
}
}

Related

Configuration inheritance and resolution

I have some trouble with gradle and I don't know why this doesn't work:
Project A with 2 sub-project B and C
B and C have a configuration named masterConfiguration and superConfiguration (who extend masterConfiguration)
I add some dependencies in both.
When I do this:
configurations.superConfigurations.resolvedConfiguration.files
All is fine, and I have all files from superConfiguration and masterConfiguration.
Now, the problem.
I create a configuration (projectAConfiguration) in the project A (the rootProject).
This configuration extends superConfiguration from B and C.
I add no new dependencies in this one.
If I do this:
configurations.projectAConfiguration.resolvedConfiguration.files
I have nothing. I don't understand why?
settings.gradle =>
include 'B'
include 'C'
build.gradle =>
configurations {
projectAConfiguration
}
def rootConfiguration = configurations.projectAConfiguration
subprojects {
configurations {
masterConfiguration
superConfiguration {
extendsFrom masterConfiguration
}
}
rootConfiguration.extendsFrom configurations.superConfiguration
dependencies {
masterConfiguration 'group:artifactid:version'
superConfiguration 'anotherGroup:anotherArtifactid:version'
}
//ALL IS OK
println configurations.superConfiguration.resolvedConfiguration.files
}
//NOT OK
println configurations.projectAConfiguration.resolvedConfiguration.files
I have solve my problem with this solution :
Add task on all subproject.
task resolveMyConf {
doLast {
it.ext.confResolve = it.configurations.superConfiguration..resolvedConfiguration.files
}
}
and in the project A
task resolveAllConf {
suprojects.each {
dependsOn it.resolveMyConf
}
doLast {
//and now, we can collect all task result
}
}
I don't know if it's good, maybe there is a better solution. But it works.
You can get the configuration of a sub-project just by referencing the project in the same way as you would declare a dependency to it. You don't need to create another configuration that extends it.
For example (Groovy DSL):
// Root project A
task printConfigurationB {
doLast {
println project("B").configurations.superConfiguration.resolve()
}
}

How to ignore dependsOn failure because some subprojects don't have the defined task

I have a multi-project gradle build where not all of the subprojects have the same plugin, but I would like to define tasks in the root build.gradle file like this:
subprojects {
task continuousBuild(dependsOn: ["clean", "check", "jacocoTestReport", "integrationTests"]
}
Not all subprojects have "jacocoTestReport" or "integrationTests" defined, but this task will fail because of that fact. How do I configure this to work, and frankly, why is the default behavior so strict?
This is what ended up working for me:
task continuousBuild(dependsOn: ['clean', 'check']) {
def uncommonTasks = ['jacocoTestReport', 'integrationTests']
dependsOn += tasks
.findAll { uncommonTasks.contains(name) }
}
And I forgot that I actually needed to run integrationTests in a doLast, which looks like this:
task continuousBuild(dependsOn: ['clean', 'check']) {
dependsOn += tasks
.findAll { 'jacocoTestReport' == name) }
if (tasks.findByName('integrationTests')) {
doLast {
integrationTests
}
}
}

How do I create my own configuration block with a Gradle script plugin?

Our company has a Gradle script plugin with a number of tasks in it. For instance, it includes the Jacoco afterEvaluate block from this answer:
def pathsToExclude = ["**/*Example*"]
jacocoTestReport {
afterEvaluate {
classDirectories = files(classDirectories.files.collect {
fileTree(dir: it, exclude: pathsToExclude)
})
}
}
We would like to take that pathsToExclude variable and define that in our build.gradle file and have the rest of the logic in a script plugin (let's call it company-script-plugin.gradle. For instance:
apply from: http://example.com/company-script-plugin.gradle
companyConfiguration {
pathsToExclude = ["**/*Example*"]
}
Our initial thought was to add a task in the build script so that we could get a companyConfiguration
task companyConfiguration {
ext.pathsToExclude = []
}
However, we think that this is a hacky workaround because running the task doesn't do anything. What is the proper way to create my own configuration block?
We'd like it to be as simple as possible, and to be a script plugin (rather than binary plugin) if possible.
Here you've an example how it can be done:
apply plugin: CompanyPlugin
companyConfiguration {
pathsToExclude = ['a', 'b', 'c']
}
class CompanyPlugin implements Plugin<Project> {
void apply(Project p) {
println "Plugin ${getClass().simpleName} applied"
p.extensions.create('companyConfiguration', CompanyConfigurationExtension, p)
}
}
class CompanyConfigurationExtension {
List<String> pathsToExclude
CompanyConfigurationExtension(Project p) {
}
}
task printCompanyConfiguration {
doLast {
println "Path to exclide $companyConfiguration.pathsToExclude"
}
}
Also, please have a look at the docs.

How do I reuse task configuration in Gradle?

I have task war configuration with many from/include/exclude:
task war {
exclude
exclude
...
into ... from ...
into ... from ...
}
I have another task war configuration which is the same except one exclude.
I don't want to duplicate those configurations. How can I reuse the first configuration?
Try:
ext.sharedCopyConf = { task, to ->
configure(task) {
into to
from 'a'
}
}
task copy1(type: Copy) { t ->
sharedCopyConf(t, 'b')
}
task copy2(type: Copy) { t ->
sharedCopyConf(t, 'c')
}
Have a look at the demo.
ext.sharedWarConfig = { task->
configure(task) {
from ... include ...
}}
task warWithoutFile(type: War) { task ->
sharedWarConfig(task)
exclude ...
}
task warWithFile(type: War) { task -> sharedWarConfig(task) }
jettyRunWar {
dependsOn warWithFile
dependsOn.remove("war")
webApp = warWithFile.archivePath
}

Is there a way to "merge" gradle tasks to avoid many dependsOn declarations

Suppose this gradle script:
task copyGroovyScript(dependsOn: prepare, type: Copy) {
from "${scriptSrcLocation}/${scriptSrcName}"
into buildFolderZipSource
}
task copyDependenciesForGroovyScript(dependsOn: copyGroovyScript, type: Copy) {
from configurations.groovyScript.resolve()
into "${buildFolderZipSource}/groovy-plugin-lib"
}
task copyTestScripts(dependsOn: copyDependenciesForGroovyScript, type: Copy ) {
from "${scriptSrcLocation}/ReadClient.groovy"
into "${buildFolderZipSource}/test"
}
task copyTestScriptsBin(dependsOn: copyTestScripts, type: Copy ) {
from "${scriptSrcLocation}/bin"
into "${buildFolderZipSource}/test/bin"
}
task copyDependenciesForTestScripts(dependsOn: copyTestScriptsBin, type: Copy) {
from configurations.testScripts.resolve()
into "${buildFolderZipSource}/test/lib"
}
task packageAll(dependsOn: copyDependenciesForTestScripts, type:Zip) {
archiveName "output-${buildTime()}.zip"
excludes ['*.zip']
destinationDir buildFolder
from buildFolder
}
I need different Copy tasks before they have different output folders.
Is there a way to avoid having to have all those dependsOn statements and just have gradle execute things in order of declaration in the file somehow?
There's no way to execute in the way it's declared. But why don't you go this way:
task packageAll(dependsOn: copyDependenciesForTestScripts, type:Zip) {
doFirst {
copy {
from "${scriptSrcLocation}/${scriptSrcName}"
into buildFolderZipSource
}
}
//following doFirst and so on..
archiveName "output-${buildTime()}.zip"
excludes ['*.zip']
destinationDir buildFolder
from buildFolder
}
EDIT
After discussion in comments it turned out that the following piece of code should do the job
task prepare {
doFirst {
copy {
from "${scriptSrcLocation}/${scriptSrcName}"
into buildFolderZipSource
}
}
//following doFirst and so on..
}
task packageAll(dependsOn: prepare, type:Zip) {
archiveName "output-${buildTime()}.zip"
excludes ['*.zip']
destinationDir buildFolder
from buildFolder
}

Resources