groovy 脚本中的 MissingMethodException
MissingMethodException in groovy scripting
我是 groovy 脚本的新手。我在远程盒子上 运行ning 脚本。像往常一样使用 jfrog 删除工件脚本 (https://www.jfrog.com/blog/advanced-cleanup-using-artifactory-query-language-aql/)。根据我的 requirement.but 更新主机,每当 运行 我得到缺少方法异常的命令时。
Artifactory 4.1.3 版本 40020 PRO
@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6')
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import org.apache.http.conn.HttpHostConnectException
/**
* Created by shaybagants on 4/30/15.
*/
def query = 'items.find({"type" : "file","name":{"$eq":"webapp-194-20140604002145-7c157a8afcd3e8fc6a9ecbbf0ec45153991ef23d.war"}})' // replace this with your AQL query
def artifactoryURL = 'http://localhost:8081/artifactory/' // replace this with your Artifactory server
def restClient = new RESTClient(artifactoryURL)
restClient.setHeaders(['Authorization': 'Basic ' + "admin:password".getBytes('iso-8859-1').encodeBase64()]) //replace the 'admin:password' with your own credentials
def dryRun = true //set the value to false if you want the script to actually delete the artifacts
def itemsToDelete = getAqlQueryResult(restClient, query)
if (itemsToDelete != null && itemsToDelete.size() > 0) {
delete(restClient, itemsToDelete, dryRun)
} else {
println('Nothing to delete')
}
/**
* Send the AQL to Artifactory and collect the response.
*/
public List getAqlQueryResult(RESTClient restClient, String query) {
def response
try {
response = restClient.post(path: 'api/search/aql',
body: query,
requestContentType: 'text/plain'
)
} catch (Exception e) {
println(e.message)
}
if (response != null && response.getData()) {
def results = [];
response.getData().results.each {
results.add(constructPath(it))
}
return results;
} else return null
}
/**
* Construct the full path form the returned items.
* If the path is '.' (file is on the root) we ignores it and construct the full path from the repo and the file name only
*/
public constructPath(HashMap item) {
if (item.path.toString().equals(".")) {
return item.repo + "/" + item.name
}
return item.repo + "/" + item.path + "/" + item.name
}
/**
* Send DELETE request to Artifactory for each one of the returned items
*/
public delete(RESTClient restClient, List itemsToDelete, def dryRun) {
dryMessage = (dryRun) ? "*** This is a dry run ***" : "";
itemsToDelete.each {
println("Trying to delete artifact: '$it'. $dryMessage")
try {
if (!dryRun) {
restClient.delete(path: it)
}
println("Artifact '$it' has been successfully deleted. $dryMessage")
} catch (HttpResponseException e) {
println("Cannot delete artifact '$it': $e.message" +
", $e.statusCode")
} catch (HttpHostConnectException e) {
println("Cannot delete artifact '$it': $e.message")
}
}
}
异常
Caught: groovy.lang.MissingMethodException: No signature of method: test.constructPath() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[created:2014-06-04T00:21:55.149Z, created_by:jenkins, modified:2014-06-04T00:21:55.146Z, ...]]
Possible solutions: constructPath(java.util.HashMap)
groovy.lang.MissingMethodException: No signature of method: test.constructPath() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[created:2014-06-04T00:21:55.149Z, created_by:jenkins, modified:2014-06-04T00:21:55.146Z, ...]]
Possible solutions: constructPath(java.util.HashMap)
at test$_getAqlQueryResult_closure1.doCall(test.groovy:41)
at test.getAqlQueryResult(test.groovy:40)
at test$getAqlQueryResult.callCurrent(Unknown Source)
at test.run(test.groovy:16)
AQL查询结果。
{
"results" : [ {
"repo" : "war-release",
"path" : "webapp",
"name" : "webapp-194-20140604002145-7c157a8afcd3e8fc6a9ecbbf0ec45153991ef23d.war",
"type" : "file",
"size" : 87411497,
"created" : "2014-06-04T00:21:55.149Z",
"created_by" : "jenkins",
"modified" : "2014-06-04T00:21:55.146Z",
"modified_by" : "jenkins",
"updated" : "2014-06-04T00:21:55.146Z"
} ],
"range" : {
"start_pos" : 0,
"end_pos" : 1,
"total" : 1
}
}
嗯,报错信息比较清楚了。
您调用一个方法 constructPath()
,它期望一个 HashMap
作为参数,但是给它一个 LazyMap
,这是不同的。
让你的方法 constructPath()
期望一个 Map
并且如果 LazyMap
实现 Map
它将工作。
我是 groovy 脚本的新手。我在远程盒子上 运行ning 脚本。像往常一样使用 jfrog 删除工件脚本 (https://www.jfrog.com/blog/advanced-cleanup-using-artifactory-query-language-aql/)。根据我的 requirement.but 更新主机,每当 运行 我得到缺少方法异常的命令时。
Artifactory 4.1.3 版本 40020 PRO
@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.6')
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import org.apache.http.conn.HttpHostConnectException
/**
* Created by shaybagants on 4/30/15.
*/
def query = 'items.find({"type" : "file","name":{"$eq":"webapp-194-20140604002145-7c157a8afcd3e8fc6a9ecbbf0ec45153991ef23d.war"}})' // replace this with your AQL query
def artifactoryURL = 'http://localhost:8081/artifactory/' // replace this with your Artifactory server
def restClient = new RESTClient(artifactoryURL)
restClient.setHeaders(['Authorization': 'Basic ' + "admin:password".getBytes('iso-8859-1').encodeBase64()]) //replace the 'admin:password' with your own credentials
def dryRun = true //set the value to false if you want the script to actually delete the artifacts
def itemsToDelete = getAqlQueryResult(restClient, query)
if (itemsToDelete != null && itemsToDelete.size() > 0) {
delete(restClient, itemsToDelete, dryRun)
} else {
println('Nothing to delete')
}
/**
* Send the AQL to Artifactory and collect the response.
*/
public List getAqlQueryResult(RESTClient restClient, String query) {
def response
try {
response = restClient.post(path: 'api/search/aql',
body: query,
requestContentType: 'text/plain'
)
} catch (Exception e) {
println(e.message)
}
if (response != null && response.getData()) {
def results = [];
response.getData().results.each {
results.add(constructPath(it))
}
return results;
} else return null
}
/**
* Construct the full path form the returned items.
* If the path is '.' (file is on the root) we ignores it and construct the full path from the repo and the file name only
*/
public constructPath(HashMap item) {
if (item.path.toString().equals(".")) {
return item.repo + "/" + item.name
}
return item.repo + "/" + item.path + "/" + item.name
}
/**
* Send DELETE request to Artifactory for each one of the returned items
*/
public delete(RESTClient restClient, List itemsToDelete, def dryRun) {
dryMessage = (dryRun) ? "*** This is a dry run ***" : "";
itemsToDelete.each {
println("Trying to delete artifact: '$it'. $dryMessage")
try {
if (!dryRun) {
restClient.delete(path: it)
}
println("Artifact '$it' has been successfully deleted. $dryMessage")
} catch (HttpResponseException e) {
println("Cannot delete artifact '$it': $e.message" +
", $e.statusCode")
} catch (HttpHostConnectException e) {
println("Cannot delete artifact '$it': $e.message")
}
}
}
异常
Caught: groovy.lang.MissingMethodException: No signature of method: test.constructPath() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[created:2014-06-04T00:21:55.149Z, created_by:jenkins, modified:2014-06-04T00:21:55.146Z, ...]]
Possible solutions: constructPath(java.util.HashMap)
groovy.lang.MissingMethodException: No signature of method: test.constructPath() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[created:2014-06-04T00:21:55.149Z, created_by:jenkins, modified:2014-06-04T00:21:55.146Z, ...]]
Possible solutions: constructPath(java.util.HashMap)
at test$_getAqlQueryResult_closure1.doCall(test.groovy:41)
at test.getAqlQueryResult(test.groovy:40)
at test$getAqlQueryResult.callCurrent(Unknown Source)
at test.run(test.groovy:16)
AQL查询结果。
{
"results" : [ {
"repo" : "war-release",
"path" : "webapp",
"name" : "webapp-194-20140604002145-7c157a8afcd3e8fc6a9ecbbf0ec45153991ef23d.war",
"type" : "file",
"size" : 87411497,
"created" : "2014-06-04T00:21:55.149Z",
"created_by" : "jenkins",
"modified" : "2014-06-04T00:21:55.146Z",
"modified_by" : "jenkins",
"updated" : "2014-06-04T00:21:55.146Z"
} ],
"range" : {
"start_pos" : 0,
"end_pos" : 1,
"total" : 1
}
}
嗯,报错信息比较清楚了。
您调用一个方法 constructPath()
,它期望一个 HashMap
作为参数,但是给它一个 LazyMap
,这是不同的。
让你的方法 constructPath()
期望一个 Map
并且如果 LazyMap
实现 Map
它将工作。