我有一个 Java 应用程序,可以针对 XQuery (*.xqy) 源文件生成 xqDoc(类似于 JavaDoc)。
我有一个 Maven 项目:https://github.com/lcahlander/xqdoc-core.git
我想对 src/main/ml-modules/root/**/*.xqy
中的所有 .xqy
文件运行以下 java 代码并放置结果分别在xqDoc/**/*.xml
中:
HashMap uriMap = new HashMap();
uriMap.put(XPathDriver.XPATH_PREFIX, XPathDriver.XPATH_URI);
InputStream is = Files.newInputStream(Paths.get(cmd.getOptionValue("f")));
controller = new XQDocController(XQDocController.JUL2017);
controller.setPredefinedFunctionNamespaces(uriMap);
XQDocPayload payload = controller.process(is, "");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource isOut = new InputSource();
isOut.setCharacterStream(new StringReader(payload.getXQDocXML()));
Document doc = db.parse(isOut);
xqDoc 解析器也可以从命令行运行
java -jar xqdoc-core-0.8-jar-with-dependency.jar -Dfn=http://www.w3.org/2003/05/xpath-functions -Dxdmp=http://marklogic .com/xdmp -f 文件路径
我想创建 gradle 任务 generateXQDoc
最佳答案
像这样的东西应该可以工作(未经测试)。您可以调整硬编码路径以使用项目属性,但应该足以演示如何迭代文件集中的每个文件并执行
task generateXQDoc {
description = 'Generate XQDocs'
doLast {
def sourceDir = 'src/main/ml-modules'
File targetDir = new File('xqDoc')
HashMap uriMap = new HashMap();
uriMap.put(XPathDriver.XPATH_PREFIX, XPathDriver.XPATH_URI);
controller = new XQDocController(XQDocController.JUL2017);
controller.setPredefinedFunctionNamespaces(uriMap);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
def xqueryFiles = fileTree(dir: sourceDir, include: '**/*.xq*')
xqueryFiles.each { file ->
InputStream is = Files.newInputStream(file));
XQDocPayload payload = controller.process(is, "");
String relativePath = new File(sourceDir).toURI().relativize(file.toURI()).getPath();
File outputFile = new File(targetDir, relativePath)
outputFile.parentFile.mkdirs()
outputFile.write(payload.getXQDocXML())
}
}
}
关于java - 如何针对 gradle 项目中某种类型的所有文件运行 java 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239147/