-1

Facing problem in a question:

Write a gradle program to generate 10 fibonaci series, with task name as fibo, and variable name as num. Use command line argument for num. For example, if a task name is test and I want to pass 10 as the input, use gradle test -Pnum=10.

I have created a function:

def fibo(n){
    a = 0
    b = 1
    if (n == 1)
    println a
    else if
    (n == 2)
    println a + " " + b
    else if (n > 2) {
        print a + " " + b
        i = 2
        while (i <= n)
        {
        c = a + b
        print " " + c
        a = b
        b = c
        i = i + 1
        }
    }
}

My question is, how to link it with a task as I encounter error like:

FAILURE: Build failed with an exception.

* What went wrong:
Task 'fibo' not found in root project 'root'.

* Try:
Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.61 secs

or how to pass parameters in a gradle task?

Note: Please do not suggest optimization in fibonacci code, thats not a concern for now.

cfrick
  • 35,203
  • 6
  • 56
  • 68
SAGARDX7
  • 1
  • 1
  • 1
  • 4
  • "_A gradle program_"? – tim_yates Mar 31 '20 at 13:59
  • why do you use gradle for this? – K Adithyan Mar 31 '20 at 14:16
  • This is how the question goes :( – SAGARDX7 Apr 01 '20 at 06:53
  • @SAGARDX7 If this was a social experiment, then you should not be surprised, that developers are oddly surprised why this is so specific about the fibinacci series. If this is about writing tasks and calling functions, any function would do and "hello world" is enough to transport the problem and you dont have to defend your fibo function or why you want to call it as an gradle task. And if it _is_ relevant, then it would help alot, if you would make it clear why and what the actual problem here is. – cfrick Apr 01 '20 at 10:39

2 Answers2

1

You can define a task like this:

def hello(name) {
        println "Hello, $name"
}

task sayHello() {
        doLast {
                hello sayHelloTo
        }
}

And call it like this:

% gradle sayHello -PsayHelloTo=World

> Task :sayHello
Hello, World

BUILD SUCCESSFUL in 518ms
1 actionable task: 1 executed

cfrick
  • 35,203
  • 6
  • 56
  • 68
0


def fibo(num)  {
  if (num < 2)  {
    return 1
  } else  {
    return fibo(num-2) + fib(num-1)
  }
}




task (fibo) << {
   println fibo(5)
}


Anshul
  • 1