我有一个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脚本是正确调用的,我已经开始工作了,但是在尝试为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]我试着设置包名并使用,计算,但最终总是出现错误。是否有一种方法可以包含外部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")发布于 2016-04-11 14:47:20
在“GitHub https://github.com/groovy/GMavenPlus/issues/53”中跟踪此问题
并执行了提出的使用建议:
def myDependentScript = new GroovyClassLoader().parseClass(new File("myScriptDependency.groovy")).newInstance()它并不像使用简单的导入一样干净,但是仍然可以工作。
https://stackoverflow.com/questions/36471417
复制相似问题