Need help in gradle task and depends on example


(Vinay Verma) #1

I am working on a gradle task from about 3 4 days and could not build a proper solution yet. The problem statement is something like this:

There is a task deploy which I need to run, now the code under this task should be something like this:

task deploy <<{
if (WebContent.equals("Yes") && ServiceContent.equals("Yes"))
{
		println "\nWeb and Services both the parts are present in the Module"
		tasks.explodedWar.execute()
		tasks.deployWebAndService.execute()
     }
else 
if (WebContent.equals("Yes") && ServiceContent.equals("No"))
{
		println "\nWebContent is present but not Service in the Module"
		tasks.explodedWar.execute()
		tasks.deployWeb.execute()
}
else
if(WebContent.equals("No") && ServiceContent.equals("Yes"))
{
	println "\nWebContent is NOT present but ServiceContent is Present in the Module"
	tasks.deployService.execute()
}
}

and tasks which are being called for execution are having content like this:

task deployWeb (dependsOn : 
[':configureWebApp',':stopWebApp',':uninstallWebApp',':installWebApp',':startWebApp']) <<{
		println "\n\tExecuting deploy for Web Content"
}
task deployService (dependsOn : [':configureService',':stopService',':uninstallService',':installService',':startService']) <<{
		println "\n\tExecuting deploy for Service Content"
}
task deployWebAndService (dependsOn : [':configureWebApp',':configureService',':stopWebApp',':uninstallWebApp',':installWebApp',':startWebApp',':stopService',':uninstallService',':installService',':startService']) <<{
		println "\n\tExecuting deploy for Web and Service Content"
}

When I am running gradle after calling task deploy, it is calling respective deployWeb or Service task but doing nothing.

Please help me get the solution for the problem. How I can use OnlyIf or some other function functionality of gradle so that I only call deploy task while running and it executes respective gradle task on the condition basis.

Thanks in Advance!


(Rowan Lonsdale) #2

Where/when are you setting the WebContent and ServiceContent variables?


(Vinay Verma) #3

I have made entries about my different projects in a single properties file (Saying which project have WebContent, which have ServiceContent and which project have Both.).
And I am reading that file using input buffer to get those values.

possible values for WebContent and ServiceContent can be “Yes” or “No”.

Until this part (means getting the values for these variables), I could successfully do.


(Rowan Lonsdale) #4

So do you read the properties file in as part of the execution of a task, or at configuration time?


(Vinay Verma) #5

At the configuration time only… Means reading the properties file is not a part of any task…

One more thing, I tried on some other solution also, under which I prepared a deploy task which says it is dependent on deployWeb, deployService, deployWebAndService and condition is from among these all, only one should run. (I used OnlyIf function of gradle). But that is also not helpful…

Please check at the following link: here

In this particular case, when only ServiceContent is Yes and WebContent is No, still then all the tasks are being executed one by one, which is not the solution I am searching for.

Requirement is: If ServiceContent is only Yes, only deployService task should be executed
If WebContent is only Yes, only deployWeb task should be executed
and If ServiceContent and WebContent both are Yes, then only deployWebAndService task should be executed.


(Rowan Lonsdale) #6

Ah - I understand the issue now.

The problem you’re having, I think, is that you’re treating tasks like methods, and dependsOn like “invoke these methods then execute the code attached to this task”. It doesn’t work like that.

In particular, dependsOn means “make sure that these tasks have been run before this task runs”. If a task is skipped because its onlyIf check fails, its dependencies will still be run (in fact, its dependencies are run before the onlyIf condition is even checked).

I’ll try to sketch out what a workable solution might be, and hopefully you can read the docs to work out how it all fits together, and how to fix anything where I’ve made the wrong assumption about what you’re trying to achieve:

//First, define some methods that we can call from tasks

def explodeWarContent() {
    println 'Code to explode war'
}

def configureWebApp() {
    println 'Code to configure web app'
}

def startWebApp() {
    println 'Code to start web app'
}

def stopWebApp() {
    println 'Code to stop web app'
}

def installWebApp() {
    println 'Code to install web app'
}

def uninstallWebApp() {
    println 'Code to uninstall web app'
}

def configureService() {
    println 'Code to configure service'
}

def startService() {
    println 'Code to start service'
}

def stopService() {
    println 'Code to stop service'
}

def installService() {
    println 'Code to install service'
}

def uninstallService() {
    println 'Code to uninstall service'
}

// Now, define tasks

task deployWeb {
    onlyIf WebContent.equals("Yes")

    doLast {
        println 'Deploying web app'
        explodeWarContent()
        configureWebApp()
        stopWebApp()
        uninstallWebApp()
        installWebApp()
        startWebApp()
    }
}

task deployService {
    onlyIf ServiceContent.equals("Yes")

    doLast {
        println 'Deploying service'
        configureService()
        stopService()
        uninstallService()
        installService()
        startService()
    }
}

task deploy(dependsOn: [deployWeb, deployService])

(Rowan Lonsdale) #7

Alternatively, if you’d like to keep each action as a separate task, so you can invoke them individually when needed from the command line, maybe a set-up like this would work:

// Web app tasks ------------------------------------------------------------------------------

task explodeWarContent << {
    println 'Code to explode war'
}

task configureWebApp(depndsOn: explodeWarContent) << {
    println 'Code to configure web app'
}

task stopWebApp << {
    println 'Code to stop web app'
}

task uninstallWebApp(dependsOn: stopWebApp) << {
    println 'Code to uninstall web app'
}

task installWebApp(dependsOn: configureWebApp, mustRunAfter: uninstallWebApp) << {
    println 'Code to install web app'
}

task startWebApp(dependsOn: installWebApp, mustRunAfter: stopWebApp) << {
    println 'Code to start web app'
}

task deployWebApp(dependsOn: [uninstallWebApp, startWebApp])

// Service tasks -----------------------------------------------------------------------------

task configureService << {
    println 'Code to configure service'
}

task stopService << {
    println 'Code to stop service'
}

task uninstallService(dependsOn: stopService) << {
    println 'Code to uninstall service'
}

task installService(dependsOn: configureService, mustRunAfter: uninstallService) << {
    println 'Code to install service'
}

task startService(dependsOn: installService, mustRunAfter: stopService) << {
    println 'Code to start service'
}

task deployService(dependsOn: [uninstallService, startService])

// Other tasks --------------------------------------------------------------------------------

task deploy

if(WebContent.equals("Yes")) {
    deploy.dependsOn deployWebApp
}

if(ServiceContent.equals("Yes")) {
    deploy.dependsOn deployService
}

(Vinay Verma) #8

Now This is what the solution is… Thanks a lot for your time and providing the solution :innocent:

It Worked!
:gift: