ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Loughran <ste...@apache.org>
Subject Re: DO NOT REPLY [Bug 39780] New: - antunit doesnt set java.class.path
Date Mon, 12 Jun 2006 16:33:15 GMT
Matt Benson wrote:
>> this test works on the command line:
>>
>>   <target name="testPath" depends="define">
>>     <book:filesize property="size">
>>       <path path="${java.class.path}"/>
>>     </book:filesize> 
>>     <echo> classpath size=${size}</echo>
>>   </target>
>>
>> but when run from antunit, it fails cos
>> java.class.path isnt set.
> 
> Steve, I was noticing this filesize task you've got in
> the "book" ns... I assume this relates to an example
> that will go in the forthcoming revision to the
> Manning book.  How does <book:filesize> differ from
> the 1.6.3+ <length> task?

hey, go check out antbook.cvs.sourceforge.net and see for yourself.


For reference, it only does file resources. I iterate and cast

     public void execute() throws BuildException {
         if (property == null) {
             throw new BuildException("No property");
         }
         long size = 0;
         int count = 0;
         Iterator element = resources.iterator();
         while (element.hasNext()) {
             Resource resource = (Resource) element.next();
             if (!(resource instanceof FileResource)) {
                 throw new BuildException("Not a file: " + resource);
             }
             log(resource.getName(), Project.MSG_VERBOSE);
             FileResource fileResource = (FileResource) resource;
             File file = fileResource.getFile();
             if (!file.exists()) {
                 throw new BuildException("Not found: " + file);
             }
             size += file.length();
             count++;
         }
         if (count == 0) {
             log("No files sized up", Project.MSG_WARN);
         }
         getProject().setNewProperty(property, Long.toString(size));
     }

It just shows how to go from a file to a fileset to resources.

the other interesting task does classpath setup and delegates to java

     /**
      * {@inheritDoc}
      * @throws org.apache.tools.ant.BuildException
      *          if something goes wrong with the build.
      */
     public void execute() {
         Java java=new Java();
         java.bindToOwner(this);
         java.init();
         java.setClasspath(classpath);
         java.setClassname(classname);
         java.setFailonerror(true);
         java.setFork(true);
         Iterator element = resources.iterator();
         while (element.hasNext()) {
             Resource resource = (Resource) element.next();
             java.createArg().setValue(resource.toString());
         }
         java.execute();
     }

Its a resource-enabled equivalent of <apply>, effectively. And you can 
see why I like toString() to be meaningful.

-steve




---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message