title: Gradle, creating objects in build.gradle scripts
body: <p>In my gradle plugin I am using other <code>build.gradle</code> script, which looks like:</p>
  
  <p>build.gradle:</p>
  
  <pre><code>sciptInstance {
      name {
          arrayWithObjects = [
              {
                  var1 = "val1"
                  var2 = "val2"
              {
          ]
      }
  }
  </code></pre>
  
  <p>In gradle plugin I declared container to hold instances of class <code>ScriptClass</code>. This class has field <code>arrayWithObjects</code>. Class declaration of <code>arrayWithObjects</code>'s element looks like:</p>
  
  <pre><code>MyClass {
      def var1
      deg var2
  }
  </code></pre>
  
  <p>In my <code>SciptClass</code>:</p>
  
  <pre><code>ScriptClass {
      // other fields
      MyClass[] arrayWithObjects
  
      // constructor and so on..
  }
  </code></pre>
  
  <p>While trying to build script, I receive a cast exception, saying that I cannot cast closure into object. I tried to declare <code>arrayWithObjects</code> in <code>build.gradle</code> with other parenthesis and so on, but nothing really works. I also do not really know where to look for it. Perhaps some of you have faced this issue before?
  Thanks in advance.</p>
  
  <p>EDIT:
  To reproduce this:</p>
  
  <p>Plugin:</p>
  
  <pre><code>apply plugin: MyPlugin
  
  apply from: 'example.gradle'
  
  class MyPlugin implements Plugin&lt;Project&gt; {
      void apply(Project project) {
          def examples = project.container(ScriptClass)
          project.extensions.examples = examples
      }
  }
  
  class ScriptClass {
      String name
      MyClass[] arrayWithObjects
  
      ScriptClass(String name) {
          this.name = name
      }
  }
  
  class MyClass {
      def var1
      def var2
  }
  </code></pre>
  
  <p>example.gradle:</p>
  
  <pre><code>examples {
      example {
          arrayWithObjects = [
                  {
                      var1 = "val1"
                      var2 = "val2"
                  }
          ]
      }
  }
  </code></pre>
  
  <p>In main <code>build.gradle</code> apply this plugin and invoke any task.</p>