3

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

在詹金斯我有:

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

Groovy 脚本是使用GMavenPlus调用的。在 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 脚本,并且我一切正常,但是在尝试对 importClass 使用导入时似乎存在问题。

我在 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]

我尝试设置包名称并使用评估但总是以该错误结束。有没有办法包含一个外部 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")
4

1 回答 1

1

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

并实施了使用的建议:

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

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

于 2016-04-11T14:47:20.550 回答