ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Jan.Mate...@rzf.fin-nrw.de>
Subject AW: Loading resources from my task's JAR?
Date Thu, 05 Jan 2006 10:12:33 GMT
I did a quick test...
and it works for me.


Jan



./build.xml
---8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<----
<project default="all">

    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build"/>
    
    <property name="classes.dir" value="${build.dir}/classes"/>


    <target name="build">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
        <jar destfile="${build.dir}/mytasks.jar">
            <fileset dir="${classes.dir}"/>
            <fileset dir="${src.dir}" includes="**/*.tmpl"/>
        </jar>
    </target>
    
    <target name="test">
        <taskdef name="mytask" classname="oata.taskdefs.MyTask" classpath="${build.dir}/mytasks.jar"/>
        <mytask/>
        <mytask resname="other.tmpl"/>
    </target>
    
    <target name="all" depends="build,test"/>

</project>
---8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<----


src\oata\taskdefs\default.tmpl
---8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<----
DEFAULT
---8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<----


src\oata\taskdefs\MyTask.java
---8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<----
package oata.taskdefs;

import org.apache.tools.ant.Task;
import java.io.InputStream;

public class MyTask extends Task {

    String resName = "default.tmpl";
    
    public void setResname(String s) {
        resName = s;
    }

    public void execute() {
        log("Hello");
        InputStream is = getClass().getResourceAsStream(resName);
        log("resource: " + resName);
        log("  stream: " + is);
    }

}
---8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<-------8-<----



>ant
Buildfile: build.xml

build:
    [mkdir] Created dir: C:\tmp\ant-taskLoadsResources\build\classes
    [javac] Compiling 1 source file to C:\tmp\ant-taskLoadsResources\build\classes
      [jar] Building jar: C:\tmp\ant-taskLoadsResources\build\mytasks.jar

test:
   [mytask] Hello
   [mytask] resource: default.tmpl
   [mytask]   stream: java.util.zip.ZipFile$1@1c80b01
   [mytask] Hello
   [mytask] resource: other.tmpl
   [mytask]   stream: null

all:

BUILD SUCCESSFUL
Total time: 1 second




>-----Urspr√ľngliche Nachricht-----
>Von: Rick Mann [mailto:rmann@latencyzero.com] 
>Gesendet: Donnerstag, 5. Januar 2006 10:56
>An: Ant Users List
>Betreff: Re: Loading resources from my task's JAR?
>
>
>On Jan 5, 2006, at 1:45 AM, Stephen McConnell wrote:
>
>> This is not a issue of locating the jar file - after all you running 
>> which means you class is loaded which means the JRE has located the 
>> class which means the jar is know.  If you task class is 
>> com/mycompany/tools/dbgen/DBGenAntTask.class then either of the 
>> following two approaches should work:
>>
>>    getClass().getResourceAsStream("Class.tmpl");
>>
>>    getClass().getResourceAsStream("/com/mycompany/tools/dbgen/
>> Class.tmpl");
>
>My thoughts exactly. So, why is it returning null? I only 
>bring up all the other stuff because I was "thinking out 
>loud", trying to find something else to look for. Have you (or 
>anyone else) ever succeeded in doing this?
>
>--
>Rick
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>For additional commands, e-mail: user-help@ant.apache.org
>
>

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


Mime
View raw message