Question
If an ant target uses both if and unless, which is evaluated first?
Example
What comes first, the chicken or the egg? . . .
<target name="prepare" if="chicken" unless="egg" >
<echo>Dinner time. Chicken is served!</echo>
</target>
Would ant evaluate the chicken property first? Or the egg property?
It isn't really a question of evaluation, since the properties either are or are not set before the target gets called.
EDIT: I looked at the 1.8.1 source and the logic is as follows:
if (!testIfAllows()) {
project.log(this, "Skipped because property '" + project.replaceProperties(ifCondition)
+ "' not set.", Project.MSG_VERBOSE);
return;
}
if (!testUnlessAllows()) {
project.log(this, "Skipped because property '"
+ project.replaceProperties(unlessCondition) + "' set.", Project.MSG_VERBOSE);
return;
}
So the unless won't matter unless the if passes. But keep in mind, these don't have anything to do with evaluating properties. It just looks them up to see if they are set.
Related
I have groovy application, and I am using the following code to make some json validations work:
String method = "{\n" +
"\"method\":\"filesContentReplacer\"," +
"\"filesContentReplacer\":{\n" +
"\"files\":[" +
"{" +
"\"path\":\"pom.xml\"," +
"\"target\":\"1.9.0\"," +
"\"replacement\":\"STF_SomeNumber\"" +
"}" +
"]" +
"}" +
"}"
def json = new JsonSlurper().parseText(method)
use(JsonSchema){
assert json instanceof Map
json.schema = (Map) executeReadJson([text: readFromResources('methods/filesContentReplacer.json')])
Log.instance.debug( "json.schema:${json.schema}")
assert json.conformsSchema()
}
That is pure groovy code, based from this git repo: https://github.com/alexdeleon/groovy-json-schema
The problem is when I run the code on a jenkins pipeline, it throws the following error:
expected to call org.codehaus.groovy.runtime.GroovyCategorySupport.use but wound up catching org.jenkinsci.plugins.workflow.cps.CpsClosure2.call; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/
After some research, it turns out, that the guys who made this pipeline groovy language, have not made everything they should have, and the keyword 'use' is not supported.
Now I need somehow to make it work. Can you help me?
Also, if you need any more code, I will provide. But I basically just copied the 2 classes from the git into my project.
Thank you for your time.
don't have jenkins to test but here what you could try:
1/
org.codehaus.groovy.runtime.GroovyCategorySupport.use(JsonSchema){...}
2/
#NonCPS
def myConformsSchema(String json, String schema){
...here put your code to validate json against schema
}
3/
you could try to use static methods from https://github.com/alexdeleon/groovy-json-schema/blob/master/src/main/groovy/com/lumata/os/groovy/jsonschema/JsonSchema.groovy
like this:
def schema = executeReadJson(...)
assert JsonSchema.conformsSchema(json, schema)
I am looking for a way to get a list of build and source code management section in the configuration.
For Example: I have jobs with Testcomplete and SoupUI Pro plug ins, in each jobs, in the configuration section, we input the git repo name, testsuite name, directory etc ...
Currently, I am having to go to each job, click on configuration and get the values I need, would be nice where I can get all this information for all the jobs. I looked in configuration slicing, but it does not have the section I need.
Thank you for your help in advance.
I have not used either plugin you mention, but this crude little groovy script will find every Freestyle job and if it has a gitSCM step, report the primary Git repo url, then for every "Invoke top-level Maven target", report the POM value if set.
Run from <JENKINS_URL>/script or in a Jenkins job with an "Execute System Groovy Script" (not an "Execute Groovy script").
You can modify to find your plugin's builder step and properties. You can get the values by examining <job>/config.xml instead of <job>/configure.
Updated example to specifically include also looking up TestComplete plugin (com.smartbear.jenkins.plugins.testcomplete.TcTestBuilder) values.
Similar approach left to OP for ReadyAPI Functional Testing plugin - (com.smartbear.ready.jenkins.JenkinsSoapUIProTestRunner)
WARNING: soapui-pro-functional-testing:1.6 transmits passwords in clear text. So too does SmartBear's Zephyr products. Does not inspire confidence in a company who's tagline is "quality matters more than ever.... we'll help you get there."
Jenkins.instance.allItems.findAll() {
it instanceof hudson.model.FreeStyleProject
}.each { job ->
if (job.scm instanceof hudson.plugins.git.GitSCM) {
println job.fullName + ' | ' + job.scm.userRemoteConfigs.url[0]
job.builders.findAll() {
it instanceof hudson.tasks.Maven
}.each {step ->
println ' : ' + step?.pom
}
job.builders.findAll() {
it instanceof com.smartbear.jenkins.plugins.testcomplete.TcTestBuilder
}.each {step ->
if (step?.getLaunchType() == 'lcRoutine') {
println ' : ' + step?.getProject() + ' : ' + step?.getUnit() + ' : ' + step?.getRoutine()
}
}
}
}
return
ps: I'm sure there's a cleaner way to iterate but I can't do all the work. Be sure to use the "?" to handle null values
I have my jmeter script running from a jenkins job but it is always reporting it as failed even though the script actually passed. I am using the 'Publish Performance test result report Post-build Action. Please see screenshot. What am I doing wrong? Why does it always show an error even though the actual jmeter script is passing?
You set the threshold for Unstable/Failure to 0, so even 0 consider as failure.
Increase the thresholds for Unstable and Failed
I found a way to do this! Use this plugin: https://wiki.jenkins.io/display/JENKINS/Log+Parser+Plugin
In a rules file containing this line:
error /FAILED/
In the script add a beanshell Assertion for each HTTP Request that will log messages for successes and failures. Here's one example:
String testscenario = "TEST SCENARIO: ";
String requestName = "Find Stuff";
String findStuff = "${MyStuff}";
String custName = "${CustName}";
String respData = new String(ResponseData);
if (respData.contains(findStuff))
{
log.info(testscenario+"Passed. "+requestName+" MyStuff for Cust: " +
custName + " containing " + findStuff + " returned.");
print(testscenario+"Passed. "+requestName+" MyStuff for Customer " +
custName + " containing " + findStuff + " returned.");
} else
{
log.error(testscenario+"*FAILED. "+requestName);
print(testscenario+"*FAILED. "+requestName);
}
I have a script like this:
param(
[Alias('a')]
[string]$aval,
[Alias('b')]
[switch]$bval,
[Alias('c')]
[string]$cval
)
if($aval.length -gt 1)
{
Do-Something
}
elseif($bval)
{
Do-Something-Else
}
elseif($cval.length -gt 1)
{
Do-Another-Thing
}
else
{
Do-This
}
If someone calls my script like so, an ugly error is displayed saying it is missing an argument for parameter 'aval/bval/cval':
PS C:\> .\MyScript.ps1 -a
C:\MyScript.ps1 : Missing an argument for parameter 'aval'. Specify a
parameter of type 'System.String' and try again.
At line:1 char:18
+ .\MyScript.ps1 -n <<<<
+ CategoryInfo : InvalidArgument: (:) [MyScript.ps1], ParameterBindingException
+ FullyQualifiedErrorId : MissingArgument,MyScript.ps1
Is there any way to make a cleaner, possibly one line, error appear instead? Also, is there a better way to handle parameters then a list of elseif statements (my actual script has ~10 parameters)?
The script sometimes passes an argument with a parameter as well:
EX:
PS C:\> .\MyScript.ps1 -b ServerName
Thanks for any help!
There are a few things that you can look at here. First, if the parameter will never have an associated value and you just want to know if the script was called with the parameter or not, then use a [switch] parameter instead of a string.
Here is a very simple example of using a switch parameter:
param(
[switch]$a
)
if($a){
'Switch was present'
}else{
'No switch present'
}
Save that as a script and run it with and without the -a parameter.
If you will sometimes have the parameter present with some value being passed in but other times without the value, then give the parameter a default value when you define it:
[Alias('a')]
[string]$aval = '',
Then in your logic if something was passed in, the length of the string will be gt 1.
As for the if-then structure that you have, there are a plethora of options for handling this sort of logic. with the little bit of information that you have shared, I suspect that using switch structure will be the best plan:
Get-Help about_Switch
I have a gant script A with two targets
t1 - default target
t2 - another target
Even when I run
grails A t2
the default target is run? How can I run the non-default target?
I have tried grails A --target='t2' etc. but doesn't work.
I'm not sure if there's a proper way to do it, but you can write a second script ("T2.groovy") that loads this one and sets that target as its default, e.g.
includeTargets << new File("path/to/YourScript")
setDefaultTarget("t2")
A tweak to argsParsing approach is to run through elements from the argsMap and iteratively depend on them. So you could call your script something like:
grails myScript do-this do-that do-the-other
scriptName = 'myScriptName'
includeTargets << grailsScript("_GrailsArgParsing")
snip
target(main: "Default Target") {
depends(parseArguments)
if(argsMap?.size() == 0) {
depends(scriptError)
}
argsMap.each() {
if (it.value) {
println "${scriptName} building: ${it.value}"
depends(it.value)
}
else {
depends(scriptError)
}
}
}
snip
target(help: "Print a help message") {
println "${scriptName}: possible targets are..."
println "\thelp - print this help message"
}
target(scriptError: "Print an error and die") {
println "${scriptName}: Please specify at least one target name"
depends(help)
exit 1
}
This is another approach that I took
includeTargets << grailsScript("_GrailsArgParsing")
snip
target(main: "a script") {
if(!argsMap.target)
throw new IllegalArgumentException("please specify target name with --target option")
depends(argsMap.target)
}
setDefaultTarget(main)
You run the script with a parameter. That parameter is the name of the method to run :) That method then get's executed.