I am trying to invoke Groovy inside Hudson (using groovy plugin) to get some properties for our build. But I am getting this exception:
groovy.lang.MissingPropertyException: No such property: manager for class: Script1
I get this with the following line:
def buildNUmber = manager.build.number
This happens when I run as an inline command within Jenkins as well as using a script:
I tried the solution below, but it fails during the declaration itself (line two):
Binding binding = new Binding();
binding.setVariable("manager", manager);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(new File("d:/dev/others/hudson/userContent/ScriptStuff.groovy").text);
The above is run using: Groovy command. And when I run the build it errors and complains about the line - binding.setVariable("manager", manager);
When I use the Groovy script file, then it complains about:
def buildNumber = manager.build.number
Both errors are :
groovy.lang.MissingPropertyException: No such property: manager for class: Script1
Tried everything mentioned in this thread as well:
I am using Hudson 2.2.1 and Groovy 2.1.3. What could be wrong?
manager is provided by certain Groovy script plugins, but not all. To make your script generic, use the Jenkins/Hudson API instead:
import hudson.model.*
def build = Thread.currentThread().executable
def buildNumber = build.number
...
Just in case it helps, if you are using the 'Execute System Groovy Script', you don't need to use the 'manager' variable. This worked for me -
def workspace = build.getEnvVars()["WORKSPACE"]
One of the reasons groovy.lang.MissingPropertyException: is thrown when you are using a variable outside of its scope or you haven't defined that variable.
Maybe I'm missing some part of your code, but where do you define the manager? If that's the complete Groovy script, you're trying to bind a variable which isn't declared anything, so it isn't surprising that it fails.
Just define a manager it that's what you want, like:
def manager = "my manager" // probably not what you want
This should solve your current error.
Related
We are trying to use some custom helper functions from a .jar library in our Jenkinsfile. To achieve this, we want to use the #Grab annotation from groovy/grape. Our Jenkinsfile looks like this:
#Grab('com.company:jenkins-utils:1.0')
import com.company.jenkinsutils.SomeClass
pipeline {
...
}
When trying to run the pipeline, we get the following error message:
java.lang.RuntimeException: No suitable ClassLoader found for grab
I already tried specifying #GrabConfig(systemClassLoader = true), however to no success. I suppose is has to do with the pipeline scripts running in the sandbox mode? Is there any way to make this work?
I'm trying to use a YAML file as a standard-in to teamcity to pass build parameters. (user will copy the yaml file as stdin) .
which build runner should i use to achieve this. I can find command line as a build runner.but it simply use arguments to a script ? ( i can do the same) but is there any way that i can copy configuration details to teamcity without an argument to a script ?
Jetbrains seems to prefere not do go with yaml. But you can still give XML or Kotlin DSL a try:
You can store project settings in the XML format or in the Kotlin language and define settings programmatically using the Kotlin-based DSL. Kotlin DSL
import jetbrains.buildServer.configs.kotlin.v2019_2.*
import jetbrains.buildServer.configs.kotlin.v2019_2.buildSteps.script
version = "2021.1"
project {
buildType {
id("HelloWorld")
name = "Hello world"
steps {
script {
scriptContent = "echo 'Hello world!'"
}
}
}
}
I’m trying to use a Java, Serenity-BDD project with gradle version 4.8+, but the application is not pulling the CLI arguments of -Denvironment and -Dservicebranches. I have these properties as blank values in my local.properties file, and they’re not getting assigned when my app runs.
./gradlew --build-cache build -Dwebdriver.remote.url=${SELENIUM_REMOTE_URL} -Denvironment=${ENVIRONMENT} -Dservicebranches=${SERVICE_BRANCHES} -Dtags=${TAGS}
I have a local.properties file with properties that are being successfully dependency injected into the project (through Serenity-Spring). I'm hoping that these CLI arguments will override these values:
servicebranches=
environment=local
But right now, anything specified in the CLI arguments are not being passed into the project. Either through DI, or through explicitly grabbing the environment variables in the build.gradle, which what I've tried hasn't been working.
Here's a few things which I have tried in the build.gradle:
//task integrationTests() {
// doFirst
// {
// def environment = System.getProperty('environment')
// def servicebranches = System.getProperty('servicebranches')
// }
// tasks.build.execute()
//}
//integrationTests.dependsOn(build)
//build.doFirst{
// systemProperties System.properties
// def environment = System.properties['environment']
// environment = environment //This actually flags with 'Silly assignment'
//}
build.doFirst{
def environment = System.getProperty('environment')
def servicebranches = System.getProperty('servicebranches')
}
The latest one seems to still be missing a step, because the program is still working, but the args are still not getting through. I've even tried -Denvironment=potato, and no errors have come up because I do not have a property or properties file named that.
I've also tried using the -P tag instead of -D tag, but that doesn't seem to be working either.
All I’m trying to do is use build.gradle to use System.getProperty(‘environment’) and System.getProperty(‘servicebranches’) before I use the already created ‘build’ task that comes with Serenity. How would I do this? Do I build a whole new task, where I use these getProperties, and then call the build task? Do I have to specify the assignment of these same named variables in the local.properties file?
-D is for system properties in Gradle. Try with -P instead (https://docs.gradle.org/current/userguide/build_environment.html#sec:project_properties)
I know this is a very old question but here's what I did to solve my problem, I got the idea from here: https://github.com/serenity-bdd/serenity-documentation/pull/120/files
Serenity was not pulling the environment from gradle to use EnvironmentSpecificProperties, it kept saying "undefined property for environment 'null" when I removed the default environment. I had to add this to my Gradle file:
test {
systemProperty 'environment', System.properties['environment']
}
I want to run the SOAPUI project xmls using Gradle script. The GRADLE script should read the project xmls from soapuiInputs.properties file and run automatically all. Please guide me step by step how to create Gradle script to run the SOAPUI projects in Linux server.
Note: We use SOAPUI version 5.1.2.
Probably the simple way is to call the SOAPUI testrunner directly from gradle as Exec task, like you can do from cli.
In gradle you can define the follow tasks (Note that I try it on windows but to do the same on linux as you ask simply you've to change the paths):
// define exec path
class SoapUITask extends Exec {
String soapUIExecutable = 'C:/some_path/SoapUI-5.2.1/bin/testrunner.bat'
String soapUIArgs = ''
public SoapUITask(){
super()
this.setExecutable(soapUIExecutable)
}
public void setSoapUIArgs(String soapUIArgs) {
this.args = "$soapUIArgs".trim().split(" ") as List
}
}
// execute SOAPUI
task executeSOAPUI(type: SoapUITask){
// simply pass the project path as argument,
// note that the extra " are needed
soapUIArgs = '"C:/location/of/project.xml"'
}
To run this task use gradle executeSOAPUI.
This task simply runs a SOAPUI project, however testrunner supports more parameters which you can pass to soapUIArgs string in executeSOAPUI task, take a look here.
Instead of this if you want to deal with more complex testing there is a gradle plugin to launch SOAPUI project, take a look on it here
Hope this helps,
I admit I am quite new to gradle but I did not expect to be unable to understand something as simple as the example below. I can read the gradle documentation about checking whether a project property have been set or not using a hasProperty(String propertyName) call and I am sitting here and have no idea why something so basic does not work.
I believe my mind must be so much "ant like" oriented that for sure I am missing something ordinary basic
task printSystem() << {
println system
println "has property: " + hasProperty("system")
}
and invoking that task with the command below:
$gradle printSystem -Psystem=mySystem
mySystem
has property: null
So my questions would be:
Why system is printed out but hasProperty returns null?
How should I check for the existence of the project property called "system"?
Is there a different way for testing for a project property as opposed to a system property?
How would you pass a system property from the command line?
This is from, the gradle documentation and I believe I am reading it right
19.2.1. Checking for project properties
You can access a project property in your build script simply by using its name as you would use a variable. If this property does not exist, an exception will be thrown and the build will fail. If your build script relies on optional properties the user might set, perhaps in a gradle.properties file, you need to check for existence before you access them. You can do this by using the method hasProperty('propertyName') which returns true or false.
You need to explicitly invoke hasProperty on the project instance - without it, hasProperty is invoked on some local context. The following example works:
task printSystem() << {
println system
println "has property: " + project.hasProperty("system")
}
Because non-existing properties (system is not defined in the script) are taken from the project instance. If you won't pass the system property, an exception will be thrown on println.
project.hasProperty('propName')
Not sure if I understood right, but you can access project properties via the project instance and system properties via the System class.
Using -D switch - gradle -Dprop=value