Why doesn't gradle exec work with scripts? - gradle

Is there any way to make gradle exec work like a shell exec? ie - understand executable files in the path?
We have code that needs to work on windows and unix - and many many scripts that are obviously different on both machines. While I can do a hack like this:
npmCommand = Os.isFamily(Os.FAMILY_WINDOWS) ? 'npm.cmd' : '/usr/local/bin/npm'
and then run that command - the paths for some scripts are not necessarily set in stone - and it's just horrible code.
Is there any way to fix the problem - ie extend the exec task to find executable files in the path and run them?

How about something like:
if (System.getProperty('os.name').toLowerCase(Locale.ROOT).contains('windows'))
{
commandLine 'cmd', '/c', 'commandGoesHere'
}
else
{
commandLine 'sh', '-c', 'commandGoesHere'
}

I'd love a better way - but I made a simple function that I put in our common that you use like this:
exec {
commandLine command("npm"), "install"
}
the function looks like this:
//
// find a command with this name in the path
//
String command( String name )
{
def onWindows = (System.env.PATH==null);
def pathBits = onWindows ? System.env.Path.split(";") : System.env.PATH.split(":");
def isMatch = onWindows ? {path ->
for (String extension : System.env.PATHEXT.split(";"))
{
File theFile = new File( path, name + extension);
if (theFile.exists())
return theFile;
}
return null;
} : {path -> def file = new File(path,name);if (file.exists() && file.canExecute()) return file;return null;}
def foundLocal = isMatch(file("."));
if (foundLocal)
return foundLocal;
for (String pathBit : pathBits)
{
def found = isMatch(pathBit);
if (found)
return found;
}
throw new RuntimeException("Failed to find " + name + " in the path")
}

Related

Using wildcard in Gradle Files then copy if it exists

In very brief, I want to find all files that ends with *.sql and copy them if they exist.
There might be 0 or more files in etc directory.
File sqlfiles = file('etc/' + '*.sql')
logger.info("Looking for SQL files: " + sqlfiles);
if (sqlfiles.exists())
{
logger.info("Found log SQL file: " + sqlfiles);
copy
{
from sqlfiles
into "$rpmStoredir"
}
}
else
{
logger.warn("No SQL file found - skipping");
}
With my code, the wildcard is not working here.
So adding "include" to the copy as in the below is working but I just want to figure how to add a logger if the file does not exist
copy
{
from "etc/"
include "*.sql"
into "$rpmStoredir"
}
file(...) is the wrong method to use as this returns a single java.io.File
You could do something like
FileTree myTree = fileTree('etc') {
include '*.sql'
}
if (myTree.empty) {
...
} else {
copy {
from myTree
...
}
}
See Project.fileTree(Object, Closure) and FileTree

Gradle Environment variables. Load from file

I am new to Gradle.
Currently I have this task:
task fooTask {
doLast {
exec {
environment 'FOO_KEY', '1234567' // Load from file here!
commandLine 'fooScript.sh'
}
}
}
fooScript.sh
#!/bin/bash
echo $FOO_KEY
Everything works great. But I have env.file with all needed environment variables. This file is used in Docker builder.
env.file
FOO_KEY=1234567
Question: how can I use env.file together with Gradle environment to load all needed env. params?
What about this :
task fooTask {
doLast {
exec {
file('env.file').readLines().each() {
def (key, value) = it.tokenize('=')
environment key, value
}
commandLine 'fooScript.sh'
}
}
}
I give also my version (check if line is not empty and not a comment, also donot override env var):
file('.env').readLines().each() {
if (!it.isEmpty() && !it.startsWith("#")) {
def pos = it.indexOf("=")
def key = it.substring(0, pos)
def value = it.substring(pos + 1)
if (System.getenv(key) == null) {
environment key, value
}
}
}
But actually, I think they should add this feature as a exec plugin property! It's quite common now to use .env file.
The following code is the only one i've been able to produce and which satisfies two of the most importants requirements to provide an efficient "UNIX standard environment file import" in Android studio :
Loads a file which depends of the Build Type (at least : debug and release)
Exposes specified environment variables in the Android code, actually not as environment variables but as buildConfigFields content.
ext {
node_env = ""
}
android.applicationVariants.all { variant ->
if (variant.name == "debug") {
project.ext.set("node_env", "development")
} else if (variant.name == "release") {
project.ext.set("node_env", "production")
}
file("." + node_env + '.env').readLines().each() {
if (!it.isEmpty() && !it.startsWith("#")) {
def pos = it.indexOf("=")
def key = it.substring(0, pos)
def value = it.substring(pos + 1)
if (System.getProperty(key) == null) {
System.setProperty("env.$key", value)
}
}
}
if (variant.name == "release") {
android.signingConfigs.release.storeFile file(System.getProperty("env.ANDROID_APP_SIGNING_STOREFILE"))
android.signingConfigs.release.keyAlias System.getProperty("env.ANDROID_APP_SIGNING_KEYALIAS")
android.signingConfigs.release.storePassword System.getProperty("env.ANDROID_APP_SIGNING_STOREPASSWORD")
android.signingConfigs.release.keyPassword System.getProperty("env.ANDROID_APP_SIGNING_KEYPASSWORD")
}
android.defaultConfig.buildConfigField "String", "ANDROID_APP_URL", "\"${System.getProperty("env.ANDROID_APP_URL")}\""
}
Kotlin :
Log.i(TAG, BuildConfig.ANDROID_APP_URL)
Please let me know what you think of it as i'm not completly sure how it works, especially to select the good file to load.
There are plugins to load env vars from a .env file (e.g. this one)
So a sample build file will look something like this (Kotlin DSL)
plugins {
id("co.uzzu.dotenv.gradle") version "1.1.0"
}
tasks.withType<Test> {
useJUnitPlatform()
//will pass the env vars loaded by the plugin to the environment of the tests
environment = env.allVariables
}
I have ended up doing it in my gradlew file. A possible drawback is that the change tends to be overwritten on upgrades to gradle.
# Hack: export all variables in the .env file
#
ENV_FILE=../../.env
if [ ! -f $ENV_FILE ];then
echo "WARNING/DEV ENV Missing a ${ENV_FILE} file with environment variables (secrets)";
fi
for secret in `cat $ENV_FILE`;do export $secret;done
If you're using Spring Boot bootRun task or anything that has a runner
tasks.named('bootRun') {
doFirst {
file('.env').readLines().each() {
def (key, value) = it.tokenize('=')
environment key, value
}
}
}

URISyntaxException in gradle environment variable while testing

I have a build.gradle as follows
task setDockerHost {
group 'Docker'
description 'Gets the docker host ip from your OS'
def stdout = new ByteArrayOutputStream()
exec {
commandLine './src/main/resources/scripts/get-docker-host.sh', '60'
standardOutput = stdout
}
project.ext.set('DOCKERHOST', "$stdout")
}
tasks.withType(Test) {
doFirst { println "DockerHost is $project.DOCKERHOST" }
environment 'DOCKERHOST', "$project.DOCKERHOST"
outputs.upToDateWhen { false }
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut'
}
reports.html.destination = file("${reporting.baseDir}/${name}")
}
I define a DOCKERHOST env variable as above and want to use in my groovy test class:
class MyClass extends Specification {
RESTClient client = new RESTClient("http://"+System.getenv('DOCKERHOST')+":9090/")
...
}
In the execution my println works: DockerHost is 192.168.99.100
But when I run the this test it throws:
I already tried replacing \n, \r and spaces by "". I also try removing the protocol from the URL (aka 192.168.99.10:9090) and it tells me that the same error occurs at index 0
How can I solve this?
I didn't figure it out in which char was the problem but I was able to solve it but replacing strings like crazy:
String url = ("http://" + System.getenv('DOCKERHOST') + ":9090/").replace("\n", "").replace("\r", "").replace(" ", "")
RESTClient client = new RESTClient(url)
I've spent like a day trying to figure this out... hopefully for someone else will be quicker.

How to define and call custom methods in build.gradle?

As part of my project, I need to read files from a directory and do some operations all these in build script. For each file, the operation is the same(reading some SQL queries and execute it). I think its a repetitive task and better to write inside a method. Since I'm new to Gradle, I don't know how it should be. Please help.
One approach given below:
ext.myMethod = { param1, param2 ->
// Method body here
}
Note that this gets created for the project scope, ie. globally available for the project, which can be invoked as follows anywhere in the build script using myMethod(p1, p2) which is equivalent to project.myMethod(p1, p2)
The method can be defined under different scopes as well, such as within tasks:
task myTask {
ext.myMethod = { param1, param2 ->
// Method body here
}
doLast {
myMethod(p1, p2) // This will resolve 'myMethod' defined in task
}
}
If you have defined any methods in any other file *.gradle - ext.method() makes it accessible project wide. For example here is a
versioning.gradle
// ext makes method callable project wide
ext.getVersionName = { ->
try {
def branchout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
standardOutput = branchout
}
def branch = branchout.toString().trim()
if (branch.equals("master")) {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
return stdout.toString().trim()
} else {
return branch;
}
}
catch (ignored) {
return null;
}
}
build.gradle
task showVersion << {
// Use inherited method
println 'VersionName: ' + getVersionName()
}
Without ext.method() format , the method will only be available within the *.gradle file it is declared. This is the same with properties.
You can define methods in the following way:
// Define an extra property
ext.srcDirName = 'src/java'
// Define a method
def getSrcDir(project) {
return project.file(srcDirName)
}
You can find more details in gradle documentation Chapter 62. Organizing Build Logic
An example with a root object containing methods.
hg.gradle file:
ext.hg = [
cloneOrPull: { source, dest, branch ->
if (!dest.isDirectory())
hg.clone(source, dest, branch)
else
hg.pull(dest)
hg.update(dest, branch)
},
clone: { source, dest, branch ->
dest.mkdirs()
exec {
commandLine 'hg', 'clone', '--noupdate', source, dest.absolutePath
}
},
pull: { dest ->
exec {
workingDir dest.absolutePath
commandLine 'hg', 'pull'
}
},
]
build.gradle file
apply from: 'hg.gradle'
hg.clone('path/to/repo')
Somehow, maybe because it's five years since the OP, but none of the
ext.someMethod = { foo ->
methodBody
}
approaches are working for me. Instead, a simple function definition seems to be getting the job done in my gradle file:
def retrieveEnvvar(String envvar_name) {
if ( System.getenv(envvar_name) == "" ) {
throw new InvalidUserDataException("\n\n\nPlease specify environment variable ${envvar_name}\n")
} else {
return System.getenv(envvar_name)
}
}
And I call it elsewhere in my script with no prefix, ie retrieveEnvvar("APP_PASSWORD")
This is 2020 so I'm using Gradle 6.1.1.
#ether_joe the top-voted answer by #InvisibleArrow above does work however you must define the method you call before you call it - i.e. earlier in the build.gradle file.
You can see an example here. I have used this approach with Gradle 6.5 and it works.
With Kotlin DSL (build.gradle.kts) you can define regular functions and use them.
It doesn't matter whether you define your function before the call site or after it.
println(generateString())
fun generateString(): String {
return "Black Forest"
}
tasks.create("MyTask") {
println(generateString())
}
If you want to import and use a function from another script, see this answer and this answer.
In my react-native in build.gradle
def func_abc(y){return "abc"+y;}
then
def x = func_abc("y");
If you want to check:
throw new GradleException("x="+x);
or
println "x="+x;

platform independent symlinks in build script

I need to add to gradle build script a command that will create a symlink. I know that the person who builds that has cygwin installed. The problem is with export command. Here is what I got so far
if(OS == 'win32') {
exec { commandLine "C:\\cygwin\\bin\\mintty.exe", "--hold always", "/bin/bash", "-l", "-e", "export", "CYGWIN=winsymlinks", "&&", "-e", "ln", "-s", link, file}
//exec { commandLine "cmd", "/c", "mklink", link, file}
//exec { commandLine "export", "CYGWIN=winsymlinks" }
//exec { commandLine "C:\\cygwin\\bin\\ln.exe" , "-s", link, file}
}
else {
exec { commandLine "ln", "-s", link, file}
}
Is there a standard way of doing it?
Gradle doesn't offer a public API for creating symlinks. If your builds run under JDK 7 or higher, you could try the JDK's symlink API. Otherwise, it should be possible to solve this with exec as well, as long as you can figure out the correct command.
The Ant builder, provided by every task has one. Seems like a solution, but as pointed out in the comments is not really cross platform:
task createLink << {
ant.symlink(resource: "file", link: "link")
}
Instead you could call the NIO API in Java, but you will need 1.7. Take a look at the
createSymbolicLink
Example of Files.createSymbolicLink from build.gradle:
File devDir = new File("${project.rootDir}/.dev")
File configDir = new File("${project.projectDir}/config")
Files.createSymbolicLink(devLink.toPath(), devDir.toPath())
In the special case where your aim is to create a symlink to an executable file, there is a cross platform workaround, which is to create a shell script that will forward to the target :
File forwarding_script_template:
#! /bin/bash
_DIR_=$(dirname "$'{'BASH_SOURCE[0]'}'")
"$_DIR_/"{0} "$#"
In Gradle build file:
String templateScript = new File(projectDir,
"gradle/forwarding_script_template.sh").text;
String script = MessageFormat.format(templateScript, target);
writeFile(destination, script);
destination.setExecutable(true);
with the writeFile method equal to:
void writeFile(File destination, String content) {
Writer writer = new FileWriter(destination);
writer.write(content);
writer.close();
}

Resources