Using @Grab to retrieve Groovy dependencies...

1,487 views
Skip to first unread message

Jeff

unread,
Aug 24, 2015, 2:29:34 PM8/24/15
to jenkins...@googlegroups.com
I'm trying to write a Groovy script that will query our Sonatype Nexus OSS Maven repository for information about a given artifact (war file) that was just deployed as part of a build.  I then want to set Jenkins job parameters so I can use them in subsequent steps in the job.

My script is currently using the groovyx.net.http.RESTClient and works in stand-alone mode see script below, but when I run it as a Groovy System Script it throws the following:

FATAL: org/apache/ivy/core/settings/IvySettings
java.lang.NoClassDefFoundError: org/apache/ivy/core/settings/IvySettings

If I remove the @GrabResolver, @Grapes and @Grab directives, it throws this error:

ERROR: Build step failed with exception
Script1.groovy: 2: unable to resolve class groovyx.net.http.RESTClient
 @ line 2, column 1.
   import groovyx.net.http.RESTClient
   ^

How do I specify a dependency for a Groovy script so I can import and use a given class?  Thanks!

##Script:
@GrabResolver(name='myrepo', root='http://nexus.mycompany.com/nexus/content/groups/myrepo/', m2Compatible='true')
@Grapes([
        @Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
    ])

import groovyx.net.http.RESTClient
import hudson.model.*
     
try {
    println "Get w/ path"
    def resp = client.get( 
        query : ['r': 'myrepo', 'g': 'mygroupid', 'a': 'myartifactid', 'v': '1.0-SNAPSHOT', 'e': 'jar'],
        headers: ['Accept': 'application/json', "Content-Type": 'application/json']
    )
        
    def json = resp.getData()
    
    def version = json.data.version
    def url = json.data.repositoryPath
    def sha1 = json.data.sha1
    
    printf( "Found: version: '%s'  url: '%s'  sha1: '%s'", version, url, sha1)
    
} catch (Exception ex) {
    println ex.getMessage()
}

--
Jeff Vincent
See my LinkedIn profile at:
http://www.linkedin.com/in/rjeffreyvincent

Joel Young

unread,
Sep 17, 2015, 4:19:27 PM9/17/15
to Jenkins Users
I ran into the same problem.  I assume you're trying to do it in a System Groovy Script.  If so, that's the problem.  The System Groovy Scripts run under the version of Groovy built into Jenkins.  In my case, it's 1.8.5.  You'll probably need to run this in a non-system Groovy Script.  Then you get into the situation that I'm still trying to resolve, which is getting the build variables and environment variables accessible by the script.  You can create a parameter file that you can fill using a System Groovy script, then in the next build step access it.  However, I'm trying to create the properties file with a name and location specific to the build, and am trying to figure out how to transfer the settings from the System Groovy build step to the non-system Groovy build step

Jeff

unread,
Sep 17, 2015, 5:33:59 PM9/17/15
to jenkins...@googlegroups.com
My issue turned out was related to where I was running (Secure Development Environment) and the default Groovy / Ivy configuration that has Maven Central as the first repository by default.  It would always fail because our SDE has no external access.

Being new to Groovy, I had no idea this file existed.  Once I updated groovyConfig.xml to ONLY have our internally accessible repository and it works.

Good luck!

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/79330dce-15bf-4a92-85ee-f1e6d2dcf4f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages