Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Pass environment to gradle exec

Tags:

gradle

How can I pass variables to Exec during execution? I want to write a pass through gradle file that will exec my current build commands which let's me move configuration from build server plans to source control managed build.gradle file. This is also part of my getting to know gradle in preparation for larger projects.

I want to have execute commands using different variables for configurations. In ant, I'd set my properties and then pass them to exec via nested env blocks. In gradle, I'm populating a map which I merge with the task's environment but this isn't working.

I can't add '<<' to the checkenv so the task code executes prior buildEnvironmentVariables being populated or is in the wrong scope. I know I'm not following proper task configuration.

Please offer suggestions or point me at the right part of the manual/docs.

build.gradle - execution gradle checkenv

def buildEnvironmentVariables = [:]
task setEnv() << {
    buildEnvironmentVariables['JAVA_OPTS']="-XX:ErrorFile=foo/logs" 
}

task checkenv(dependsOn: 'printEnv', type:Exec) {
    workingDir '../..'
    executable = 'cmd'
    environment << buildEnvironmentVariables
    println "buildEnvironmentVariables = " << buildEnvironmentVariables['JAVA_OPTS']
    args = ['/c','set','JAVA_OPTS']
}

Should I be only adding a task to the project when it is the equivalent of a "target" and encapsulating actions like the exec within the top level tasks?

Added task is like ant target and encapsulated tasks is like ant task?

def buildEnvironmentVariables = [:]
task setEnv() << {
    buildEnvironmentVariables['JAVA_OPTS']="-XX:ErrorFile=foo/logs" 
}

task checkenv(dependsOn: 'printEnv') << {
    println "buildEnvironmentVariables = " << buildEnvironmentVariables['JAVA_OPTS']
    ext.check = exec() {
        workingDir '../..'
        executable = 'cmd'
        environment << buildEnvironmentVariables
        args = ['/c','set','JAVA_OPTS']
    }
}

Thanks

like image 845
Peter Kahn Avatar asked Oct 05 '12 20:10

Peter Kahn


People also ask

How do you pass JVM arguments in Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.

What is exec in Gradle?

Gradle provides a built-in `Exec` task type to execute external processes. Command-line tools often produce some output for the user. By default, an Exec task prints all of the command's output back to the console. build.gradle: task date(type: Exec) {

How do I change the environment of a Gradle?

Enter the environment-specific property settings inside the appropriate properties file. The contents of these environment files will override any values set in the gradle.properties file. To specify an environment at runtime, use the -PenvironmentName=xxx option.

Where are Gradle properties stored in Linux?

Gradle properties such as org.gradle.caching=true that are typically stored in a gradle.properties file in a project root directory or GRADLE_USER_HOME environment variable. Environment variables such as GRADLE_OPTS sourced by the environment that executes Gradle.

How do I PASS system properties to Gradle?

System properties Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle.properties files with the prefix systemProp.

Is it possible to use javaexec with Gradle?

However, this is only available from Gradle 4.9 onward. Let’s see what this would look like using a JavaExec task. First, we need to define it in our build.gradle:


1 Answers

It's probably better to start from a clean slate:

task doSomething(type: Exec) {
    workingDir ...
    executable ...
    args ...
    environment JAVA_OPTS: "-XX:ErrorFile=foo/logs"
}

You can then run this task with gradle doSomething. Does this accomplish your goals?

like image 111
Peter Niederwieser Avatar answered Sep 22 '22 08:09

Peter Niederwieser