Use double quotes
dir ("RELEASE/abc/${abc_version}")
in groovy Single quotes are a standard Java String while Double quotes are a templatable String
e.g
a = 10
b = "RELEASE/abc/${a}"
c = 'RELEASE/abc/${a}'
print(b)
print('\n')
print(c)
output will be
RELEASE/abc/10
RELEASE/abc/${a}
You can try it here
Answer from Talha Junaid on Stack OverflowUse double quotes
dir ("RELEASE/abc/${abc_version}")
in groovy Single quotes are a standard Java String while Double quotes are a templatable String
e.g
a = 10
b = "RELEASE/abc/${a}"
c = 'RELEASE/abc/${a}'
print(b)
print('\n')
print(c)
output will be
RELEASE/abc/10
RELEASE/abc/${a}
You can try it here
We need to use double quotes to resolve variable in groovy script.
use the following sample code
def dirpath = "RELEASE/abc/${abc-version}"
dir(dirpath){
//logic
}
groovy - Get a list of all the files in a directory (recursive) - Stack Overflow
cwd - How to change the current working directory in Groovy - Stack Overflow
How to find current base execution directory in groovy (or java)? - Stack Overflow
What's the Groovy equivalent to Python's dir()? - Stack Overflow
This code works for me:
import groovy.io.FileType
def list = []
def dir = new File("path_to_parent_dir")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}
Afterwards the list variable contains all files (java.io.File) of the given directory and its subdirectories:
list.each {
println it.path
}
Newer versions of Groovy (1.7.2+) offer a JDK extension to more easily traverse over files in a directory, for example:
import static groovy.io.FileType.FILES
def dir = new File(".");
def files = [];
dir.traverse(type: FILES, maxDepth: 0) { files.add(it) };
See also [1] for more examples.
[1] http://mrhaki.blogspot.nl/2010/04/groovy-goodness-traversing-directory.html
If you can run other script as separate process, you can give ProcessBuilder parameter working dir:
def processBuilder=new ProcessBuilder(command)
processBuilder.directory(new File("Working dir"))
def process = processBuilder.start()
or
command.execute(null, new File("Working dir"))
so that process will switch to your new folder and execute it there.
As Groovy runs on JVM, the same restrictions apply. Unfortunately it is not possible.
Changing the current working directory in Java?
JDK bug
Looks particulary nice in Groovy (untested, taken from this link so code credit should go there):
// Introspection, know all the details about classes :
// List all constructors of a class
String.constructors.each{println it}
// List all interfaces implemented by a class
String.interfaces.each{println it}
// List all methods offered by a class
String.methods.each{println it}
// Just list the methods names
String.methods.name
// Get the fields of an object (with their values)
d = new Date()
d.properties.each{println it}
The general term you are looking for is introspection.
As described here, to find all methods defined for String object:
"foo".metaClass.methods*.name.sort().unique()
It's not as simple as Python version, perhaps somebody else can show better way.
My current plan is to split the string
repo/dir/file.javaon the forward slash, and create directories until the last element of the split result
Rather than splitting your string manually, you could try using File.mkdirs():
File newDirectoryStructureParent = new File('some/path/to/parent/dir')
def s = 'repo/dir/file.java'
def newContainer = new File(s, newDirectoryStructureParent).getParent()
newContainer.mkdirs()
Similar to the accepted answer with a few less hoops.
File file = new File("full/path/to/file.java")
if(!file.getParentFile().exists()) { // Checks for "full/path/to".
file.getParentFile().mkdirs(); // Creates any missing directories.
}