java - 如何针对 gradle 项目中某种类型的所有文件运行 java 方法?

标签 java maven gradle build.gradle xquery

我有一个 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/

相关文章:

gradle - 如何在不修改 settings.gradle 的情况下添加 gradle 项目依赖项

android - 更新到 sdk28 后构建项目的问题

java - 在变量中包含千瓦时/年的最佳方法是什么

java - 如何阻止kafka消费者中的拉取请求

JavaFX 事件处理程序与更改监听器术语

java - 如何通过依赖未声明其依赖项的项目一次过渡到 gradle/maven 一个项目

android - 为什么 CardView 和 RecyclerView 需要 minSdkVersion L?

java将单个数字流式传输到一个范围

java - 无法启动 osgi 包,因为无法解析导入

java - 如何在现有项目中配置Maven?