I have the following requirement.
from Ant xmlproperty task. What happens when there is more than one tag with the same name?
it's clear how to repeat for each file.
My requirement is to iterate for each file and I would like to get the value of 'machine' element for corresponding file
eg:
<echo>${PREFIX.main.tagList.tag.file[file1]}</echo> // should return machine1
<echo>${PREFIX.main.tagList.tag.file[file2]}</echo> // should return machine2
An example would help, but I think I discovered this limitation in the xmlproperty task before. For performing complex processing of external files I would use an embedded groovy task, which just loves XML :-)
You haven't specified a sample input, so here's a similar example:
Parse HTML using with an Ant Script
Related
Part of the job was generated through job-dsl, but there is still part of the job that was created by manual. If Jenkins has a mechanism or api to get a list of all manual jobs.
I see that all jobs through job-dsl have a mark which job created them(seed job). Perhaps having received a list of all generated ones, you can take a list of all jobs and weed out manual ...
JobDSL saves information about generated jobs in the javaposse.jobdsl.plugin.ExecuteDslScripts.xml file, example:
<?xml version='1.1' encoding='UTF-8'?>
<javaposse.jobdsl.plugin.DescriptorImpl plugin="job-dsl#1.77">
<templateJobMap class="com.google.common.collect.HashMultimap" serialization="custom">
<com.google.common.collect.HashMultimap>
<default/>
<int>8</int>
<int>0</int>
</com.google.common.collect.HashMultimap>
</templateJobMap>
<generatedJobMap class="concurrent-hash-map">
<entry>
<string>generated-job-name1</string>
<javaposse.jobdsl.plugin.SeedReference>
<seedJobName>job-which-created-it</seedJobName>
<digest>hash1</digest>
</javaposse.jobdsl.plugin.SeedReference>
</entry>
<entry>
<string>generated-job-name2</string>
<javaposse.jobdsl.plugin.SeedReference>
<seedJobName>job-which-created-it</seedJobName>
<digest>hash2</digest>
</javaposse.jobdsl.plugin.SeedReference>
</entry>
...
</generatedJobMap>
</javaposse.jobdsl.plugin.DescriptorImpl>
If you parse the file and collect all javaposse.jobdsl.plugin.DescriptorImpl/generatedJobMap/entry/string values then you get a list of all generated jobs.
You can next get all jobs by using Jenkins.get().getItems(), collect all names and at the end remove those which were found in the javaposse.jobdsl.plugin.ExecuteDslScripts.xml file.
EDIT:
The file keeps an XML representation of the javaposse.jobdsl.plugin.DescriptorImpl class. You can also get it programmatically: Jenkins.get().getDescriptorByType(Class<?> type).
def jenkins = Jenkins.get()
def clazz = Class.forName('javaposse.jobdsl.plugin.DescriptorImpl', true, jenkins.pluginManager.uberClassLoader)
def descriptor = jenkins.getDescriptorByType(clazz)
descriptor.generatedJobMap // map with listed previously entries
I found a way using the script console!
TL;DR:
Jenkins.instance.jobNames.findAll {
job = Jenkins.instance.getItemByFullName(it)
a = job.getAction(javaposse.jobdsl.plugin.actions.SeedJobAction.class)
return a?.isConfigChanged()
}.each {
println it
}
Explanation
It took some digging (and I've no idea what I'm doing; maybe there's an easy way). I noticed that all the information I was looking for was readily available on the "front page" of jobs. Information here can be added via plugins. After searching the JobDsl code for the string "Seed job:" I came across this file, which is a Jelly file. After some guesswork I came up with the code above.
The a object has getters for the information you see accessed via the it object in the Jelly file, e.g. getSeedJob(). For checking whether the config changed you prefix it with is instead of get, presumably because it returns a boolean. If a is null then it was not generated by a seed job.
If you want to automate: Jenkins has a detailed rest api where you can fetch information about jobs etc. You will get JSON objects back which then need to be interpreted.
https://www.jenkins.io/doc/book/using/remote-access-api/
To check if the Api provides the information you need, you can use http://jenkins:8080/job/yourjob/api/json?pretty=true
I am getting the logs runtime in jmeter and saving it to text file for further processing.
I need to parse the text file for specific keywords like start and stop and get the equivalent timestamp from the text file, then get the difference between them to get the time for processing an operation.
Can anyone let me know how this can be done using Beanshell scripting in Jmeter. Or is there any other better way for the above requirement?
In JMeter 3.3 you have new function __timeShift which return current time,
You can use to save in start variable: ${__timeShift(,,,,start)}
And then save stop variable ${__timeShift(,,,,stop)}
And then use scripting for example for calculate differences e.g. ${__groovy(${stop} - ${start})}
It is recommended to avoid scripting where possible so first of all try using built-in JMeter components, like:
__FileToString() function - to read the file into a JMeter Variable
Regular Expression Extractor - to extract "start" and "end" timestamps
__longSum() function - to get the delta between start and end time
If you have to go for scripting consider using the most performing option which is Groovy language.
I have a directory with a list of property files for different environment(DEV/STG/QA etc.,)
I want to call an Ant target in a loop with each of this file. How do I do this. I downloaded ant-contrib and tried using the foreach but I couldn't find any example where I can read property files one at a time and call the target. Any suggestions?
I have been looking at a lot of samples on this site and online, nothing seem to match my requirement.
As per apacche documentation : https://ant.apache.org/manual/Tasks/property.html
<property file="foo.properties"/>
reads a set of properties from a file called "foo.properties".
so, if you have my.var=25 , then ${my.var} will get you its value 25. For your requirement you can iterate through the files based on 'env' name & do required tasks.
I am reading a file in ant and loading the properties through loadproperties. I am interested in using the value of a specific property, whose name is not known. I know that it follows a pattern because that is how I load the property.
I can echoproperties and see that it is being loaded.
But I dont know how to access its value, given that its name is actually a pattern rather that hardcoded.
How can I access this property's value to do some processing.
I hope this is clear. Please ask if I need to clarify some more.
Take a look at ant-contrib package. Its propertycopy task will do what you need. If you need to resolve an arbitrary number of properties following an established pattern, you would use ant-contrib's propertycopy in conjunction with ant-contribs "for" task.
http://ant-contrib.sourceforge.net/tasks/tasks/index.html
You should use Ant's script task.
I suggest using the beanshell script since it is pure java.
For example, to print all properties for your project, use the following:
<target name="echoprops">
<script language="beanshell">
System.out.println("All Properties: " + project.getProperties().keySet());
</script>
</target>
It should be easy to modify the above script to get the property you want.
To use this task, you will need to run the following in $ANT_HOME first:
ant -f fetch.xml script -Ddest=user
That will download all required optional jars to ~/.ant/lib .
I use the Ant cvs and sql tasks to check out and deploy a full code set of database objects. I also use the Ant cvschangelog task to generate a list of what has changed between two tags. It seems that there should be some way of defining a target process that would iterate over the list of elements from the generated changelog xml file and only execute files that have changed between two tags (all of the files use "CREATE or REPLACE" syntax and only replaceable objects are of interest here).
Is there any native Ant solution for this or will a custom task to parse the xml file be necessary?
You might investigate the ant-contrib tasks for looping constructs. The for or foreach task may be just what you need.
Here is an example :
http://franckbehaghel.free.fr/antTask/
Last time I checked Ant didn't support any sort of looping constructs, so you'll have to write your own custom task> However, there is an xslt task that lets you apply stylesheets to xml files. It will depends on what you want to do with the xml itself.