2017-05-23 43 views
0

我想在GROOVY腳本中實現以下,但出現錯誤: 讀取數組中的HTML文件的內容,然後grep爲該數組中的某些內容讀取內容。Groovy:讀取數組中的文件的內容並grep的東西

def file1 = new File("/path/to/the/file/xyz.html"); 
def lines = file1.readLines() 
if ((-e "/path/to/the/file/xyz.html") and (!(grep /jira.bugtracker.com/, lines)) 
{ 
    println (" the changes are present\n"); 
    exit 0; 
} 
else 
{ 
    println (" the changes are not present\n"); 
    exit 1; 
} 

請檢查代碼並提出正確的方法。

+0

你得到了什麼錯誤?這是在詹金斯文件的上下文中嗎?你可以發佈嗎? – burnettk

回答

2
def file1 = new File("/path/to/the/file/xyz.html") 
def lines = file1.readLines() 
def found = lines.find{ line-> line =~ /jira.bugtracker.com/ } 
println ("the changes are ${ found ? '' : 'not ' }present") 
return found ? 0 : 1 
+0

通過這段代碼,我得到以下錯誤信息:'腳本不允許使用staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods readLines java.io.File'。我錯過了什麼? – Yash

+0

似乎你是在詹金斯......我發現這個問題:https://issues.jenkins-ci.org/browse/JENKINS-35681 /似乎有一個你可以打電話的方法列表。如果你在管道內 - 可能有一些本地管道等價物來加載文件內容...我發現這個https://github.com/jenkinsci/pipeline-examples/blob/master/docs/BEST_PRACTICES.md – daggett

+0

謝謝@ daggett的參考! – Yash

1

你可以試試這個。

if (new File("/path/to/the/file/xyz.html").text?.contains("jira.bugtracker.com")){ 
    println (" the changes are present\n"); 
} else { 
    println (" the changes are not present\n"); 
} 
+0

謝謝@sfgroups。它爲我工作。 – Yash

相關問題