如何在Java与Groovy中使用Gradle的NamedDomainObjectContainer

时间:2019-05-02 12:08:48

标签: java gradle gradle-plugin

我一直在尝试创建具有嵌套NamedDomainObjectContainer的扩展名的自定义插件。与使用Action在Groovy中使用同一事物相比,如果我使用Closure在Java中实现该错误,则会不断出现奇怪的错误。

这是Groovy之一:

package com.example.gradle

import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.Plugin

class DeploymentPlugin implements Plugin<Project> {

    void apply(final Project project) {
        def servers = project.container(Server)
        servers.all {
            nodes = project.container(Node)
        }
        project.extensions.add('deployments', servers)
    }

    static class Server {
        NamedDomainObjectContainer<Node> nodes
        String url
        String name

        Server(String name) {
            this.name = name
        }

        def nodes(final Closure configureClosure) {
            nodes.configure(configureClosure)
        }
    }

    static class Node {
        String name
        Integer port

        Node(String name) {
            this.name = name
        }
    }
}

还有Java:

package com.example.gradle;

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

public class DeploymentPlugin2 implements Plugin<Project> {

    public void apply(final Project project) {
        final NamedDomainObjectContainer<Server2> servers = project.container(Server2.class);
        servers.all(it ->
                it.nodes = project.container(Node2.class)
        );
        project.getExtensions().add("deployments2", servers);
    }

    public static class Server2 {
        public NamedDomainObjectContainer<Node2> nodes;
        public String url;
        public String name;

        public Server2(String name) {
            this.name = name;
        }

        public void nodes(final Action<? super NamedDomainObjectContainer<Node2>> action) {
            action.execute(nodes);
        }
    }

    public static class Node2 {
        public String name;
        public Integer port;

        public Node2(String name) {
            this.name = name;
        }
    }
}

还有build.gradle文件:

apply plugin: com.example.gradle.DeploymentPlugin
apply plugin: com.example.gradle.DeploymentPlugin2

wrapper {
    gradleVersion = '5.4.1'
    distributionType = Wrapper.DistributionType.ALL
}

deployments {
    aws {
        url = 'http://aws.address'

        nodes {
            node1 {
                port = 9000
            }
            node2 {
                port = 80
            }
        }
    }

    cf {
        url = 'http://cf.address'

        nodes {
            test {
                port = 10001
            }
            acceptanceTest {
                port = 10002
            }
        }
    }
}

deployments2 {
    aws {
        url = 'http://aws.address'

        nodes {
            node1 {
                port = 9000
            }
            node2 {
                port = 80
            }
        }
    }

    cf2 {
        url = 'http://cf.address'

        nodes {
            test {
                port = 10001
            }
            acceptanceTest {
                port = 10002
            }
        }
    }
}

哪个失败:

PS C:\source\gradle-nested-doc-bug> ./gradlew tasks

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\source\gradle-nested-doc-bug\build.gradle' line: 42

* What went wrong:
A problem occurred evaluating root project 'gradle-nested-doc-bug'.
> Could not find method node1() for arguments [build_afudfj5pxfy9w4tkoowa6djon$_run_closure3$_closure12$_closure14$_closure15@4724dfaa] on object of type com.example.gradle.DeploymentPlugin2$Server2.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

使用NamedDomainObjectContainer时,嵌套Action会发生一些时髦的事情。

有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

类似的东西

<input typeaheadval="3" onfocus="fnborderRow(21);" onfocusout="fnborderRow(0);" type="text" id="drpClass21" vendorpaymentid="149962" vendorpaymentdetailid="167679" rowid="21" sortfieldcol="clsSortPayee" value="210 UT - Salt Lake City" selectedid="210" onchange="fnChangeObj(this);" style="border-width: 1px; border-style: solid; border-color: rgb(195, 195, 195); background-color: rgb(255, 255, 255);" class="form-control clsSearch clsClass ui-autocomplete-input clsGLClass149962" autocomplete="off">