3

I have a Jenkins job that call a maven build file that calls a groovy script.

In Jenkins I have:

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

The Groovy script is called using the GMavenPlus. In the pom.xml I have

<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>

Which is calling the testImport.groovy script:

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

This script tries to include another groovy script, ImportedClass.groovy which has a single method:

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

}

The testImport script is correctly called and I have all working, but there seems an issue while trying to use an import for the importedClass.

I have this error appearing in the Jenkins console

[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]

I tried to setup packages names and use also evaluate but always ending up with that error. Is there a way to include an external groovy file ?

I managed to have external dependencies working by using this in the pom.xml:

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

Then I can use in the groovy code:

import groovyx.net.http.HTTPBuilder
// and create instance of the class
def httpBuilder = new HTTPBuilder("blablabla")
dawez
  • 2,714
  • 2
  • 29
  • 50

1 Answers1

1

Followed this Issue in GitHub https://github.com/groovy/GMavenPlus/issues/53

and implemented the suggestion given to use:

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

It is not beautifully clean as using a simple import but nonetheless working

dawez
  • 2,714
  • 2
  • 29
  • 50