user3777385
user3777385

Reputation: 31

passing environment variables from build.gradle to custom plugin

I have a section that defines the environment variables in build.gradle and I want to pass this to my custom plugin.

Snippet of build.gradle as below:

apply "myplugin"

ext { 
    lombokVersion = '1.18.6'
    setEnvironmnetVariables = { 
        environment -> environment.put{'RUNTIME_ENV', 'test')
    }

I want to see this RUNTIME_ENV in my plugin 'myplugin'. I am new to this gradle plugin development. Could anyone help me out with this? I am using spring-boot project with groovy.

Upvotes: 0

Views: 1917

Answers (1)

Cisco
Cisco

Reputation: 23052

You can't set environment variables from Gradle nor Java in general.

You can however set dynamic project properties which is one way to convey information to your custom plugin.

Since you're already using the extra propeties, you can just set the values you need directly:

// Root project build.gradle
ext {
    lombokVersion = "1.18.6"
    RUNTIME_ENV = "test
}

Then your custom plugin, you access them like so:

import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class MyPlugin implements Plugin<Project> {

    @Override
    public void apply(Project project) {
        String runtimeEnv = (String) project.getExtensions().getExtraProperties().get("RUNTIME_ENV");
        // do something with variable
    }

}

Gradle has great guides on building plugins, give them a look.

Additional, if you have Gradle installed locally, you can create a skeleton Gradle plugin project with the init task:

gradle help --task init

Upvotes: 2

Related Questions