unable to resolve class MarkupBuilder @ line 68, column 5. new MarkupBuilder(out).with {
时间: 2024-03-04 19:47:51 浏览: 104
抱歉,这是因为缺少对 `groovy.xml.MarkupBuilder` 类的引用。请在脚本的开头添加以下语句:
```groovy
import groovy.xml.MarkupBuilder
```
这样就可以正确解析 `MarkupBuilder` 类了。完整的脚本应该是这样的:
```groovy
@Grab(group='com.googlecode.java-diff-utils', module='diffutils', version='1.3.0')
import difflib.*
import java.nio.file.*
import java.nio.charset.*
import groovy.xml.MarkupBuilder
// 定义要比较的两个文件夹路径
def folder1 = Paths.get("path/to/folder1")
def folder2 = Paths.get("path/to/folder2")
// 获取两个文件夹下的所有文件
def files1 = Files.walk(folder1).filter{Files.isRegularFile(it)}.toList()
def files2 = Files.walk(folder2).filter{Files.isRegularFile(it)}.toList()
// 定义 HTML 文件的模板
def htmlTemplate = """
<html>
<head>
<title>${"DIFF: " + file1.getFileName()}</title>
<style>
.added { background-color: #aaffaa; }
.deleted { background-color: #ffaaaa; }
.changed { background-color: #ffffaa; }
</style>
</head>
<body>
<h1>${"DIFF: " + file1.getFileName()}</h1>
<table>
<tr>
<th>${folder1.toString()}</th>
<th>${folder2.toString()}</th>
</tr>
<%
for (diff in diffs) {
if (diff.operation == Operation.DELETE) {
out << "<tr><td class='deleted'>${diff.text}</td><td></td></tr>"
} else if (diff.operation == Operation.INSERT) {
out << "<tr><td></td><td class='added'>${diff.text}</td></tr>"
} else if (diff.operation == Operation.CHANGE) {
out << "<tr><td class='deleted'>${diff.text1}</td><td class='added'>${diff.text2}</td></tr>"
} else {
out << "<tr><td>${diff.text}</td><td>${diff.text}</td></tr>"
}
}
%>
</table>
</body>
</html>
"""
// 遍历两个文件夹下的所有文件,进行比较,并将结果写入 HTML 文件
(files1 + files2).unique().each { file ->
def relativePath = folder1.relativize(file)
def file1 = folder1.resolve(relativePath)
def file2 = folder2.resolve(relativePath)
def diffs = []
if (Files.exists(file1) && Files.exists(file2)) {
// 两个文件都存在,进行比较
def lines1 = Files.readAllLines(file1, StandardCharsets.UTF_8)
def lines2 = Files.readAllLines(file2, StandardCharsets.UTF_8)
diffs = new DiffRowGenerator.Builder().showInlineDiffs(true).build().generateDiffRows(lines1, lines2)
} else {
// 只有一个文件存在,输出提示信息
diffs = [new DiffRow(Operation.INSERT, "[ERROR] File not found: ${file1.toAbsolutePath()}"), new DiffRow(Operation.DELETE, "[ERROR] File not found: ${file2.toAbsolutePath()}")]
}
// 生成 HTML 文件并写入比较结果
def htmlContent = new StringWriter().with { out ->
new MarkupBuilder(out).with {
mkp.xml()
mkp.template(htmlTemplate).with {
setProperty('file1', file1.getFileName())
setProperty('file2', file2.getFileName())
setProperty('diffs', diffs)
}
}
return out.toString()
}
def htmlFile = new File("path/to/output/${relativePath}.html")
htmlFile.parentFile.mkdirs()
htmlFile.write(htmlContent)
}
```
阅读全文