Grails 4 如何在自定义命令中获取工件的句柄

发布于 2025-01-10 14:37:21 字数 705 浏览 0 评论 0 原文

我需要在 Grails 4 应用程序中构建自定义命令 (https:/ /docs.grails.org/4.0.11/guide/single.html#creatingCustomCommands),我需要获取一些 Grails 服务和域类的句柄,我将根据需要查询这些类。

自定义命令框架非常简单:

import grails.dev.commands.*
import org.apache.maven.artifact.Artifact

class HelloWorldCommand implements GrailsApplicationCommand {

    boolean handle() {
        return true
    }
}

虽然文档说自定义命令可以访问整个应用程序上下文,但我还没有找到任何有关如何获取该句柄并开始访问各种应用程序工件的示例。

有什么提示吗?

编辑:添加上下文并阐明自定义命令的目标,以便进一步推荐/最佳实践/等:该命令以自定义格式从文件中读取数据,保留数据,然后写入另一种自定义格式的报告。 一旦第三方 REST API 按需提供数据,最终将被周期性作业取代。

I need to build a custom command in a Grails 4 application (https://docs.grails.org/4.0.11/guide/single.html#creatingCustomCommands), and I need to get an handle to some Grails Services and Domain classes which I will query as needed.

The custom command skeleton is quite simple:

import grails.dev.commands.*
import org.apache.maven.artifact.Artifact

class HelloWorldCommand implements GrailsApplicationCommand {

    boolean handle() {
        return true
    }
}

While the documentation says that a custom command has access to the whole application context, I haven't found any examples on how to get an handle of that and start accessing the various application artifacts.

Any hints?

EDIT: to add context and clarify the goal of the custom command in order for further recommendation/best practices/etc.: the command reads data from a file in a custom format, persist the data, and writes reports in another custom format.
Will eventually be replaced by a recurrent job, once the data will be available on demand from a third party REST API.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2025-01-17 14:37:21

请参阅 github.com/jeffbrown/marco-vittorini-orgeas-artifacts- 的项目cli

grails-app/services/marco/vittorini/orgeas/artifacts/cli/GreetingService.groovy

package marco.vittorini.orgeas.artifacts.cli

class GreetingService {
    String greeting = 'Hello World'
}

grails-app/commands/marco/vittorini/orgeas/artifacts/cli/HelloCommand.groovy

package marco.vittorini.orgeas.artifacts.cli


import grails.dev.commands.*

class HelloCommand implements GrailsApplicationCommand {
    GreetingService greetingService
    boolean handle() {

        println greetingService.greeting

        return true
    }
}

编辑:

我在 github.com/jeffbrown/marco-vittorini-orgeas-artifacts-cli/commit/49a846e3902073f8ea0539fcde550f6d002b9d89 它演示了访问域类,这是我在编写初始答案时忽略的问题的一部分。

See the project at github.com/jeffbrown/marco-vittorini-orgeas-artifacts-cli.

grails-app/services/marco/vittorini/orgeas/artifacts/cli/GreetingService.groovy

package marco.vittorini.orgeas.artifacts.cli

class GreetingService {
    String greeting = 'Hello World'
}

grails-app/commands/marco/vittorini/orgeas/artifacts/cli/HelloCommand.groovy

package marco.vittorini.orgeas.artifacts.cli


import grails.dev.commands.*

class HelloCommand implements GrailsApplicationCommand {
    GreetingService greetingService
    boolean handle() {

        println greetingService.greeting

        return true
    }
}

EDIT:

I have added a commit at github.com/jeffbrown/marco-vittorini-orgeas-artifacts-cli/commit/49a846e3902073f8ea0539fcde550f6d002b9d89 which demonstrates accessing a domain class, which was part of the question I overlooked when writing the initial answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文