StringUtils for ant build files? - string

I am looking for some util methods for strings manipulation. Eg. split a version into its major.minor.bugfix parts:
1.0.2
major=1
minor=0
bugfix=2
I have looked at:
http://www.docjar.com/docs/api/org/apache/tools/ant/util/StringUtils.html
which is located in my C:\apache-ant-1.8.2\lib\ant.jar
but how do I use eg. the plit method in my build.xml file?
Here its done using groovy:
http://www.coderanch.com/t/431213/tools/Ant-Split-string-assign-property
But is there no "official" ant jar/extensions that contain this basic kind of string manipulation tasks that can be called in my build.xml file?

See Ant Addon Flaka, it provides some functions for string manipulation,
some examples here.
The answer to your problem is the example :
Question : Given is an ant property which has value of the type 1.0.0.123
How to extract the value after the last dot, in this case that would be '123' ?
Solution : use the split function with index
Alternatively use Groovy's Ant task or script task with groovy/jruby/beanshell..

Related

Can I create 'shared library' in Jenkins Pipeline in other language than Groovy?

I have python script which does a REST command and processes the result. I want this script to be used by different Jenkins Pipelines, one way I found through Jenkins official documentation is to use 'Shared library' and that examples(also others example which I found online) they use the Groovy.
My question is, is it possible to create a shared lib in other language than Groovy? For ex. python?
Short answer is no. All Jenkins Pipeline execution (right now) is specialized Groovy that is executed with the Pipeline: Groovy plugin that uses the Groovy CPS library to perform the compilation and runtime transformations. The Jenkins Pipeline ecosystem is very heavily tied to Groovy. That may change in the future, but is not what worth the effort right now.
What you can do, if you really want to use Python code in a Shared Library, is to put it in the resources/ folder of the library and then load and execute it with pipeline steps. Your use case for why you want to use Python is not stated (or what problem you are trying to solve), so below is a contrived example:
In your Shared Library: resources/com/mkobit/sharedlib/my_file.py
#!/usr/bin/env python
print("Hello")
Shared Library Groovy global variable: vars/mkobitVar.groovy
def runMyPython() {
final pythonContent = libraryResource('com/mkobit/sharedlib/my_file.py')
// There are definitely better ways to do this without having to write to the consumer's workspace
writeFile(file: 'my_file.py', text: pythonContent)
sh('chmod +x my_file.py && ./my_file.py')
}
In a consumer
#Library('mkobitLib') _
node('python') {
mkobitVar.runMyPython()
}

Can I combine all Groovy sources to one script?

I have a project on Groovy, which is built with Maven. Can I somehow on build step to merge all files with a source code into a single Groovy script for delivery to the end user?
That is impossible. Another way is use evaluate:
evaluate(new File("../tools/Tools.groovy"))

Give an example of: groovyc --sourcepath

I am unable to get the --sourcepath option of groovyc to work at all. Can someone furnish a trivial example of it actually doing anything?
Ultimately I want to use "groovyc" at the command line with a directory a packaged organized tree of mixed groovy and java source. I don't want to reference each source file explicitly. And I don't want to use an ant or maven task either, on grounds of both principle (hey is there a bug here?) and because the production scenario that I might want to tweak the source in has neither but will have groovy. I know I could use unix find but must I resort to that?!
sourcepath isn't used anymore. It's only there for backwards compatibility and will be removed in the future.
The Groovy documentation is currently rewritten, you can find a snapshot including the documentation for groovyc here: https://dl.dropboxusercontent.com/u/20288797/groovy-documentation/index.html#ThegroovycAntTask-groovyc

How to load a text file from within an XQuery?

Is there an XQuery command to load a text file?
I can load an xml document by doing the following;
declare variable $text := doc("test.xml");
But it only seems to work if test.xml is a well-formed xml document. What I want is to load a plain test.txt file into a string variable. Something like this;
declare variable $str as xs:string := fn:loadfile("test.txt");
Can it be done?
I'm using the Saxon engine but I can't find an answer in the saxon documentation.
Indeed you can find one implementation of the file functions in Zorba: http://www.zorba-xquery.com/doc/zorba-1.4.0/zorba/xqdoc/xhtml/www.zorba-xquery.com_modules_file.html
XQuery 3.0 has the function fn:unparsed-text (which was originally defined in XSLT), which does exactly what you want. XQuery 3.0 is still a work in progress though, but whilst there are not many XQuery 3.0 processors available, many XQuery processors already support this function (including Saxon).
There is a standardization effort for this on EXPath. A spec already exists for an XQuery File module that is capable of doing what you describe: EXPath File Module Spec.
Yet, I don't know how many implementations are out there. Saxon doesn't seem to implement it unfortunately (Or, please point me to it). An example implementation is shipped with zorba (see XQDoc Site of Zorba). If you want to know how to get started with zorba, you can check out this tutorial: Get Started with XQuery and Zorba.
XQuery by default( means fn: namespace ) doesn;t have any file-access methods.
MarkLogic :
xdmp:filesystem-file()
xdmp:filesystem-directory()
Zorba:
already mentioned by user457056
Exist
Exist File Module
Saxon since version 9.2 has an extension of fn:collection that can be used to read unparsed text. Here is an example:
collection('file:///c:/TEMP?select=text.txt;unparsed=yes')
This is described under "Changes in this Release" for 9.2. Apparently it is not mentioned in the function library documentation. However it works well and I have been using it a lot.

Importing csv files using groovy

I have developed a groovy application. Now it has been required
that for feeding the DB a CSV interface must be provided.
That is, I have to load a csv file, parse it and
insert records into the DB in a transactional way.
The question is if there exists for groovy something
like ostermiller utils (a configurable csv parser).
Thanks in advance,
Luis
Kelly Robinson just wrote a nice blog post about the different possibilities that are available to work with CSV files in Groovy.
Groovy and Java are interoperable. Take a look at the documentation for mixed Java and Groovy applications. Any Java class can easily be used in Groovy with no change (plus you have the groovy syntax). If you are interested in the ostermiller utils to do your CSV parsing, you can use it directly from Groovy.
If the ostermiller library does what you want you can call it directly from Groovy. Just put the necessary jars in your groovy\lib directory and you should be ready to go.

Resources