Understanding Gradle configurations

satya - 7/26/2018, 6:12:09 PM

What are configurations?

1. A project object (represented by the gradle build file) has a set of configuraitons

2. Each configuration is (at the base) a set of files (like all the jar files that one might need during compile, or test, or build, or just for a classpath).

3. The dependencies object/script of the project object (or the main gradle build file) attaches many files for/under each configuration

4. So the object hierarchy is "project/configuration(s)/dependencies (files)/"

satya - 7/26/2018, 6:14:20 PM

An example of a configuration


configurations {
    jasper
}

repositories {
    mavenCentral()
}

dependencies {
    jasper 'org.apache.tomcat.embed:tomcat-embed-jasper:9.0.2'
}

task preCompileJsps {
    doLast {
        ant.taskdef(classname: 'org.apache.jasper.JspC',
                    name: 'jasper',
                    classpath: configurations.jasper.asPath)
        ant.jasper(validateXml: false,
                   uriroot: file('src/main/webapp'),
                   outputDir: file("$buildDir/compiled-jsps"))
    }
}

configurations {
    smokeTest.extendsFrom testImplementation
}

dependencies {
    testImplementation 'junit:junit:4.12'
    smokeTest 'org.apache.httpcomponents:httpclient:4.5.5'
}

satya - 7/26/2018, 6:29:10 PM

Here is how to read that file

1. configurations is a method (script) against the project object

2. Delegate for this script is ConfigurationContainer

3. jasper is equivalent to create("jasper") creating a new configuration called "jasper"

4. it is not clear how the translation in ste 3 happens!!!

5. In dependencies block a jar file is added to the dependency "jasper"

satya - 7/26/2018, 6:30:06 PM

ConfiguraitonContainer is documented here

ConfiguraitonContainer is documented here

satya - 7/26/2018, 6:38:22 PM

NamedDomainObjectContainer and Gradle DSL

NamedDomainObjectContainer and Gradle DSL

Search for: NamedDomainObjectContainer and Gradle DSL

satya - 7/27/2018, 8:30:35 AM

This is discussed here

This is discussed here

satya - 7/27/2018, 1:03:29 PM

See this first: AbstractNamedDomainObjectContainer.java

See this first: AbstractNamedDomainObjectContainer.java

satya - 7/27/2018, 1:04:58 PM

See this code


public AbstractNamedDomainObjectContainer<T> configure(Closure configureClosure) {
        ConfigureDelegate delegate = createConfigureDelegate(configureClosure);
        ConfigureUtil.configureSelf(configureClosure, this, delegate);
        return this;
    }

Question now is what actually is the ConfigureDelegate?

satya - 7/27/2018, 1:05:23 PM

That code is here


protected ConfigureDelegate createConfigureDelegate(Closure configureClosure) {
        return new NamedDomainObjectContainerConfigureDelegate(configureClosure, this);
    }

satya - 7/27/2018, 1:08:55 PM

Here is that code


public class NamedDomainObjectContainerConfigureDelegate 
extends ConfigureDelegate {
    private final NamedDomainObjectContainer _container;

    public NamedDomainObjectContainerConfigureDelegate(Closure configureClosure, 
                                   NamedDomainObjectContainer container) {
        super(configureClosure, container);
        _container = container;
    }

    @Override
    protected DynamicInvokeResult _configure(String name) {
        return DynamicInvokeResult.found(_container.create(name));
    }

    @Override
    protected DynamicInvokeResult _configure(String name, Object[] params) {
        if (params.length == 1 && params[0] instanceof Closure) {
            return DynamicInvokeResult
                  .found(_container.create(name, (Closure) params[0]));
        }
        return DynamicInvokeResult.notFound();
    }
}

satya - 7/27/2018, 1:25:26 PM

what this means is all NamedDomainObjectContainerDelegates behave this way

Not just the configurations {} script!!!!

Good to realize.