Gradle任务在执行Gradle构建时显示错误。

huangapple go评论60阅读模式
英文:

Gradle Task showing error when executing in the gradle build

问题

我遇到的错误是class java.lang.Integer cannot be cast to class java.lang.String。当运行gradle test -Pnum=10时。
这是我的代码:

def fibo(n){
    //something
}
task test() {
    doLast {
        fibo num
    }
}
英文:

The error that i am facing is the class java.lang.Integer cannot be cast to class java.lang.String. when running gradle test -Pnum=10.
This is my code:

     def fibo(n){
 //something
        }
    }
        task test() {
           doLast {
               fibo num
           }
        }

答案1

得分: 1

当使用参数 -Pnum=10 输入 num 的值时,num 将是一个 String,但您需要一个 Integer

像这样更改任务 test 应该可以解决问题:

task test() {
   doLast { 
       fibo num.toInteger()
   }
}

有关在 Groovy 中将 String 转换为 Integer 的更多信息,请参阅 此答案

英文:

When entering the value of num using the parameter -Pnum=10, num will be a String, but you need an Integer.

Changing the task test like this should fix it:

task test() {
   doLast { 
       fibo num.toInteger()
   }
}

See this answer for more information about converting a String to an Integer in Groovy.

答案2

得分: 0

将您的参数在使用之前转换为整数,因为它是作为字符串传递的。

int nValue = Integer.valueOf(n);
英文:

Cast you argument to integer before using, as its passed as string
int nValue = Integer.valueOf(n);

huangapple
  • 本文由 发表于 2020年9月7日 14:12:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63772222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定