【问题标题】:How to invoke external command in gradle for Spring Boot project and eclipse IDE?如何在 gradle 中为 Spring Boot 项目和 Eclipse IDE 调用外部命令?
【发布时间】:2018-05-02 18:01:10
【问题描述】:

在我的 Spring Boot 2.0 gradle 项目中,每次修改 bar.tmpl.html 并将其保存在 eclipse IDE 中时,我都需要调用以下命令:

handlebars src/main/resources/static/templates/bar.tmpl.html -f src/main/resources/static/js/bar.tmpl.js

此命令在 spring 项目目录根目录的 shell 中运行时有效。

我想在使用 devtools 在 STS eclipse IDE 中进行开发时自动执行此过程。每次对车把模板文件进行编辑时,都应自动重新构建运行上述命令的项目。

如何做到这一点?

【问题讨论】:

    标签: spring-boot gradle handlebars.js


    【解决方案1】:

    您可以编写自定义exec 类型的任务来运行您的命令并使其执行continuous。或者,您可以将任务挂钩到生命周期中的某个点,例如 processResources 任务。

    执行类型任务以运行命令:

    task updateHandelbar(type: Exec) {
        inputs.files "${projectDir}/src/main/resources/static/templates/bar.tmpl.html"
        outputs.files "${projectDir}/src/main/resources/static/templates/bar.tmpl.html"
        commandLine 'cmd', '/c', 'handlebars src/main/resources/static/templates/bar.tmpl.html -f src/main/resources/static/js/bar.tmpl.js'
    }
    

    将任务挂钩到生命周期中的任何点:

    语法:<someTask>.shoudlRunAfter(<anotherTask>)
    示例:processResources.shouldRunAfter(updateHandelbar)

    连续运行任务并等待文件更改:

    这将连续执行任务。表示当输入文件更改时它会重新运行任务:

    gradlew <someTask> -continuous
    

    每个代码 sn-p 的组合可能会让您获得预期的行为。

    【讨论】:

    • UnlikePluto 我听从了你的回答,但它不起作用。命令把手永远不会运行。此外,即使这有效,eclipse IDE 也不会看到生成的文件,除非我在 eclipse 中手动刷新 eclipse 项目,因为对 eclipse 之外的项目文件所做的任何更改,eclipse 都不会看到它。
    • 你如何运行你的应用程序?
    • Gradle 与任何 IDE 或刷新 IDE 的资源无关。它完全独立于任何平台运行。如果您依赖 Eclipse 构建链而不使用 Gradle,即 gradle 包装器来运行您的应用程序,则不会调用任何任务。
    • 我在 STS eclipse 中运行我的应用程序。但是为了测试您的解决方案,我按照您的建议运行 ./gradlew processResources -continuous
    • 声明 processResources.shouldRunAfter(updateHandelbar) 旨在作为一个示例,其中包含将任务挂钩到项目生命周期中适合的点的注释。如果您的生命周期没有使用 processResources 任务,它将永远不会被调用。要测试 updateHandelbar 任务,您必须运行 gradlew updateHandelbar。我仍然不知道您如何运行您的应用程序。你在eclipse中有运行配置吗?你在命令行上使用gradlew 吗?您正在运行什么 gradle 任务来执行您的应用程序?
    猜你喜欢
    • 2023-04-01
    • 2018-09-29
    • 2018-03-05
    • 2017-11-03
    • 2018-07-24
    • 2021-11-01
    • 2020-09-25
    • 1970-01-01
    • 2019-02-10
    相关资源
    最近更新 更多