Here's what I have at the top of my Mojo:
* @goal generate
* @phase process-sources
* @execute phase="compile"
* @requiresDependencyResolution
*/
public class MyMojo extends AbstractMojo
In execute(), I have:
/**
* @see org.apache.maven.plugin.Mojo#execute()
*/
public void execute() throws MojoExecutionException, MojoFailureException
{
Thread currentThread = Thread.currentThread();
ClassLoader oldClassLoader = currentThread.getContextClassLoader();
try
{
currentThread.setContextClassLoader(getClassLoader());
{
doExecute();
}
}
finally
{
currentThread.setContextClassLoader(oldClassLoader);
}
}
getClassLoader() is defined as:
/**
* Returns the an isolated classloader.
*
* @return ClassLoader
* @noinspection unchecked
*/
private ClassLoader getClassLoader()
{
try
{
List classpathElements = project.getCompileClasspathElements();
classpathElements.add(project.getBuild().getOutputDirectory());
classpathElements.add(project.getBuild().getTestOutputDirectory());
URL urls[] = new URL[classpathElements.size()];
for (int i = 0; i < classpathElements.size(); ++i)
{
System.out.println((String) classpathElements.get(i));
urls[i] = new File((String) classpathElements.get(i)).toURL();
}
return new URLClassLoader(urls, this.getClass().getClassLoader());
}
catch (Exception e)
{
getLog().debug("Couldn't get the classloader.");
return this.getClass().getClassLoader();
}
}
When I run this plugin, I get the compile-time dependencies, but not the
<scope>provided</scope> dependencies. The reason I'm using provided is
because I don't want the dependencies to end up in my WAR (I'm using OSGi).
Is it better to use <optional>true</optional> for these dependencies, or is
their a better way to build up the classpath?
Thanks,
Matt
Benjamin Bentmann wrote:
>
> mraible wrote:
>
>> When authoring a plugin, is there an easy way to add the project's
>> classpath
>> to the plugin's classpath? I need access to the project's dependencies in
>> order to resolve some classes.
>
> To my knowledge, there is only the hard way:
>
> Add "@requiresDependencyResolution <scope>" to your mojo to make sure
> Maven
> resolves the dependencies you intend to access.
>
> Acquire the desired class path, either programmatically via
> MavenProject.get(Compile|Runtime|Test)ClasspathElements() or declaratively
> via some mojo parameter with
> default-value="${project.*ClasspathElements}".
>
> Create an (isolated) URLClassLoader from the class path and finally start
> loading the project classes.
>
> There was some proposal about simplifying this common task over at
> [EMAIL PROTECTED]
> [0] but I'm not sure how far this has progressed.
>
>
> Benjamin
>
>
> [0]
> http://www.nabble.com/-proposal--AbstractMojo-enhancement-td16516834.html
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
>
--
View this message in context:
http://www.nabble.com/Adding-project-dependencies-to-a-plugin-tp17199492p17212064.html
Sent from the mojo - user mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email