How to add a jar for Groovy plugin in Jenkins? - jenkins

I need to use some classes in httpclient.jar when building a project in Jenkins. I added this jar to both {Groovy_Classpath}\lib and {Jenkins_Path}\plugins\groovy\WEB-INF\lib and tried to imported into my job (also see the screenshot below):
import jenkins.model.*
import hudson.model.*
import groovy.*
import org.apache.http.*
HttpClient httpClient = HttpClientBuilder.create().build();
It has two issues:
startup failed:
Script1.groovy: 6: unable to resolve class HttpClient
# line 6, column 12.
HttpClient httpClient = HttpClientBuilder.create().build();
^
1 error
So I removed HttpClient and saw another:
Caught: groovy.lang.MissingPropertyException: No such property: HttpClientBuilder for class: xxxx
groovy.lang.MissingPropertyException: No such property: HttpClientBuilder
I guess probably they meant the same thing - the class couldn't be found. But I have no idea how to resolve as I've added the jar to classpath...
Updated on the basis of Mike W's suggesion

Try getting the dependency using Grape, add the following to the top of your script.
#Grab(group='org.apache.httpcomponents', module='httpclient', version='4.5.3'))

Related

Getting unable to resolve class org.apache.commons.configuration.PropertiesConfiguration

Iam new to groovy on grails.
In my grails application i have to edit a property file without deleting other key values for that Iam using apache commons following code
PropertiesConfiguration conf = new PropertiesConfiguration("config.properties");
props.setProperty("key", "value");
conf.save();
In BuildConfig.groovy I have included the depentencies
dependencies {
compile 'org.apache.commons:commons-configuration2:2.2'
compile 'org.apache.commons:commons-lang3:3.1'
}
While executing grails compile the dependencies seems to be downloaded without any error, but that Iam getting the compilation error
unable to resolve class org.apache.commons.configuration.PropertiesConfiguration
[groovyc] # line 5, column 1.
[groovyc] import org.apache.commons.configuration.PropertiesConfiguration;
[groovyc] ^
[groovyc]
What additional settings that i need to do in grails please suggest
Use
import org.apache.commons.configuration2.PropertiesConfiguration;
Note the 2 at the end of configuration!

Upgrading Grails from v3.0 to v3.1 or v3.2

I'm in process of upgrading an app from Grails v2 to v3 .
I got to a point where it works reasonably well with Grails 3.0.17.
I wanted to upgrade it to v3.1 or v3.2. But after changing the version in gradle.properties that's what I get after executing grails run-app:
me#host:[~/](feature/grails3-migration) : grails run-app
| Error Error occurred running Grails CLI: startup failed:
script14867378818131525390840.groovy: 3: unable to resolve class xxx.yyy.CollectorsJob
# line 3, column 1.
import xxx.yyy.jobs.CollectorsJob
^
script14867378818131525390840.groovy: 6: unable to resolve class xxx.yyy.domain.business.Company
# line 6, column 1.
import xxx.yyy.domain.business.Company
^
script14867378818131525390840.groovy: 5: unable to resolve class xxx.yyy.domain.access.User
# line 5, column 1.
import xxx.yyy.domain.access.User
^
...
There's about 15 errors like that printed.
What's the problem here? I've tried clear the project and change the jdk version fro 1.8 -> 1.7. Each time the same result.
If you are referencing application classes in grails-app/conf/application.groovy these will need to be moved to your runtime configuration in grails-app/conf/runtime.groovy
The application.groovy file is parsed by the build system in addition to the runtime so cannot contain references to classes that are not yet compiled

Unable to compile groovy script in Jenkins

The script is working fine in groovy console. But when I do check-syntax for the same script in Jenkins, the following error message is coming up -
Script1.groovy: 6: unable to resolve class groovyx.net.http.RESTClient
# line 6, column 1.
import groovyx.net.http.RESTClient
^
Script1.groovy: 4: unable to resolve class groovyx.net.http.ContentType
# line 4, column 1.
import groovyx.net.http.ContentType
^
Script1.groovy: 3: unable to resolve class groovyx.net.http.HTTPBuilder
# line 3, column 1.
import groovyx.net.http.HTTPBuilder....
How to get this issue resolved?
This exception is because you dont have these dependencies (Jars) that has these classes so you have two option :
1- if you currently using any dependency management framework like (maven,gradle) then just add these dependencies
2- in the groovy file at the top add #Grapes and then add the dependency here an example :
#Grapes(
#Grab(group='yourDependencyGroupID', module='yourDependencyArtifactID'
, version='theDesireVersion')
)
you can search for these dependencies in Maven Repository
i hope this will help :)

Groovy classpath not set up properly by <groovy> Ant task

My Groovy script depends on some libraries. This is what I have at the top of my script.
#Grapes([
#Grab(group = 'net.sf.json-lib', module = 'json-lib', version = '2.3',
classifier = 'jdk15'),
#Grab(group = 'org.codehaus.groovy.modules.http-builder',
module = 'http-builder', version = '0.7.1'),
#Grab(group = 'commons-cli', module = 'commons-cli', version = '1.2')])
When I run the script from command line using groovy executable, everything works properly. The artefacts get downloaded and the script runs.
However, if I try to execute the same script from Apache Ant using <groovy src="myscript.groovy" fork="true" /> (simplified), the artefacts also get resolved and downloaded but then I get [groovy] Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException.
The Apache Ant installation uses groovy-all-2.3.6.jar that I have copied from the Groovy installation that I have installed, so they should be pretty much identical.
What am I missing? How can I make the <groovy> task work and use the downloaded jars?
UPDATE I
The issue I believe is that Grape and Ant use different classloaders so the artifacts aren't visible to Ant. Can you try adding this: #GrabConfig(systemClassLoader = true) to your groovy script after the #Grape annotations?
If I do that I get General error during conversion: No suitable ClassLoader found for grab.
UPDATE II
I have also tried this now:
import groovy.grape.Grape;
Grape.grab(group:"commons-cli", module:"commons-cli", version:"1.2", classLoader:this.class.classLoader.rootLoader)
//...
It does not help. I get compile time error then:
[groovy] Exception in thread "main" Script Failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
[groovy] C:\Users\xxx\AppData\Local\Temp\embedded_script_in_2825216891785993632groovy_Ant_task: 18: unable to resolve class groovyx.net.http.HTTPBuilder
[groovy] # line 18, column 1.
[groovy] import groovyx.net.http.HTTPBuilder
[groovy] ^
Get rid of the #Grab and use the static .grab() method Grape offers. It allows you to specify a the rootLoader classloader which Ant can see:
import groovy.grape.Grape;
Grape.grab(group:"commons-cli", module:"commons-cli", version:"1.2", classLoader:this.class.classLoader.rootLoader)
<repeat for rest of #Grab>

Imports failed in Config.groovy file (Grails 2.2.3)

I try to run a grails app thanks to grails run-app command. Version I use is grails 2.2.3.
All imports failed and I've got the "unable to resolve class" about all import line. For example :
unable to resolve class com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator
Errors are about the following lines :
import com.octo.captcha.service.multitype.GenericManageableCaptchaService
import com.octo.captcha.engine.GenericCaptchaEngine
import com.octo.captcha.image.gimpy.GimpyFactory
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator
import com.octo.captcha.component.image.color.SingleColorGenerator
import com.octo.captcha.component.image.textpaster.NonLinearTextPaster
Here you can read these 2 files : Config.groovy and the return of "grails run-app" to help you : http://dl.free.fr/vPbCJEUnN
Thanks for your help !

Resources