I figured out the problem.  It was a package mismatch that in turn messed up an 
assumption about the location of the resource in the jar file.  Here's what 
happened: the original ant task class resides in package "com.sas.tools.ant".  
However, the Creator class I used to wrap this class resides in a different 
package because of the way I declared it (inside the maven plugin).  Now, the 
ant task class assumes that, in the jar file, the resource it's accessing is in 
the same directory as the class (com\sas\tools\ant).  That's why using  
getClass().getResourceAsStream() can find the resource file.  But that's not 
the case for the wrapper class, so the assumption failed and the code couldn't 
find the resource.

To fix this, I put the wrapper class in its own file and set its package to be 
the same as that of the original Ant task class.  Problem solved. Hope this 
helps somebody else trying to wrap Ant tasks; it was a bear to figure out! :-)

Will  


-----------------------------------------
Will Gwaltney
SAS Institute
[EMAIL PROTECTED]
919-531-9025
"Nothing is more impossible than to write a book that wins every reader's 
approval." - Miguel de Cervantes
-----------------------------------------


-----Original Message-----
From: Will Gwaltney [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, April 19, 2005 2:49 PM
To: Maven Developers List
Subject: [m2] wrapping in a M2 plugin an Ant task that accesses a resource in a 
jar

Hi all,

  I'm writing a maven plugin in java to wrap an Ant taskdef that we use in our 
build process.  I'm having some trouble accessing a resource in the jar that 
contains the taskdef.  The taskdef uses the following code to read the file:

-------------------------------- (snip) ----------------------------------------

   private static final String DEFAULT_TEMPLATE_FILE_NAME = "RB.template";

        .
        .
        .

        BufferedInputStream bis = new 
BufferedInputStream(getClass().getResourceAsStream(DEFAULT_TEMPLATE_FILE_NAME));
        if (bis == null)
          throw new 
BuildException(MessageFormat.format("CreateResourceBundleClasses task could not 
open RB template \"{0}\".",
                                                        new Object[] { 
DEFAULT_TEMPLATE_FILE_NAME }));
        BufferedOutputStream bos = new BufferedOutputStream(new 
FileOutputStream(file));
        byte bytes[] = new byte[Math.max(2048, bis.available())];

-------------------------------- (snip) ----------------------------------------

This works just great when the taskdef is run from Ant, but when I try to run 
it from my maven plugin, the taskdef code throws in the bis.available() method. 
 Here's stack trace:

-------------------------------- (snip) ----------------------------------------

org.apache.maven.plugin.PluginExecutionException: Error generating Resource 
Bundle code
        at org.apache.maven.plugin.rb.RBMojo.execute(RBMojo.java:142)
        at 
org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:432)
        at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeMojo(DefaultLifecycleExecutor.java:448)
        at 
org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:144)
        at org.apache.maven.DefaultMaven.processProject(DefaultMaven.java:212)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:136)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:233)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:324)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:303)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:243)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:416)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:363)
Caused by: java.io.IOException: Stream closed
        at 
com.sas.tools.ant.CreateResourceBundleClasses.createResourceBundles(CreateResourceBundleClasses.java:652)
        at 
com.sas.tools.ant.CreateResourceBundleClasses.execute(CreateResourceBundleClasses.java:503)
        at org.apache.maven.plugin.rb.RBMojo.execute(RBMojo.java:129)
        ... 14 more
Caused by: java.io.IOException: Stream closed
        at 
com.sas.tools.ant.CreateResourceBundleClasses.getDefaultTemplate(CreateResourceBundleClasses.java:460)
        at 
com.sas.tools.ant.CreateResourceBundleClasses.getTemplate(CreateResourceBundleClasses.java:426)
        at 
com.sas.tools.ant.CreateResourceBundleClasses.createResourceBundles(CreateResourceBundleClasses.java:580)
        ... 16 more
Caused by: java.io.IOException: Stream closed
        at java.io.BufferedInputStream.ensureOpen(BufferedInputStream.java:120)
        at java.io.BufferedInputStream.available(BufferedInputStream.java:336)
        at 
com.sas.tools.ant.CreateResourceBundleClasses.getDefaultTemplate(CreateResourceBundleClasses.java:449)
        ... 18 more
-------------------------------- (snip) ----------------------------------------

Note the IOException at the bottom that says "Stream closed".  I assume that 
the getResourceAsStream() method can't find the file, but I can't figure out 
why it doesn't find it in maven but does find it in ant.

BTW, here's the pertinent plugin code that wraps the ant taskdef:

-------------------------------- (snip) ----------------------------------------

                        // create a dummy ant project and target to wrap the 
task we're invoking
                        final class Creator extends CreateResourceBundleClasses 
{
                                public Creator() {
                                        project = new Project();
                                        project.init();
                                        taskType = "create";
                                        taskName = "create";
                                        target = new Target();
                                }       
                        }
                        
                        Creator creator = new Creator();
                        creator.execute();

-------------------------------- (snip) ----------------------------------------

Could this be a difference in classloaders between Ant and Maven? A security 
issue? I'm kind of stumped right now, so any help would be much appreciated!

Will

-----------------------------------------
Will Gwaltney
SAS Institute
[EMAIL PROTECTED]
919-531-9025
"Nothing is more impossible than to write a book that wins every reader's 
approval." - Miguel de Cervantes
-----------------------------------------



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: 
[EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to