Help on the lazy evaluation and user-defined method call?

(Guojun Zhu) #1

I have a folder (“distJarPath”) with all the runtime jars and project jar. I need to copy some into another folder ("${gridlibPath}/jar").

project.ext.gridlibJarList = [, 'commons-lang', 'commons-logging', 'visualNumerics', 'jini-core']
  private boolean isGridlibJar(final File file) {
    gridlibJarList.each {
        gridlibJar ->
            if ( {
                println "One jar for gridlib: ${file}"
                return true;
    println "not for gridlib: ${file}"
    return false;
  task buildDistribution(dependsOn: ['buildJarDir', 'buildGridlib']) << {
    println "Building the files for distribution..."
      println "Building the jar libs @ ${distJarPath} by the task "buildJarDir" ..."
          println "Building the gridlib jar folder @ "${gridlibPath}/jar" ..."
    def distJarCollection = fileTree(distJarPath)
      def gridlibJars=file(distJarPath).listFiles().findAll{println; isGridlibJar(it)}.collect{println it;it}
    println " ${distJarCollection};\t ${distJarCollection.files}"
    println "gridlib jars: (${gridlibJars.class})
    copy {
        from gridlibJars
        into "${gridlibPath}/jar"

However I got the result as such:

               One jar for gridlib: C:\test\biz_sim\build\dist\fmac\jars\commons-lang-2.4.jar
not for gridlib: C:\test\biz_sim\build\dist\fmac\jars\commons-lang-2.4.jar
            One jar for gridlib: C:\test\biz_sim\build\dist\fmac\jars\commons-logging-1.2.jar
not for gridlib: C:\test\biz_sim\build\dist\fmac\jars\commons-logging-1.2.jar
              not for gridlib: C:\test\biz_sim\build\dist\fmac\jars\commons-math3-
 directory 'build/dist/fmac/jars';
     [C:\test\biz_sim\build\dist\fmac\jars\aopalliance-1.0.jar, ..... C:\test\biz_sim\build\dist\fmac\jars\xsdlib-1.0.jar]
  gridlib jars: (class java.util.ArrayList)

Why does the final ‘gridlibJars’ object have no element?

(Guojun Zhu) #2

I figured it out. I should not return the value in the clouse for ‘each’ in ‘isGridlibJar()’ method. The code keeps running and it returns false all the time. Change it to for loop or modify it works. I am really a Java programmer and functional programming is mind-bogging.

Etiehr way it works.

private boolean isGridlibJarEach(final File file) {
    boolean qualified=false;
    return qualified;


private boolean isGridlibJar(final File file) {
    for (String str:gridlibJarList) {
        final String gridlibJar=str
            if ( {
                return true;
    return false;