> Problem Solved!
>
> It was, of course, a classloader issue. To solve the problem I created my
> own URLClassLoader instance and gave it the URLs in of my MoJo's
> classloader. Then it was a simple matter to load and instantiate my
> utility class from there.
>
> Because I also need access to the project's classpath I also used
> ${project.runtimeClasspathElements} to import that list.
>
> The relevant solution bits are shown at: http://rafb.net/p/TmK9TL26.html
rafb.net is a short-term paste... Here are those relevant bits in case
somebody needs them in the future:
public class GenerateSqlMojo extends AbstractMojo
{
...
/**
* The classpath elements of the project.
* We need this so that we can get the JDBC driver for the project.
*
* @parameter expression="${project.runtimeClasspathElements}"
* @required
* @readonly
*/
private List<String> classpathElements;
public void execute() throws MojoExecutionException
{
final URL[] urls = buildUrls();
final URLClassLoader cl =
new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
Thread.currentThread().setContextClassLoader(cl);
final Class clazz =
cl.loadClass(UtilityExecutor.class.getName());
final Constructor ctor =
clazz.getConstructor(new Class[] { String.class,
String.class });
ctor.newInstance(new Object[] { inputDirectory, outputPrefix });
}
/**
* Combine the MoJo URLS with the project's classpath elements.
* We need the classpath elements because the JDBC driver will be
* specified there.
*
* @return
* @throws MalformedURLException
*/
private URL[] buildUrls() throws MalformedURLException
{
final URL[] mojoUrls =
((URLClassLoader) getClass().getClassLoader()).getURLs();
final URL[] urls =
new URL[mojoUrls.length + classpathElements.size()];
int ndx = 0;
for (final URL url : mojoUrls) { urls[ndx++] = url; }
for (final String cpe : classpathElements) {
urls[ndx++] = new URL("file:/" + cpe); }
return urls;
}
}
UtilityExecutor is a simple wrapper to execute my utility's main class:
public UtilityExecutor(final String inputDirectory,
final String outputDirectory)
{
final MyUtility main = new MyUtility();
main.setSaveOnlyMode(true);
main.setSaveTarget(outputDirectory);
main.execute(new String[] { inputDirectory });
}
>
> Thanks for the pointers Jerome. I would still be beating my head on the
> desk without your help!
>
>> On 4/4/07, James CE Johnson <[EMAIL PROTECTED]> wrote:
>>> Hi Jerome,
>>>
>>> My latest nohup.out is http://rafb.net/p/seMDUu17.html. I've dumped the
>>> classloader of my app as well as the system classloader. It appears
>>> that I have a RealmClassloader loading my classes and it has everything
>>> it needs.
>>>
>>> However, http://rafb.net/p/qC12GT63.html captures my execution from
>>> within Eclipse. Here we see that all of the dependencies are in the
>>> system classloader.
>>>
>>> Perhaps Spring is looking to the (mostly empty) system classloader in
>>> the case where it fails?
>>>
>>> Any insights would be appreciated.
>>
>> In the xfire maven plugin, i've had to override the class loader,
>> otherwise Spring would not find the appropriate classes/resources. See
>>
>> http://svn.codehaus.org/mojo/trunk/mojo/mojo-sandbox/xfire-maven-plugin/src/main/java/org/codehaus/mojo/xfire/WsdlgenMojo.java
>>
>> I would guess that you need to do something similar in your plugin.
>>
>> Cheers,
>>
>> J
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]