Skip to content

Instantly share code, notes, and snippets.

@mubbashir
Last active April 9, 2019 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mubbashir/821e9327e48ae828b8658626ed0a69b2 to your computer and use it in GitHub Desktop.
Save mubbashir/821e9327e48ae828b8658626ed0a69b2 to your computer and use it in GitHub Desktop.
Gradle task to check files/commands in PATH environment variable
// rest of your build
// Verfied with gradle 4.10
task("checkEnv"){
doFirst {
def listOfFileToCheckInPath = ['find', 'grep']
listOfFileToCheckInPath.each { file ->
if(!isFoundInPath(file))
throw new GradleException("${file} was not found in any of the folder in PATH: ${System.getenv('PATH').split(File.pathSeparator)}")
}
}
}
/**
* Static function to verify if a file/command exist in PATH environment
* @param file
* @return true if found, else false
*/
def static isFoundInPath( file){
def PATH_ENV = System.getenv('PATH')
def fileFound = PATH_ENV.split(File.pathSeparator).find{ folder ->
println("Looking for ${file} in ${folder}")
if (Paths.get( "${folder}${File.separator}${file}").toFile().exists()){
println("Found ${file} in ${folder}")
return true
}
}
return fileFound
}
// Making test task to depend on checkEnv
test.dependsOn checkEnv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment