You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Rick Mann <rm...@latencyzero.com> on 2006/01/05 06:48:43 UTC
Loading resources from my task's JAR?
I wrote an Ant task that needs to load some files (it's a source
generator that loads template files). I'd like to store those files
in the task's .jar file, but so far I've met with no success. I've
tried storing the files in various parts of the .jar:
templates/Class.tmpl
com/mycompany/tools/dbgen/Class.tmpl
Class.tmpl
and I've tried getting at it with (and more) from within my task's code:
getClass().getResourceAsStream("templates/Class.tmpl")
getClass().getResourceAsStream("/templates/Class.tmpl")
getClass().getResourceAsStream("Class.tmpl")
I test my task with ant. Here's the build.xml snippet:
<path id="test.classpath">
<fileset refid="compile.classpath.fileset"/>
<pathelement location="${target.task}/${taskName}-$
{taskVersion}.jar"/>
</path>
<target name="test-task" depends="task" description="--> Test the
Ant task">
<echo message="classpath: ${java.class.path}"/>
<taskdef name="dbgen"
classname="com.keepmedia.tools.dbgen.DBGenAntTask">
<classpath refid="test.classpath"/>
</taskdef>
<dbgen customizationDir="custDir" destDir="destDir">
<fileset dir="../../db/definitions1">
<include name="*.def"/>
</fileset>
</dbgen>
</target>
In all cases, it returns null. So, I decided to check the class path
(with System.getProperty("java.class.path")), and it's this. Note
that only basic and Ant jars are there (not the jar containing my task).
/Developer/Java/Ant/lib/ant-launcher.jar
/Developer/Java/Ant/lib/ant-antlr.jar
/Developer/Java/Ant/lib/ant-apache-bcel.jar
/Developer/Java/Ant/lib/ant-apache-bsf.jar
/Developer/Java/Ant/lib/ant-apache-log4j.jar
/Developer/Java/Ant/lib/ant-apache-oro.jar
/Developer/Java/Ant/lib/ant-apache-regexp.jar
/Developer/Java/Ant/lib/ant-apache-resolver.jar
/Developer/Java/Ant/lib/ant-commons-logging.jar
/Developer/Java/Ant/lib/ant-commons-net.jar
/Developer/Java/Ant/lib/ant-icontract.jar
/Developer/Java/Ant/lib/ant-jai.jar
/Developer/Java/Ant/lib/ant-javamail.jar
/Developer/Java/Ant/lib/ant-jdepend.jar
/Developer/Java/Ant/lib/ant-jmf.jar
/Developer/Java/Ant/lib/ant-jsch.jar
/Developer/Java/Ant/lib/ant-junit.jar
/Developer/Java/Ant/lib/ant-launcher.jar
/Developer/Java/Ant/lib/ant-netrexx.jar
/Developer/Java/Ant/lib/ant-nodeps.jar
/Developer/Java/Ant/lib/ant-starteam.jar
/Developer/Java/Ant/lib/ant-stylebook.jar
/Developer/Java/Ant/lib/ant-swing.jar
/Developer/Java/Ant/lib/ant-trax.jar
/Developer/Java/Ant/lib/ant-vaj.jar
/Developer/Java/Ant/lib/ant-weblogic.jar
/Developer/Java/Ant/lib/ant-xalan1.jar
/Developer/Java/Ant/lib/ant-xslp.jar
/Developer/Java/Ant/lib/ant.jar
/Developer/Java/Ant/lib/junit.jar
/Developer/Java/Ant/lib/xercesImpl.jar
/Developer/Java/Ant/lib/xml-apis.jar
What can I do to get at resources in my tasks .jar file?
TIA,
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Loading resources from my task's JAR?
Posted by Ray Tayek <rt...@comcast.net>.
At 09:48 PM 1/4/2006, you wrote:
>I wrote an Ant task that needs to load some files ...
>
>and I've tried getting at it with (and more) from within my task's code:
i had a bunch of trouble getting resources from jars a while back,
but was all in java. if you are getting a null when trying to get the
resource from plain old java, then the code below may be of interest.
i had resources with the same name in different packages, with
data/test files in src/ and tst/. also, iirc, there were lot's
problems with file names in windoze. i can send you the zip of the
project if you like.
thanks
package l;
public class L {
public static void main(String[] argument) throws Exception {
}
}
package l;
import junit.framework.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class LTestCase extends TestCase {
public LTestCase(java.lang.String testName) {
super(testName);
}
public static Test suite() {
TestSuite suite = new TestSuite(LTestCase.class);
return suite;
}
public void testGetPropertiesFileAsResurceBundle() {
final ResourceBundle rb=ResourceBundle.getBundle(c.getName());
assertNotNull(""+c,rb);
}
public void testGetAPropertyValue() {
final ResourceBundle rb=ResourceBundle.getBundle(c.getName());
assertEquals(expectedValue,rb.getObject(key));
}
public void testGetPathfromGetResource() {
final String resource="files";
final URL url=c.getResource(resource);
assertNotNull("check for existence of: "+resource,url);
final String path=url.getPath();
assertNotNull("path: ",path);
final File file=new File(path);
assertNotNull("file: ",file);
//assertTrue("file.exists() using: "+file,file.exists());
}
public void testOpenStreamfromGetResource() throws Exception {
final String resource="files/file1.txt";
final URL url=c.getResource(resource);
assertNotNull("check for existence of: "+resource,url);
final InputStream is=url.openStream();
assertNotNull("check for ability to open this as an
input stream: ",is);
final BufferedReader r=new BufferedReader(new
InputStreamReader(is));
assertNotNull("check for ability to open this as a
reader: ",r);
final String line=r.readLine();
assertEquals("check to see if we got the corect
value: ",value,line);
is.close();
}
public void testGetTestPropertiesFileAsResurceBundle() {
final ResourceBundle
rb=ResourceBundle.getBundle(this.getClass().getName());
assertNotNull("check to see if we loaded the
resource bundld",rb);
}
public void testGetATestPropertyValue() {
final ResourceBundle
rb=ResourceBundle.getBundle(this.getClass().getName());
assertEquals(expectedValue+"Test",rb.getObject(key));
}
public void testGetTestPathAsResource() {
final String resource="testFiles"; // no /'s allowed!
final URL url=testC.getResource(resource);
assertNotNull(url.toString(),url);
assertEquals(""+url,resource,(new
File(url.getPath())).getName());
}
public void testGetTestFileResourceInFolder() throws Exception {
final String resource="testFiles/file1.txt";
final URL url=testC.getResource(resource);
assertNotNull("check for existence of: "+resource,url);
final InputStream is=url.openStream();
assertNotNull("check for ability to open this as an
input stream: ",is);
final BufferedReader r=new BufferedReader(new
InputStreamReader(is));
assertNotNull("check for ability to open this as a
reader: ",r);
final String line=r.readLine();
assertEquals(url.toString(),value+"Test",line);
is.close();
}
public void testWriteToTempFile() throws Exception{
final String resource="testFiles";
final URL url=testC.getResource(resource);
final File path=new File(url.getPath()); // hack -
may not always work!
assertNotNull("path from resource",path);
assertTrue("check that path exists",path.exists());
assertTrue("check that path is a
directory",path.isDirectory());
assertTrue("check that path is readable",path.canRead());
assertTrue("check that path is writable",path.canWrite());
final File
file=File.createTempFile(this.getClass().getName().substring(0,3),".ext",path);
assertNotNull("temp file",file);
final Writer w=new FileWriter(file);
w.write(key+"\n");
w.close();
final BufferedReader r=new BufferedReader(new
FileReader(file));
final String line=r.readLine();
r.close();
assertEquals("check what we wrote",key,line);
file.deleteOnExit();
}
public static void main(String[] argument) {
junit.textui.TestRunner.run(suite());
}
private final Class c=L.class,testC=this.getClass();
private final String key="foo",expectedValue="bar",value="l";
}
---
vice-chair http://ocjug.org/
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
RE: Loading resources from my task's JAR?
Posted by Stephen McConnell <mc...@dpml.net>.
> -----Original Message-----
> From: Rick Mann [mailto:rmann@latencyzero.com]
> Sent: Thursday, 5 January 2006 8:26 PM
> To: Ant Users List
> Subject: 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?
In both cases?
> 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?
Sure.
First thing to do is to validate that the correct jar is referenced.
E.g.:
System.out.println(
getClass().getProtectionDomain().getCodeSource().getLocation().toString()
);
Secondly, verify that the url returned from the above references the url
your assuming, and that it does contain the resources your expecting.
/Steve.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Loading resources from my task's JAR?
Posted by Rick Mann <rm...@latencyzero.com>.
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
RE: Loading resources from my task's JAR?
Posted by Stephen McConnell <mc...@dpml.net>.
> -----Original Message-----
> From: Rick Mann [mailto:rmann@latencyzero.com]
> Sent: Thursday, 5 January 2006 8:03 PM
> To: Ant Users List
> Subject: Re: Loading resources from my task's JAR?
>
>
> On Jan 5, 2006, at 1:04 AM, Steve Loughran wrote:
>
> > this should work. make sure you have put the template into
> the JAR.
> > the <jar> task may need to explicitly ask for it, or you should be
> > using <copy> to copy the files from their source dir to
> build/ classes
> > or wherever.
> >
> > I would have an "unjar" target that used <unjar> to unjar a
> jar just
> > built, so you can see what is going on. Once you are 100% sure that
> > the resource is going in, then you can worry about classloading.
>
> I have verified this. Here's the output of jar tf:
>
> $ jar tf targets/task/dbgen-1.1.jar
> META-INF/
> META-INF/MANIFEST.MF
> com/
> com/mycompany/
> com/mycompany/tools/
> com/mycompany/tools/dbgen/
> com/mycompany/tools/dbgen/BaseClass.tmpl <--
> com/mycompany/tools/dbgen/Class.tmpl <--
> com/mycompany/tools/dbgen/Column.class
> com/mycompany/tools/dbgen/DBClassGenerator.class
> com/mycompany/tools/dbgen/DBClassGeneratorCLI.class
> com/mycompany/tools/dbgen/DBGenAntTask.class
> com/mycompany/tools/dbgen/PInterface.tmpl <--
> com/mycompany/tools/dbgen/Table.class
>
> The System property java.class.path does not contain my jar
> or any of its classes, but obviously they're getting loaded
> because the code is executing. It may not be significant that
> my classes aren't in the system classpath. However, I'm not
> sure what the classloader arrangement is in this case (I
> don't know exactly what happens when
> Class.forName() is called).
>
> Now, I'm using the executing class' getResourceAsStream(), so
> one would think it would know where the jar file was.
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");
/Steve.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Loading resources from my task's JAR?
Posted by Rick Mann <rm...@latencyzero.com>.
On Jan 5, 2006, at 1:04 AM, Steve Loughran wrote:
> this should work. make sure you have put the template into the JAR.
> the <jar> task may need to explicitly ask for it, or you should be
> using <copy> to copy the files from their source dir to build/
> classes or wherever.
>
> I would have an "unjar" target that used <unjar> to unjar a jar
> just built, so you can see what is going on. Once you are 100% sure
> that the resource is going in, then you can worry about classloading.
I have verified this. Here's the output of jar tf:
$ jar tf targets/task/dbgen-1.1.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mycompany/
com/mycompany/tools/
com/mycompany/tools/dbgen/
com/mycompany/tools/dbgen/BaseClass.tmpl <--
com/mycompany/tools/dbgen/Class.tmpl <--
com/mycompany/tools/dbgen/Column.class
com/mycompany/tools/dbgen/DBClassGenerator.class
com/mycompany/tools/dbgen/DBClassGeneratorCLI.class
com/mycompany/tools/dbgen/DBGenAntTask.class
com/mycompany/tools/dbgen/PInterface.tmpl <--
com/mycompany/tools/dbgen/Table.class
The System property java.class.path does not contain my jar or any of
its classes, but obviously they're getting loaded because the code is
executing. It may not be significant that my classes aren't in the
system classpath. However, I'm not sure what the classloader
arrangement is in this case (I don't know exactly what happens when
Class.forName() is called).
Now, I'm using the executing class' getResourceAsStream(), so one
would think it would know where the jar file was.
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: SOLVED Loading resources from my task's JAR?
Posted by Rick Mann <rm...@latencyzero.com>.
Okay, I found the problem. I'm very sorry to have wasted everyone's
time. Although I was dumping the name of the file to the log, I was
actually calling getResourceAsStream() with a modified version of the
filename (an artifact left over from my first attempt to put the
templates in a "templates" dir).
That's what I get for writing code during the Rose Bowl...
Thank you to Stephen and Jan for showing me that I was using the
right approach!
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
RE: Loading resources from my task's JAR?
Posted by Stephen McConnell <mc...@dpml.net>.
> -----Original Message-----
> From: Rick Mann [mailto:rmann@latencyzero.com]
> Sent: Thursday, 5 January 2006 8:53 PM
> To: Ant Users List
> Subject: Re: Loading resources from my task's JAR?
>
>
> On Jan 5, 2006, at 1:25 AM, Stephen McConnell wrote:
>
> > Given that the "templates/Class.tmpl" is a relative address
> (relative
> > to the package namespace of the class returned from
> getClass()), and
> > if the class is in the package namespece
> "com/mycompany/tools/dbgen"
> > then woudn't the absolute resource reference would be
> > "/com/mycompany/tools/dbgen/templates.Class.tmpl" ?
>
> The jar tf was for one particular attempt, one where I placed
> the .tmpl files next to the classes (in order to simplify the
> paths used in the code).
>
> I think you're right about the relative templates dir, but is the dot
before "Class" correct?
No - my copy and paste mistake - should be "/".
/Steve.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Loading resources from my task's JAR?
Posted by Rick Mann <rm...@latencyzero.com>.
On Jan 5, 2006, at 1:25 AM, Stephen McConnell wrote:
> Given that the "templates/Class.tmpl" is a relative address
> (relative to the
> package namespace of the class returned from getClass()), and if
> the class
> is in the package namespece "com/mycompany/tools/dbgen" then
> woudn't the
> absolute resource reference would be
> "/com/mycompany/tools/dbgen/templates.Class.tmpl" ?
The jar tf was for one particular attempt, one where I placed
the .tmpl files next to the classes (in order to simplify the paths
used in the code).
I had originally tried to have a "templates" directory at the top of
my jar file. I think you're right about the relative templates dir,
but is the dot before "Class" correct?
I'm trying the debugging in your next email...
file:/Developer/Java/Ant/lib/ant.jar
Interesting! Clearly not the jar file I would've expected. This is
class com.mycompany.tools.dbgen.DBClassGenerator.
--
Rick
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
RE: Loading resources from my task's JAR?
Posted by Stephen McConnell <mc...@dpml.net>.
> -----Original Message-----
> From: Steve Loughran [mailto:stevel@apache.org]
> Sent: Thursday, 5 January 2006 7:34 PM
> To: Ant Users List
> Subject: Re: Loading resources from my task's JAR?
>
> Rick Mann wrote:
> > I wrote an Ant task that needs to load some files (it's a source
> > generator that loads template files). I'd like to store
> those files
> > in the task's .jar file, but so far I've met with no success. I've
> > tried storing the files in various parts of the .jar:
> >
> > templates/Class.tmpl
> > com/mycompany/tools/dbgen/Class.tmpl
> > Class.tmpl
> >
> > and I've tried getting at it with (and more) from within my
> task's code:
> >
> > getClass().getResourceAsStream("templates/Class.tmpl")
>
> this should work.
Given that the "templates/Class.tmpl" is a relative address (relative to the
package namespace of the class returned from getClass()), and if the class
is in the package namespece "com/mycompany/tools/dbgen" then woudn't the
absolute resource reference would be
"/com/mycompany/tools/dbgen/templates.Class.tmpl" ?
/Steve.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Re: Loading resources from my task's JAR?
Posted by Steve Loughran <st...@apache.org>.
Rick Mann wrote:
> I wrote an Ant task that needs to load some files (it's a source
> generator that loads template files). I'd like to store those files in
> the task's .jar file, but so far I've met with no success. I've tried
> storing the files in various parts of the .jar:
>
> templates/Class.tmpl
> com/mycompany/tools/dbgen/Class.tmpl
> Class.tmpl
>
> and I've tried getting at it with (and more) from within my task's code:
>
> getClass().getResourceAsStream("templates/Class.tmpl")
this should work. make sure you have put the template into the JAR. the
<jar> task may need to explicitly ask for it, or you should be using
<copy> to copy the files from their source dir to build/classes or wherever.
I would have an "unjar" target that used <unjar> to unjar a jar just
built, so you can see what is going on. Once you are 100% sure that the
resource is going in, then you can worry about classloading.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org