我有以下groovy代码:
def script
String credentials_id
String repository_path
String relative_directory
String repository_url
CredentialsWrapper(script, credentials_id, repository_name, repository_group, relative_directory=null) {
this(script, credentials_id, 'git@gitlab.foo.com:' + repository_group +'/' + repository_name + '.git', relative_directory);
}
CredentialsWrapper(script, credentials_id, repository_url, relative_directory=null) {
this.script = script;
this.credentials_id = credentials_id;
this.repository_url = repository_url;
if (null == relative_directory) {
int lastSeparatorIndex = repository_url.lastIndexOf("/");
int indexOfExt = repository_url.indexOf(".git");
this.relative_directory = repository_url.substring(lastSeparatorIndex+1, indexOfExt);
}
}Jenkins给了我以下信息:
由于构造函数@第30行,第7列中的哈希冲突,无法编译类com.foo.CredentialsWrapper。
我不明白为什么,构造函数是不同的,它们没有相同数量的参数。
另外," script“是来自"WorkflowScript”的一个实例,但是我不知道应该导入什么来访问这个类,这将允许我显式地声明脚本,而不是使用"def“。
有什么想法吗?
发布于 2018-08-10 04:59:29
当您使用四个参数调用构造函数时,您希望调用第一个还是第二个参数?
如果您使用默认值编写构造函数/方法,groovy实际上将生成两个或更多版本。所以
Test(String x, String y ="test")将导致
Test(String x, String y) {...}和
Test(String x) {new Test(x, "test")}所以你的代码想要编译成4个构造函数,但是它包含了两次带有签名CredentialsWrapper(def, def, def, def)的构造函数。如果我正确理解了您的代码,您可以省略其中一个或两个=null。结果是一样的,但是你只能得到两到三个签名。然后,您可以通过使用正确的参数计数调用它们来在两个版本之间进行选择。
https://stackoverflow.com/questions/51774414
复制相似问题