Maven原型在执行Groovy脚本时增加错误

发布于 2025-02-03 04:05:04 字数 1112 浏览 3 评论 0原文

我制作了一个自定义的Maven原型,并基于,添加了文件原型post-generate.groovy src/main/resours/resources/meta-inf folder。
在此Groovy脚本中,我想连接到数据库并根据数据采取一些操作。
脚本如下:

import groovy.sql.Sql

Sql.withInstance(
        'jdbc:sqlserver://localhost:1433;databaseName=db;',
        'sa',
        'pass',
        'com.microsoft.sqlserver.jdbc.SQLServerDriver') { sql ->
    List rows = sql.rows('SELECT top 5 id, description FROM Project')
    for(row in rows) {
        println row.getProperty("id")
        println row.getProperty("description")
        println "/*************\\"
    }
}

安装原型后,当我与该模块生成模块时,一旦执行脚本,就会提出以下错误:

Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.0:generate (default-cli) on project standalone-pom: startup failed:
Script1.groovy: 1: unable to resolve class groovy.sql.Sql
 @ line 1, column 1.
   import groovy.sql.Sql
   ^

1 error

是否有解决此问题的解决方案?

I made a custom maven Archetype and based on the Maven Documentation, added the file archetype-post-generate.groovy to the src/main/resources/META-INF folder.
In this groovy script I want to connect to a database and take some actions according to the data.
The script is as follow:

import groovy.sql.Sql

Sql.withInstance(
        'jdbc:sqlserver://localhost:1433;databaseName=db;',
        'sa',
        'pass',
        'com.microsoft.sqlserver.jdbc.SQLServerDriver') { sql ->
    List rows = sql.rows('SELECT top 5 id, description FROM Project')
    for(row in rows) {
        println row.getProperty("id")
        println row.getProperty("description")
        println "/*************\\"
    }
}

After installing the archetype when I generate a module with it, once the script is executed, the following error is raised:

Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.0:generate (default-cli) on project standalone-pom: startup failed:
Script1.groovy: 1: unable to resolve class groovy.sql.Sql
 @ line 1, column 1.
   import groovy.sql.Sql
   ^

1 error

Is there any solution to resolve this problem?

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

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

发布评论

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

评论(1

妄司 2025-02-10 04:05:04

根据 @emilles的指导和 grovy文档使用@grab注释。
例如:

@Grab('org.codehaus.groovy:groovy-sql:2.4.16')
@Grab('com.microsoft.sqlserver:mssql-jdbc:8.4.1.jre8')

According to @emilles's guidance and Groovy documentation, we can add any library to the classpath of script using the @Grab annotation.
For example:

@Grab('org.codehaus.groovy:groovy-sql:2.4.16')
@Grab('com.microsoft.sqlserver:mssql-jdbc:8.4.1.jre8')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文