Search code examples
javaantgroovyantbuilder

How to display all resources included in a Groovy AntBuilder classpath?


I am using Groovy's AntBuilder to build a Java project. An error is occurring where classes are missing that really look like they should be included. I want to print out every JAR/WAR that's included in the following classpath closure, but I can't figure out how to do that. Every time I've tried to display the contents of classpath, it shows up as null. Any ideas on how to do this?

For reference, here's the java build task and the classpath. This occurs within a new AntBuilder().with { } closure.

javac srcdir: sourceDir, destdir: destDir, debug: true, target: 1.6, fork: true, executable: getCompiler(), {
   classpath {
      libDirs.each { dir -> fileset dir: dir, includes: "*.jar,*.war"   }
      websphereLib.each { dir -> fileset dir: dir, includes: "*.jar*" }
      if (springLib.exists()) { fileset dir: springLib, includes: "*.jar*" }
      was_plugins_compile.each { pathelement location: was_plugins_dir + it }
      if (webinf.exists()) { fileset dir: webinf, includes: "*.jar" }         
      if (webinfclasses.exists()) { pathelement location: webinfclasses }
   }
}

Solution

  • You could define the classpath outside the javac task as an Ant path. Then you can save the resulting org.apache.tools.ant.types.Path object to get its path entries and print them to System.out. Within the javac task you can reference the classpath with its refid:

    new AntBuilder ().with {
        compileClasspath = path(id: "compile.classpath") {
            fileset(dir: "libs") {
                include(name: "**/*.jar")
            } 
        }
    
        // print the classpath entries to System.out
        compileClasspath.list().each { println it }
    
        javac (...) {
            classpath (refid: "compile.classpath")
        }
    }