I need to obtain JAVA_HOME property from Groovy (Gradle), does anyone know how to achieve this? Only way I can think of is somehow executing this from cmd line via Exec.
Thanks
(I'm running Windows btw :))
System.properties.find { it.key == "java.home" }
A gotcha that bit me. Remember to use curly braces inside a gstring.
println "inside a gstring, java.home=$System.properties.'java.home' will be problematic
//dumps all system properties
but
println "inside a gstring, java.home=${System.properties.'java.home'} will be fine
Result: inside a gstring, java.home=C:\FAST\JDK64\1.7.0.79\jre will be fine
Related
I am fairly new to java programming and I am trying to learn groovy right now.
I am getting this weird error when I am entering a simple line of code of hello world.
I am buffered. I believe I have set the environment variables correctly
You have the method name wrong
It's println not printIn
If you look at the exception, it tells you what's wrong, and a list of possible solutions
I think that you are using the wrong letter:
println "hello"
with "L" lowercase
I am trying to generate a drop-down for a Jenkins job that will parse out the version numbers from the file names in a Linux directory. I have gotten it to work most of the way but I think my lack of knowledge of groovy has me at a standstill. Here is the code I have:
Arrays.asList(new File("/path/to/files").list().join(", ").findAll(/(\d+)\.(\d+)\.(\d+)\.(\d+)/))
and my file names look like:
returns?-?1.0.0.19?.war
returns?-?1.0.0.20?.war
What I get as a return from the Jenkins script console is:
Result: [[1.0.0.19, 1.0.0.20]]
This is essentially what I want but in the Jenkins job I get one item in the drop-down that is everything inside the outer brackets.
[1.0.0.19, 1.0.0.20]
I think the second set of brackets is the issue and I have tried to remove them using Groovy's .minus() method, double escaping the brackets, with no luck. I have also tried the .split() method, with no luck.
Any help would be greatly appreciated!
You do not need Arrays.asList(). Below should suffice.
new File("/opt/staples/ci-tools/workspace/archive/returns")
.list()
.join(',')
.findAll(/(\d+)\.(\d+)\.(\d+)\.(\d+)/)
What is the standard (or best practice) for Groovy error messages that that shouldn't span over a certain number of characters/line, e.g., 80 characters?
Consider the following (which is working fine)
throw new IOException("""\
A Jenkins configuration for the given version control
system (${vcs.name}) does not exist."""
.stripIndent()
.replaceAll('\n', ' '))
This will result in a one-line error message with no indention characters (what I want). But is there some other way ("the Groovy way of doing it") how to achieve this? If not, how could you add such a method to the GString class in a standalone Groovy application (if found hints regarding a Bootstrap.groovy file but it seems to be related to Grails)?
Example: """Consider a multi line string as shown above""".toSingleLine()
You could use the String continuation character then strip multiple spaces:
throw new IOException( "A Jenkins configuration for the given version control \
system (${vcs.name}) does not exist.".replaceAll( /( )\1+/, '$1' ) )
Or you could wrap this in a function and add it to the String.metaClass as I believe the answers you've seen point to.
You're right in thinking that Bootstrap.groovy is a Grails thing, but if you just set the metaClass early on in your applications lifecycle, you should get the same result...
String.metaClass.stripRepeatedWhitespace = { delegate.replaceAll( /( )\1+/, '$1' ) }
In saying all this however, I'd probably just keep the message on a single line
Is there any way I can redefine asBoolean for core class in Groovy? I tried
Number.metaClass.asBoolean = {false}
While it works for non-Groovy classes I can't find how to make the same trick for Number or any other predefined class.
I could reproduce your problem when executing the script in the GroovyConsole. However, when executed as a .groovy file on the command line, the metaClass change works as expected.
I think you're just missing a ->. This works for me
Number.metaClass.asBoolean = {-> false}
assert !2.asBoolean() // Normally 2 evaluates to true
When i try execute the following code, which should just print a slashy string in groovy console version 1.7.4 i get a compilation error:
println /slashy string/
if i change this to:
def s = /slashy string/;
println s
everything is fine and the expected string is printed.
Any ideas what i am doing wrong?
The final gotcha (on the linked doc) says, a slashy string cannot be used with assert, because of a grammar limitation. Since println is also a part of the grammar (afaik, since its not a classical java function), I would guess this applies here, too.
It says to use braces around it:
println (/slashy string/)
This worked fine in my groovy shell.