Maven原型在执行Groovy脚本时增加错误
我制作了一个自定义的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 @emilles的指导和 grovy文档使用
@grab
注释。例如:
According to @emilles's guidance and Groovy documentation, we can add any library to the classpath of script using the
@Grab
annotation.For example: