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:26:41 GMT
Played a little bit more ...


    public void execute() {
        log("My JAR is " + getClass().getProtectionDomain().getCodeSource().getLocation().toString()
);
        log("resource: " + resName);

        InputStream is = getClass().getResourceAsStream(resName);
        log("  stream: " + is);
        
        log("Content:");
        log("-----------------------------------------------------------");
        try {
            int i;
        	while ( (i=is.read()) > -1) {
                System.out.print((char)i);
        	}
        	is.close();
        } catch (Exception e) {
            log("Cant open resource");
        }
        log("-----------------------------------------------------------");
        
        log(" ");
    }



    <target name="test">
        <taskdef name="mytask" classname="oata.taskdefs.MyTask" classpath="${build.dir}/mytasks.jar"/>
        <mytask/>
        <mytask resname="/oata/taskdefs/default.tmpl"/>
        <mytask resname="templates/another.tmpl"/>
        <mytask resname="/templates/another.tmpl"/>
        <mytask resname="does-not-exist.tmpl"/>
    </target>


test:
   [mytask] My JAR is file:/C:/ant/17.3/lib/ant.jar
   [mytask] resource: default.tmpl
   [mytask]   stream: java.util.zip.ZipFile$1@162dbb6
   [mytask] Content:
   [mytask] -----------------------------------------------------------
   [mytask] DEFAULT
   [mytask] -----------------------------------------------------------
   [mytask]
   [mytask] My JAR is file:/C:/ant/17.3/lib/ant.jar
   [mytask] resource: /oata/taskdefs/default.tmpl
   [mytask]   stream: java.util.zip.ZipFile$1@146c1d4
   [mytask] Content:
   [mytask] -----------------------------------------------------------
   [mytask] DEFAULT
   [mytask] -----------------------------------------------------------
   [mytask]
   [mytask] My JAR is file:/C:/ant/17.3/lib/ant.jar
   [mytask] resource: templates/another.tmpl
   [mytask]   stream: null
   [mytask] Content:
   [mytask] -----------------------------------------------------------
   [mytask] Cant open resource
   [mytask] -----------------------------------------------------------
   [mytask]
   [mytask] My JAR is file:/C:/ant/17.3/lib/ant.jar
   [mytask] resource: /templates/another.tmpl
   [mytask]   stream: java.util.zip.ZipFile$1@5f8172
   [mytask] Content:
   [mytask] -----------------------------------------------------------
   [mytask] another template
   [mytask] -----------------------------------------------------------
   [mytask]
   [mytask] My JAR is file:/C:/ant/17.3/lib/ant.jar
   [mytask] resource: does-not-exist.tmpl
   [mytask]   stream: null
   [mytask] Content:
   [mytask] -----------------------------------------------------------
   [mytask] Cant open resource
   [mytask] -----------------------------------------------------------
   [mytask]


Jan

 

>-----Urspr√ľngliche Nachricht-----
>Von: Jan.Materne@rzf.fin-nrw.de [mailto:Jan.Materne@rzf.fin-nrw.de] 
>Gesendet: Donnerstag, 5. Januar 2006 11:13
>An: user@ant.apache.org
>Betreff: AW: Loading resources from my task's JAR?
>
>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
>
>

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


Mime
View raw message