gpt4 book ai didi

Jenkins GMavenPlus 插件无法在 groovy 脚本中导入本地类

转载 作者:行者123 更新时间:2023-12-04 18:00:12 54 4
gpt4 key购买 nike

我有一个 Jenkins 作业调用一个调用 groovy 脚本的 maven 构建文件。

在 Jenkins 我有:

Maven version 3.0
Goals and options: -U -P hudson gplus:execute

使用 GMavenPlus 调用 Groovy 脚本.在 pom.xml 我有

<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<scripts>
<script>
file:///${project.basedir}/src/main/java/com/mycompany/testImport.groovy
</script>
</scripts>
</configuration>
</plugin>

正在调用 testImport.groovy 脚本:

println "Hello from testImport"
importedClass = new ImportedClass()
importedClass.hello()

这个脚本试图包含另一个 groovy 脚本,ImportedClass.groovy,它只有一个方法:

class ImportedClass {
def hello() {
println( "Hello from imported class" )
}

testImport 脚本被正确调用并且我已经全部正常工作,但是在尝试对 importedClass 使用导入时似乎出现了问题。

我在 Jenkins 控制台中出现了这个错误

[ERROR] Failed to execute goal org.codehaus.gmavenplus:gmavenplus-plugin:1.5:execute (default-cli) on project com.mycompany: Error occurred while calling a method on a Groovy class from classpath. InvocationTargetException: startup failed:
[ERROR] Script1.groovy: 3: unable to resolve class ImportedClass
[ERROR] @ line 3, column 21.
[ERROR] def importedClass = new ImportedClass()
[ERROR] ^
[ERROR]
[ERROR] 1 error
[ERROR] -> [Help 1]

我尝试设置包名称并使用 evaluate 但总是以该错误告终。有没有办法包含外部 groovy 文件?

通过在 pom.xml 中使用它,我设法让外部依赖项工作:

<dependencies>
<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<version>0.7</version>
</dependency>

然后我可以在 groovy 代码中使用:

import groovyx.net.http.HTTPBuilder
// and create instance of the class
def httpBuilder = new HTTPBuilder("blablabla")

最佳答案

在 GitHub 中关注此问题 https://github.com/groovy/GMavenPlus/issues/53

并实现了使用的建议:

def myDependentScript = new GroovyClassLoader().parseClass(new File("myScriptDependency.groovy")).newInstance()

它不像使用简单的导入那样漂亮干净,但仍然可以工作

关于Jenkins GMavenPlus 插件无法在 groovy 脚本中导入本地类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36471417/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com