【问题标题】:Jenkins GMavenPlus plugin cannot import local class in groovy scriptJenkins GMavenPlus 插件无法在 groovy 脚本中导入本地类
【发布时间】:2017-06-25 03:14:27
【问题描述】:

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

在詹金斯我有:

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 脚本,并且我已经完成了所有工作,但是在尝试对 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]

我尝试设置包名称并使用 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")

【问题讨论】:

    标签: jenkins groovy gmaven-plugin


    【解决方案1】:

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

    并实施了使用的建议:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-08
      • 2014-09-03
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多